POJ 2661: AD HOC

POJ 2661: Factstone Benchmark

  • Description
  • Data
  • 思路
    • AD HOC
    • 具体分析
      • LOG2
    • AC!
  • Code

——AD HOC
原题传送门

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 y, what will be the Factstone rating of Amtel’s most recently released chip?

Data

Input
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.
Output
For each test case, output a line giving the Factstone rating.

	Sample Input
	1960
	1981
	0
	Sample Output
	3
	8		

1960 <= y <= 2160

思路

好像是一道比较神奇的AD HOC题目?

AD HOC

又称杂题(水题),是一种很考验思路 (智商) 的题目,算法和结构需要自己考虑.
这种题,多做做就会了需要打开思路.

具体分析

这道题让我们分析2x与y!的大小关系.
其中4<=x<=212,这就很尴尬了.
毕竟我们都很聪明 (lazy) ,这道题肯定不能用高精!
否则有大整数乘法和阶乘,还有比较……
所以,我们考虑用double类型来解决爆范围的问题.

LOG2

即使用double,我们也不能直接乘,因为double的精度不能保证到个位.
但是,注意到,本题只需要整型(个位以上的部分).
所以,我们可以把精度无法保证的部分转移到小数位.
而又看到2x,立刻就能想到用LOG2解题!

AC!

想到这里,豁然开朗.
我们将2x转化为log(2x)=x,以2为底取对数.
所以y!就转化为log(y!)=log(y)+log(y-1)+…+log(1)<=log(2x)=x
所以先计算出x,再让i:1到INF循环,每次累加S+=log(i),
当S>x时,i-1即为答案.

Code

#include
#include
#include
using namespace std;
int main()
{
    int n;
    while (~scanf("%d",&n))
    {
        if (n==0) break;
        int i;
        double sum=0.,x;
        x=pow(2.,(n-1960)/10+2);
        for (i=1;x-sum>1e-5;i++) 
            sum+=log2(i);
        printf("%d\n",i-2);
    }
    return 0;
    // Very Short
}

感谢奆老关注 qwq ?

你可能感兴趣的:(题库,AD,HOC)