【水题-数学】 HDU 1141 Factstone Benchmark

本文介绍了一种计算Factstone Benchmark值的方法,通过求解n! < 2^(2^p)来确定最大整数n,其中p=(年份-1960)/10+2。利用对数简化计算过程,适用于从1960到2160年的年份。

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

1141 ( Factstone Benchmark )   


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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值