您的位置:首页 > 其它

HDU 1018 Big Number 详解

2017-02-27 19:58 405 查看


Big Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 36767    Accepted Submission(s): 17654


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

Asia 2002, Dhaka (Bengal)

 

Recommend

JGShining   |   We have carefully selected several similar problems for you:  1071 1003 1020 1061 1060 

 一开始我直接用大数乘法做的,果然超时了,这里附上那个代码

#include <stdio.h>

int main()
{
int t,n,a[100005];
int i,j,place,carry,ans,ter;

scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
a[0]=1;

place=0;//表示位数
for(i=1;i<=n;i++)
{
carry=0;//表示进位
for(j=0;j<=place;j++)
{
a[j]=a[j]*i+carry;
carry=a[j]/10000;
a[j]%=10000;
}
if(carry>0)
{
place++;
a[place]=carry;
}
}
ter=0;
while(a[place])
{
ter++;
a[place]=a[place]/10;
}

ans=4*place+ter;
printf("%d\n",ans);
}

return 0;
}


然后上网看了看他们的思路,都是用的公式
可以将n!表示成10的次幂,即n!=10^M(10的M次方,10^2是3位M+1就代表位数)则不小于M的最小整数就是

    n!的位数,对该式两边取对数,有M=log10^n!即:

    M = log10^1+log10^2+log10^3...+log10^n

    循环求和,就能算得M值,该M是n!的精确位数。

主要是使用了下面这个公式:

log10(n!)=log10(1*2*3…*n)=log10(1)+log10(2)+…+log10(n)

ac代码:

#include <stdio.h>
#include <math.h>

int main()
{
int t,n,i;
double ans;

scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
ans=0;
for(i=1;i<=n;i++)
{
ans+=log10(i*1.0);
}
printf("%d\n",(int)ans+1);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: