HDOJ 1018 Big Number

来源:互联网 发布:双色球参选数据真的吗 编辑:程序博客网 时间:2024/06/05 01:58

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

该问题要用到斯特林公式: log10(n!) = log10(sqrt(2 * pi * n)) + n * log10(n / e)

一个数的位数=log10(n)+1.


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


#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#define PI 3.1415926 using namespace std;int main(){int a;long long b;int sum;cin>>a;while(a--){cin>>b;int sum=(0.5*log(2.0*PI*b) + b*log((double)b) - b) / log((double)10) + 1;cout<<sum<<endl;}return 0;}

0 0
原创粉丝点击