hdu1018 斯特林公式

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.

这题要用到斯特林公式:

求10^7阶乘的位数,将其求出来再进行模运算肯定是溢出的。

我们需要取对数

首先对于一个普通的数字n,他的位数 = (int)log10(n)+1

那么对于n!,我们要求他的位数可以这样:

log10(n!) = 0.5*log10(2πn)+n*log10(n)-n*log10(e)

#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
const double PI = 3.1415926535897932385;
const double e = 2.7182818284590452354;
int main(){
    //freopen("datain.txt","r",stdin);
    int t;scanf("%d",&t);
    while(t--){
        int n;
        scanf("%d",&n);
        double ans=0.5*log10(2*PI*n)+n*log10(n)-n*log10(e);
        printf("%d\n",(int)ans+1);
    }
}

猜你喜欢

转载自blog.csdn.net/hanker99/article/details/84755068