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

  题目:

                                                                                                Big Number
                                        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;也很容易得出其位数。

 

 

 

  代码:

 

     

 

 

   第一种方法:

 1 #include<stdio.h>
2 #include<math.h>
3
4 int main()
5 {
6 int n,i,m;
7 double sum;
8 scanf("%d",&n);
9 while(n--)
10 {
11 sum=0;
12 scanf("%d",&m);
13 for(i=1;i<=m;i++)
14 sum+=log10((double)i);
15 printf("%d\n",(int)sum+1);
16 }
17 return 0;
18 }

     第二种方法:

 

 1 #include<stdio.h>
2 #include<math.h>
3
4 #define e 2.7182818284590452354
5 #define pi acos(-1.00)
6
7 int main()
8 {
9 int n,i,m,sum;
10 scanf("%d",&n);
11 while(n--)
12 {
13 scanf("%d",&m);
14 sum=(int)(1.0/2.0*log10(2.0*pi*m)+1.0*m*log10(m/e)+1);
15 printf("%d\n",sum);
16 }
17 return 0;
18 }

PS.好几天没做题了,这几天挺累的,越发觉得自己的责任所在,越发觉得自己真的该勇敢地去面对这一切,FIGHT!

 

 

posted @ 2012-01-24 19:30  hankers  阅读(429)  评论(0编辑  收藏  举报