Problem Description
Amtel has announced that it will release a 128-bit computer chip by 2010, a 256-bit computer by 2020, and so on, continuing its strategy of doubling the word-size every ten years. (Amtel released a 64-bit computer in 2000, a 32-bit computer in 1990, a 16-bit computer in 1980, an 8-bit computer in 1970, and a 4-bit computer, its first, in 1960.)Amtel will use a new benchmark - the Factstone - to advertise the vastly improved capacity of its new chips. The Factstone rating is defined to be the largest integer n such that n! can be represented as an unsigned integer in a computer word.
Given a year 1960 ≤ y ≤ 2160, what will be the Factstone rating of Amtel's most recently released chip?
There are several test cases. For each test case, there is one line of input containing y. A line containing 0 follows the last test case. For each test case, output a line giving the Factstone rating.
Sample Input
1960
1981
0
Sample Output
3
8
说白了,这题就是让你求最大的n,使其满足n!<2^(2^p),这里的p需要先计算出(p=(年份-1960)/10+2)。但是很明显,当年份为2160时,p=22,那么2^(2^p)是多少呢?这个算起来十分麻烦,数据超过了double的范围。那要怎么办呢?能不能把数字变小一些。仔细观察2^(2^p),有两次指数,能不能去掉一次呢?当然可以,取对数!等式两边取对2的对数,就变成了log2(1)+log2(2)+log2(3)+...+log2(n)<2^p。这样就很容易计算了。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n;
while (cin >> n, n)
{
int p = (n - 1960) / 10 + 2;
p = 1 << p;
double sum = 0.0, fac = 1.0;
while (sum < (double)p)
sum += log(fac) / log(2.0), fac += 1.0;
cout << int(fac - 2.0) << endl;
}
return 0;
}