您的位置:首页 > 其它

hdoj1018 大数阶乘位数

2015-10-04 01:04 453 查看

[align=left]题目如下[/align]

[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]
[align=left]2[/align]
[align=left]10[/align]
[align=left]20[/align]

[align=left]Sample Output[/align]
[align=left]7[/align]
[align=left]19[/align]

[align=left]题意是让我们求出给定数字的阶乘的结果数字位数[/align]
[align=left]属于一道涉及数学的水题[/align]

[align=left]我们知道对任意整数n的位数为 log10(n)+1[/align]
[align=left]所以对n!的位数即 log10(n)+1[/align]

[align=left]根据所知资料可以得到关于求阶乘近似值的一个公式[/align]

斯特林(Stirling)公式:



[align=left]在这个近似值求解中近似度不影响到位数[/align]
[align=left]所以我们可以在求解过程中带入这个公式[/align]

[align=left]n!的位数即 1/2*log10(2*PI*n)+n*log10(n/e)+1[/align]

[align=left]知道思路后代码即可一遍AC[/align]

#include<iostream>
#include<cmath>
using namespace std;

const double PI = 3.141592653;
const double E = 2.71828182846;
int x, y;

int solve(int a)
{
if (a <= 3)
{
return 1;
}
else
{
return log10(2 * PI*a) / 2 + a*log10(a / E) + 1;
}
}

int main()
{
cin >> x;
while (x--)
{
cin >> y;
cout << solve(y) << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: