POJ 1423 Big number

本文介绍了一个使用Stirling公式来计算任意大整数阶乘位数的方法,并提供了具体的C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目来源

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

输入由几行整数数组成。第一行包含一个整数n,这是要测试的情况的数量,后面是n行,每行一个整数 1 <= m <= 10^7

Output

输出包含输入中出现的整数的阶乘的位数。

Sample Input

2
10
20

Sample Output

7
19

Source

Dhaka 2002

Analyze

考的是 Stirling 公式。。。

z = x ^ y => y = logx z
我们易知 n 的位数为 (lg n) + 1

当 n 很大时, n! 接近于 sqrt(2 * PI * n (n/e)^n ).

两边取对数:

lg n! = (lg 2PIn) / 2 + n * lg n/e

所以:

n! 的位数为: res = (lg 2PIn) / 2 + n * lg n/e + 1;

Code

#include<iostream>
#include<cmath>
using namespace std;
const double e = 2.7182818284590452354, pi = 3.141592653589793239;
double strling_digits_num(int n)
{
	return log10(2*pi*n)/2.0+n*(log10(n/e));     //0ms
	//return log10( sqrt( 2 * pi * n ) ) + n * log10( n / e );    //16ms
}
 
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		double m=0;
		m=strling_digits_num(n);
		int answer=(int)m;
		//注意!!!!10的n次方有n+1位数字
		answer++;
		cout<<answer<<endl;
	}
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值