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]
[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; }
相关文章推荐
- 读jQuery官方文档:$(document).ready()与避免冲突
- XAMPP Apache + MySQL + PHP + Perl
- 信息安全系统设计基础第四周学习内容
- 【VBS教程1】:基本输入输出
- [LeetCode] Game of Life
- 计算结构体、数组、指针的sizeof
- 绘制Wifi 矢量图标
- 根据基类指针判断指针指向的子类
- 数据库语言(三):MySQL、PostgreSQL、JDBC
- 【站在巨人的肩膀上】值得推荐的C/C++框架和库
- 第四周(9.28-10.5)
- 用javascript实现五子棋(一)
- 轻量级ORM框架初探-Dapper与PetaPoco的基本使用
- Tutorial: ROS Control
- 进程管理工具的使用
- 4.Python深入_闭包
- boost::bind释疑
- 对xcode的project,target,scheme的理解
- HDOJ 5154 Harry and Magical Computer(拓扑)
- UVALive 7417 思维