题目:
• 题意:
1960年发行了4位计算机,从此以后每过10年,计算机的位数变成两倍。输入某一个年份,求出在这个年份的最大的整数n使得n!能被一个字表示。
• 限制:年份1960<=n<=2160,且n%10 == 0
• 解法:
由于位长最多为2^22,能够表示的数范围很大,所以我们考虑使用log来缩小数值范围
如果n!能够被位长为 bit_len 的字表示,那么应该
有 n ! < 2^bitLen
也就是,log2(n!) < bitLen
即是,log2(1)+log2(2)+….+log2(n-1)+log2(n) < bitLen
那么,其实我们只需要从小到大枚举n,再做判断就可以了
代码:
// Problem#: 1119
// Submission#: 5152249
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
int getMaxN(int year)
{
int bitLen = 1 << ((year - 1960) / 10 + 2), n = 1;
double tmp = 0;
while (tmp / log(2) < bitLen)
{
tmp += log(++n);
}
return n - 1;
}
int main() {
int year;
while (cin >> year&&year)
{
cout << getMaxN(year) << endl;
}
//system("pause");
}