http://poj.org/problem?id=1423
Big Number
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 22141 | Accepted: 7064 |
Description
In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of digits in the factorial of the number.
Input
Input consists of several lines of integer numbers. The first line contains an integer n, which is the number of cases to be 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 10 20
Sample Output
7 19
Source
一开始按照白书73页敲(当时就知道要TEL了),抱着侥幸的心理还是试了试,把组数开大。果然试了几次一直TEL果断放弃。
看了网上的题解发现是有公式的。
代码1:TLE
#include<iostream>
#include<cstring>
const int maxn =30000;
int f[maxn];
using namespace std;
int main()
{
int i,j,n,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(f,0,sizeof(f));
f[0]=1;
for(i=2;i<=n;i++)
{
int c=0;
for(j=0;j<maxn;j++)
{
int s=f[j]*i+c;
f[j]=s;
c=s/10;
}
}
for(j=maxn-1;j>=0;j--)
if(f[j])
break;
printf("%d\n",j+1);
}
return 0;
}
#include<cstring>
const int maxn =30000;
int f[maxn];
using namespace std;
int main()
{
}
代码2:AC
//log在c语言里面是自然对数
//可以用斯特林(Stirling)公式求解
//于是求n!的位数就是求log10((2*PI*n)^1/2*(n/e)^n)+1 即
//const double PI=acos(-1.);
//const double e=exp(1.);
//Stiring公式ap=log10(sqrt(2*PI*n))+n*log10(n/e)+1;
#include<iostream>
const double PI=3.141592653589;
const double e=2.718281828459;
#include<cmath>
using namespace std;
int main()
{
}