hdu 1018 Big Number 两种步骤 log方法(300+ms)+斯特林公式(0+ms)
hdu 1018 Big Number 两种方法 log方法(300+ms)+斯特林公式(0+ms)
Total Submission(s): 28178 Accepted Submission(s): 12819
斯特林公式(维基百科):
Big Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 28178 Accepted Submission(s): 12819
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
log方法代码:
#include <stdio.h> #include <math.h> #define MAX 10000010 int main() { int t ; scanf("%d",&t) ; while(t--) { int n ; double ans = 0.0 ; scanf("%d",&n) ; for(int i = 1 ; i <= n ; ++i) { ans += log10((double)i) ; } printf("%d\n",(int)ceil(ans)) ; } return 0 ; }
斯特林公式(维基百科):
代码:
与君共勉
#include <stdio.h> #include <math.h> #define M_E 2.7182818284590452354 #define M_PI 3.14159265358979323846 int main() { int t ; scanf("%d",&t) ; while(t--) { double n ; scanf("%lf",&n) ; int ans ; if(n == 1) { ans = 1.0 ; } else { ans = (int)ceil(0.5*log10(2*M_PI*n)+n*log10(n)-n*log10(M_E)) ; } printf("%d\n",ans) ; } return 0 ; }
与君共勉