hdu Big Number

 

Big Number

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 480    Accepted Submission(s): 330
 
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

分析:此题为斯特林公式的应用。斯特林公式如下:

取对数:lg(n!) = (lg(2*pi)+lg(n))/2 + n*(lg(n)-lg(e))

#include<iostream>
#include<cmath>

using namespace std;

int main()
{
    int test,n;
    long long int s;
    const long double c1 = 0.798179868358; //lg(2*pi)
    const long double c2 = 0.434294481903; //lg(e)
    long double c3;
    cin >> test;
    while(--test+1)
    {
        cin >> n;
        c3 = log10((double)n);
        s = 1;
        if(n > 3)
        {
            s = (c3 + c1)/2 + n * (c3 - c2) + 1;
            cout << s << endl;
        }
        else
            cout << 1 << endl; 
    }
    return 0;
}

 

 

 

posted @ 2012-08-30 21:31  YogyKwan  阅读(147)  评论(0编辑  收藏  举报