POJ 2661Factstone Benchmark(斯特林公式)

链接:传送门
题意:一个人自命不凡,他从1960年开始每10年更新一次计算机的最长储存长数。1960年为4位,每10年翻一倍。给出一个年份y,问这一年计算机可以执行的n!而不溢出的最大n值
思路:如果直接比较 2^x - 1 < n! 是一定会溢出的,所以不妨对左右取对数,使之变为 x*log10(2) < log10(n!)
使用斯特林公式优化一下n!的计算就能解决这个问题了。


/*************************************************************************
    > File Name: poj2661.cpp
    > Author:    WArobot 
    > Blog:      http://www.cnblogs.com/WArobot/ 
    > Created Time: 2017年04月26日 星期三 22时52分03秒
 ************************************************************************/

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

#define PI 3.1415926535
int y , n;
int main(){
	double t = log10(2);
	while(~scanf("%d",&y) && y){
		int d = (y-1960)/10;
		int x = pow(2.0,d)*4;
		double tmp;
		for(n = 1; 1 ; n++){
			tmp = log10( sqrt(2*PI*n)) + n*log10(n*1.0/exp(1));
			if(x*t<tmp)	break;
		}
		printf("%d
",n-1);
	}
	return 0;
}
原文地址:https://www.cnblogs.com/WArobot/p/6775543.html