using namespace std;
This is an AC solution in C++. This uses a special library from Boost, as calculating very big values in C++ is very difficult as compared to Java which has a BigInteger class.
The logic is same, but do note the changes in your code.
You will have to reset the values of F and I every time you want to calculate a factorial of a different number.
What you did was correct if the values of the number was increasing or constant but never less than the previous number (somewhat like DP!).