poj 1423 Big Number——斯特林公式

本文介绍如何利用Stirling公式解决实际问题,即给定一个大整数n,快速计算其阶乘的位数。通过C++代码实现并提供了一个实例,展示了如何通过对数运算和数学公式简化计算过程。

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

Big Number
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 <= m <= 10^7 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

题意: 求n的阶乘的结果有多少位数。

题解: Stirling公式,然后对n!求以10为底的对数就行。

c++ AC 代码:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int test,n;
	long long int s;
	const long double c1 = 0.798179868358; //lg(2*pi)
	const long double c2 = 0.434294481903; //lg(e)
	long double c3;
	cin >> test;
	while(--test+1)
	{
		cin >> n;
		c3 = log10((double)n);
		s = 1;
		if(n > 3)
		{
			s = (c3 + c1)/2 + n * (c3 - c2) + 1;
			cout << s << endl;
		}
		else	cout << 1 << endl; 
	}
	return 0;
}

代码参考自:https://blog.csdn.net/qq_38633884/article/details/102533596

其实之前数学竞赛培训学过这个公式,第一眼看这个公式的名字就感觉挺熟悉的,不过那时候没好好听课,所有学的不牢。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值