hdu 1018 Big Number 两种方法 log方法(300+ms)+斯特林公式(0+ms

Big NumberTime Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 28178Accepted 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

21020

Sample Output

719

log方法代码:

#include <stdio.h>#include <math.h>#define MAX 10000010int 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_E2.7182818284590452354#define M_PI3.14159265358979323846int 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 ;}与君共勉

,爱情使人忘记时间,时间也使人忘记爱情。

hdu 1018 Big Number 两种方法 log方法(300+ms)+斯特林公式(0+ms

相关文章:

你感兴趣的文章:

标签云: