Big Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 45155 Accepted Submission(s): 22070
Problem 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 ≤ n ≤ 107 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
题意:求n!的位数
如果不刷到这个题还不知道呢,,,
斯特林公式是:(int)log10(n)+1
作用:求n的位数
还有 一个 可以 求 n!的阶乘……
可以直接用第一个公式,也可以两个结合。
/*
听说有一个斯特林公式 能求 一个数的位数 (int)log10(n)+1
*/
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <iomanip>
#define ll long long
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
int main()
{
ios::sync_with_stdio(false);
ll t,n;
double sum;//注意用 double
cin>>t;
while(t--)
{
sum=0;
cin>>n;
for(int i=1;i<=n;i++)
{
sum+=log10(i);
}
cout<<(int)sum+1<<endl;
}
return 0;
}
两个结合版。注意化简
lg(n!)=lg(sqrt(2*pi*n))+n*lg(n/e)
/*
听说有一个斯特林公式 能求 一个数的位数 (int)log10(n)+1
*/
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <iomanip>
#define ll long long
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
#define e 2.7182818284
using namespace std;
int main()
{
ios::sync_with_stdio(false);
ll t,n;
//double sum;//注意用 double
cin>>t;
while(t--)
{
//sum=0;
cin>>n;
cout<<(int)(log10(sqrt(2*pi*n))+n*log10(n/e)+1)<<endl;
}
return 0;
}