HDU 1018 Big Number

HDU 1018 Big Number

斯特灵公式是一条用来取n阶乘近似值的数学公式。一般来说,当n很大的时候,n阶乘的计算量十分大,所以斯特灵公式十分好用,而且,即使在

 

n很小的时候,斯特灵公式的取值已经十分准确。

公式为:

这就是说,对于足够大的整数n,这两个数互为近似值。更加精确地:

或者:

 

 

Big Number

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8759    Accepted Submission(s): 3879


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 ≤ 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(n!)=(0.5*log(2*PI*n)+n*log(n)-n)/log(10)

#include 
< iostream >
#include 
< cstdio >
#include 
< cmath >

const   double  PI  =   3.1415926 ;

int  main()
{
    
int n;
    
int tmp;
    
while~scanf("%d"&n ) )
    
{
        
forint i = 0; i < n; i++ )
        
{
            scanf(
"%d"&tmp);
            
double cnt = 1;
            cnt 
+= (0.5 * log( 2 * PI * tmp ) + tmp * log( tmp ) - tmp ) / log(10);
            printf(
"%d\n", (int)(cnt));
        }

    }

    
return 0;
}

你可能感兴趣的:(HDU 1018 Big Number)