POJ 1423:Big Number 求N的阶乘的长度 斯特林公式
Big Number
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 27027 | Accepted: 8626 |
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 <= m <= 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
题意是要求N!有多少位。
因为有斯特林公式,所以求n!的位数即log10(n)=log10(sqrt(2*acos(-1.0)*n))+n*log10(n/exp(1.0));
代码:
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <cstring> #pragma warning(disable:4996) using namespace std; int main() { int test; long long n; cin >> test; while (test--) { cin >> n; double re = log10(sqrt(2 * acos(-1.0)*n)) + n*log10(n / exp(1.0)); cout << (int)re + 1 << endl; } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。