poj 1423 Big Number<<求N!位数>>

时间:2022-11-21 20:54:33
Big Number
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 27542 Accepted: 8789
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

Dhaka 2002


当超时后可以打表出中间几个值,,,打几个值时间复杂度就会缩小几倍。。。。。


代码:

#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
	int t;scanf("%d",&t);
	while (t--)
	{
		int n;
		int i;
		double j,kai, p=0,s=0;
		scanf("%d",&n);
		int kkk[11]={0,1000000,2000000,3000000,4000000,5000000,6000000,7000000,8000000,9000000};
		double shu[11]={0.0,5565708.917187,11733474.577126,18128483.956099,24671065.737818,31323381.360738,38063144.399049,44875628.728418,51750367.891344,58679536.124036};
		for (i=9;i>=0;i--)
		{
			if (n>=kkk[i])
			{
				s=shu[i];
				p=kkk[i];
				break;
			}
		}
		for (j=p+1;j<n+1;j++)
		{
			s=s+ log10(j);
		}
		long long pp=s;
		printf("%lld\n",pp+1);
	}
	return 0;
}

斯格林公式  代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define PI 3.1415926
#define E 2.7182818
#define LL long long
LL s;
double n;
int main()
{
	int t;scanf("%d",&t);
	while (t--)
	{
		scanf("%lf",&n);
		if (n==1) {
			printf("1\n");
			continue;
		}
		s=(LL)((log10(sqrt(4.0*acos(0.0)*n))+n*(log10(n)-log10(exp(1.0))))+1);
	//	s=(LL)(1+log10(sqrt(2*PI*n)*pow(n/E,(int)n)));
		printf("%lld\n",s);
	}
	return 0;
}