您的位置:首页 > 其它

hdu 1018 Big Number

2014-01-06 11:19 309 查看
[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
The output contains the number of digits in the factorial of the integers appearing in the input.

[align=left]Sample Input[/align]

2
10
20

[align=left]Sample Output[/align]

7
19
[b]n!的位数len=log10(n!)=log10(n)+log10(n-1)+log10(n-2)+......+log10(2)+log10(1)+1.再对len取整就可以了[/b]
[b]直接求代码如下:差点超时[/b]

#include<stdio.h>
#include<math.h>
int main()
{
int n,t,i;
double count;
scanf("%d",&t);
while(t--)
{
count=0;scanf("%d",&n);
for(i=2;i<=n;i++)
count+=log10(i);
printf("%d\n",int(count)+1);
}
return 0;
}


下面介绍一个一个公式log10(n!)=1.0/2*log10(2*pi*n)+n*log10(n/e)

lnN!=NlnN-N+0.5ln(2N*pi)

而N的阶乘的位数等于:log10(N!)取整后加1

log10(N!)=lnN!/ln(10)所以len=lnN!/ln(10)+1

#include<stdio.h>
#include<math.h>
const double pi=acos(-1.0);
int main()
{
int t,n,count;
scanf("%d",&t);
while(t--)
{
count=0;
scanf("%d",&n);
count=(0.5*log(2*pi*n)+n*log(n)-n)/log(10);
printf("%d\n",count+1);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: