hdu 1018 Big Number
2014-01-06 11:19
309 查看
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
The output contains the number of digits in the factorial of the integers appearing in the input.
[align=left]Sample Input[/align]
2
10
20
[align=left]Sample Output[/align]
7
19
[b]n!的位数len=log10(n!)=log10(n)+log10(n-1)+log10(n-2)+......+log10(2)+log10(1)+1.再对len取整就可以了[/b]
[b]直接求代码如下:差点超时[/b]
下面介绍一个一个公式log10(n!)=1.0/2*log10(2*pi*n)+n*log10(n/e)
lnN!=NlnN-N+0.5ln(2N*pi)
而N的阶乘的位数等于:log10(N!)取整后加1
log10(N!)=lnN!/ln(10)所以len=lnN!/ln(10)+1
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
The output contains the number of digits in the factorial of the integers appearing in the input.
[align=left]Sample Input[/align]
2
10
20
[align=left]Sample Output[/align]
7
19
[b]n!的位数len=log10(n!)=log10(n)+log10(n-1)+log10(n-2)+......+log10(2)+log10(1)+1.再对len取整就可以了[/b]
[b]直接求代码如下:差点超时[/b]
#include<stdio.h> #include<math.h> int main() { int n,t,i; double count; scanf("%d",&t); while(t--) { count=0;scanf("%d",&n); for(i=2;i<=n;i++) count+=log10(i); printf("%d\n",int(count)+1); } return 0; }
下面介绍一个一个公式log10(n!)=1.0/2*log10(2*pi*n)+n*log10(n/e)
lnN!=NlnN-N+0.5ln(2N*pi)
而N的阶乘的位数等于:log10(N!)取整后加1
log10(N!)=lnN!/ln(10)所以len=lnN!/ln(10)+1
#include<stdio.h> #include<math.h> const double pi=acos(-1.0); int main() { int t,n,count; scanf("%d",&t); while(t--) { count=0; scanf("%d",&n); count=(0.5*log(2*pi*n)+n*log(n)-n)/log(10); printf("%d\n",count+1); } return 0; }
相关文章推荐
- hdu1018 big number
- HDU 1018 Big Number 计算N!的位数
- 求位数 hdu 1018 Big Number
- hdu 1018 Big Number (数论)
- HDU 1018 Big Number
- hdu 1018 Big Number(纯数学公式)
- HDU 1018 Big Number
- hdu 1018 Big Number(n!求位数)
- hdu 1018 big number (有个组合公式)
- 【再谈阶乘的近似解】HDU 1018——Big Number
- HDU 1018 Big Number
- HDU 1018 Big Number (数的阶乘的长度:数学)
- hdu 1018 Big Number(纯数学公式)
- HDU-1018-Big Number( 这段数学推导我给满分! && 然而并不想用斯特林公式! )
- HDU 1018 Big Number
- HDU_1018 Big Number
- hdu 1018 Big Number (斯特林公式)
- hdu 1018 Big Number
- HDU 1018 Big Number
- HDU 1018 Big Number