The most important things to note while solving a problem are:
- Make sure that the algorithm that you are using for solving the problem has the time complexity according to the time limit specified in the problem. This step is very important.
Here is a discussion about time limits and the maximum looping in a program to avoid time limit exceed: Click here
2. Make sure that the data types being used are according to the maximum possible values( given by the constraints of the problem), that particular variable can take during execution.
If the constraints on n are 1<=n<=10^18 and you are declaring n as int, it will lead to wrong answer, as the int does not have the space to store such a large value. So, you need to use long long int.
Here are a few things about handling large numbers in C++:
a) In c++ the data types long long int and unsigned long long int are enough for handling the upper bounds of most of the problems.
b) If still there is a need for storing more larger number you need to do it using an array.
For these kinds of problems,there will an array based algorithm for solving them.
The problem : small factorials uses an array for storing the factorial of numbers.
The limit of long long int is -2^63+1 to +2^63-1, so by looking at the constraints, try and deduce, that the values should be stored in long long int or you will need an array for storage of value.