zoj 1526 Big Number (N阶乘的位数)

注:还是推公式,类似zoj2277,打表超内存(话说才10^7数组就爆了,也是醉了),默默暴力计算930ms险过


Big Number

Time Limit: 10 Seconds       Memory Limit: 32768 KB

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.


题意:求n!的位数

设  n!= a * 10^k  ( 1<=a<10 )

(1*2*3*4*...*n)  = a * 10^k

两边对10取对数: log10(1*2*3*4*...*n) = log10(a * 10^k)

log10(1)+log10(2)+log10(3)+log10(4)+...+log10(n) = log10(a)+k

因为 0<=log10(a)<1

所以将左边数加起来,再向下取整,结果就是k

最后结果为k+1 (10^0 =1)


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#define N 10000010
using namespace std;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        double sum=0.0,ans;
        for(int i=1; i<=n; i++){
            sum=sum+log10(i*1.0);
        }
        ans=floor(sum);
        printf("%.0f\n",ans+1.0);
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/const_qiu/article/details/47188077