码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1018 Big Number

时间:2015-01-22 17:19:53      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

Big Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27546    Accepted Submission(s): 12525


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
 
这个题用模拟很容易得到答案,但是10的7次方!!!!!!毫无疑问超时超内存。
这里用到一个stirling公式,斯特林公式(Stirling‘s approximation)是一条用来取n的阶乘的值近似值的数学公式。一般来说,当n很大的时候,n阶乘的计算量十分大,所以斯特林公式十分好用,而且,即使在n很小的时候,斯特林公式的取值已经十分准确。   stirling公式:   技术分享
但只用这个公式远远不够,因为n的阶乘算到十几就超int范围了,这题要算n!阶乘的位数,对于一个数a,若有10^(x-1)<a<10^x,则a为x位的整数,所以求n!的位数对公式两边取以10为底的对数,算出来是小数,转为int型后要加1.
x=int(lgn!)=int((lg2*PI+lgn)/2+n*(lgn-lge))+1。
注意n=1的情况。
技术分享
 1 #include <iostream>
 2 #include <cmath>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int n,m;
 8     double s;
 9     cin>>n;
10     while (n--)
11     {
12           s=0.0;
13           cin>>m;
14           if (m==1)
15           s=0;
16           else
17           s=(log10(2*3.141592654)*0.5+log10(m)*0.5+m*(log10(m)-log10(2.718281828459)));
18            cout <<int(s+1)<<endl;
19     }
20     return 0;
21 }
View Code

 

hdu 1018 Big Number

标签:

原文地址:http://www.cnblogs.com/arno-my-boke/p/4241961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!