您的位置:首页 > 其它

HDU 1018--关于阶乘大数问题

2012-01-24 19:30 225 查看
题目:

[b] Big Number[/b]
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13404 Accepted Submission(s): 5967

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

分析:

第一种方法:

N!=1*2*3....*n 要

求位数我们很自然想到对一个数取对数,

log10(n!)=log10(1)+ log10(2) +log10(3)...+log10(n)


第二种方法:

关于斯特林数,有斯特林公式:

lnN!=NlnN-N+0.5*ln(2*N*pi;也很容易得出其位数。

代码:

第一种方法:

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

int main()
{
int n,i,m;
double sum;
scanf("%d",&n);
while(n--)
{
sum=0;
scanf("%d",&m);
for(i=1;i<=m;i++)
sum+=log10((double)i);
printf("%d\n",(int)sum+1);
}
return 0;
}


第二种方法:

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

#define e 2.7182818284590452354
#define pi acos(-1.00)

int main()
{
int n,i,m,sum;
scanf("%d",&n);
while(n--)
{
scanf("%d",&m);
sum=(int)(1.0/2.0*log10(2.0*pi*m)+1.0*m*log10(m/e)+1);
printf("%d\n",sum);
}
return 0;
}


PS.好几天没做题了,这几天挺累的,越发觉得自己的责任所在,越发觉得自己真的该勇敢地去面对这一切,FIGHT!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: