hdu_1018_Big Number_求阶乘长度模板题

来源:互联网 发布:报价单自动生成软件 编辑:程序博客网 时间:2024/05/08 12:10

阶乘长度模板!!

http://acm.hdu.edu.cn/showproblem.php?pid=1018

Big Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12440    Accepted Submission(s): 5558


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
21020
 

Sample Output
719
 

    1. #include<iostream>
    2. #include <cmath>
    3. using namespace std;
    4. int normal(double n)
    5. {
    6.     double x=0;
    7.     while(n)
    8.     {
    9.         x +=log10(n);
    10.         n--;
    11.     }
    12.     return (int)x+1;
    13. }
    14. long stirling(double n)
    15. {
    16.     long x=0;
    17.     if( n ==1 )
    18.         x = 1;
    19.     else
    20.     {
    21.         x = (long)( (log10(sqrt(4.0*acos(0.0)*n)) + n*(log10(n)-log10(exp(1.0)))) + 1 );
    22.     }
    23.     return x;
    24. }
    25. int main()
    26. {
    27.     int n;
    28.     cin>>n;
    29.     while(n--)
    30.     {
    31.         int x;
    32.         cin>>x;
    33.         cout<<stirling(x)<<endl;
    34.     }
    35.     return 0;
    36. }