POJ 1423 Big Number

Big Number

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 <= m <= 10^7 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

 

这题也是利用log10这个函数,和POJ 1019一样,这不过在这里,如果对每个数单独求值,则会超出时间。因而我们采用了以空间来换时间的策略,将8000001个数先计算出来保存在内存中,对于在这个范围的数,则直接取出,超出这个范围的,则进一步计算。

 

具体代码如下:

 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <time.h>
 4 
 5 //log10(n!)求的就是n!的位数,然后展开之
 6 #define LEN 8000001
 7 double result[LEN];
 8 int count(int n)
 9 {
10     double var = 0.0;
11     //clock_t start = clock();
12     int i;
13     for(i = LEN; i <= n; i++)
14         var += log10((double)i);
15     return (int)(result[LEN - 1] + var) + 1;
16     //clock_t end = clock();
17     //printf("%f", (double)(end -start)/CLOCKS_PER_SEC);
18     
19 }
20 
21 int main()
22 {
23     result[0] = 0;
24     result[1] = 0;
25     result[2] = log10((double)2);
26     int i;
27     for(i = 3; i < LEN; i++)
28     {
29         result[i] = result[i-1] + log10((double)i);
30     }
31     int cases;
32     int n;
33     scanf("%d", &cases);
34     while (cases--)
35     {
36         scanf("%d", &n);
37         if(n <= 800000)
38             printf("%d\n",(int)result[n] + 1);
39         else
40             printf("%d\n", count(n));
41     }    
42     return 0;
43 }

posted on 2012-06-28 14:54  NULL00  阅读(1160)  评论(1编辑  收藏  举报

导航