首页 > 其他 > 详细

hdu 1018 Big Number 两种方法 log方法(300+ms)+斯特林公式(0+ms)

时间:2015-03-10 19:29:04      阅读:204      评论:0      收藏:0      [点我收藏+]

Big Number

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


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
 log方法代码:
#include <stdio.h>
#include <math.h>
#define MAX 10000010
int main()
{
	int t ;
	scanf("%d",&t) ;
	while(t--)
	{
		int n ;
		double ans = 0.0 ;
		scanf("%d",&n) ;
		for(int i = 1 ; i <= n ; ++i)
		{
			ans += log10((double)i) ;
		}
		printf("%d\n",(int)ceil(ans)) ;
	}
	return 0 ;
}

斯特林公式(维基百科):
代码:
#include <stdio.h>
#include <math.h>
#define M_E		2.7182818284590452354
#define M_PI		3.14159265358979323846
int main()
{
	int t ;
	scanf("%d",&t) ;
	while(t--)
	{
		double n ;
		scanf("%lf",&n) ;
		int ans ;
		if(n == 1)
		{
			ans = 1.0 ;
		}
		else
		{
			ans = (int)ceil(0.5*log10(2*M_PI*n)+n*log10(n)-n*log10(M_E)) ;
		}
		printf("%d\n",ans) ;
	}
	return 0 ;
}

与君共勉

hdu 1018 Big Number 两种方法 log方法(300+ms)+斯特林公式(0+ms)

原文:http://blog.csdn.net/lionel_d/article/details/44177719

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!