Big number
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 27537 |
|
Accepted: 8787 |
Description in many applications very large integers numbers is required. Some of these applications is using keys for secure transmission of data, encryption, etc. In this problem you is given a number, you has to determine the number of digits in the factorial of the number.
Input input consists of several lines of the integer numbers. The first line contains a integer n, which is the number of cases to being tested, followed by n lines, one integer 1 <= M <= 10^7 on each line.
Output the output contains the number of digits in the factorial of the integers appearing in the input.
Sample Input
2
20
Sample Output
7
19
Ideas:
Seek log10 (n!) for the number of digits of the factorial of N. =LOG10 (1) +log10 (2) +......log10 (n)
another formula is given in the art of computer programming.
n! = sqrt (2*π*n) * ((n/e) ^n) * (1 + 1/(12*n) + 1/(288*n*n) + O (1/n^3))
π= ACOs ( -1)
e = exp (1)
10 logarithm on both sides
Ignore log10 (1 + 1/(12*n) + 1/(288*n*n) + O (1/n^3)) ≈log10 (1) = 0
Get the formula
log10 (n!) = log10 (sqrt (2 * pi * n)) + N * log10 (n/e) . Baidu to the ... Forgive me for not being good at maths ~ ~ ~ Ac-code:
#include <iostream>
#include <cmath>
using namespace std;
Double E=exp (1.0), Pi=acos ( -1.0);
int main ()
{
int T;
Long long N,ans;
cin>>t;
while (t--)
{
cin>>n;
Ans=int (log10 (sqrt (2.0*pi*n)) +n*log10 (n/e)) +1;
cout<<ans<<endl;
}
return 0;