Title Address: POJ 2661
Test instructions: Starting from 1960, the maximum number of storage bits of the computer is updated every 10 years, with the first 1960-year word length being 4 bits, and the subsequent growth of one-fold every 10 years. Give you a year, ask this year, the computer can execute n! The maximum n value without overflow.
Idea: The number of digits in the nth year k=2^ (y-1960)/10, the largest unsigned integer that can be placed in K-bit is (2^k)-1 if it is directly not greater than (2^k)-1 n! is easy to overflow and slow, we introduce a logarithmic operation that is based on log2 (n! ) =log2 (1) +.....+log2 (n) <=log2 ((2^k)-1)
#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>#pragma COMMENT (linker, "/stack:102400000,102400000")using namespace STD;typedef Long LongLL;Const intinf=0x3f3f3f3f;Const DoublePi=ACOs(-1.0);Const Doubleesp=1e-7;intMain () {intNDoubleK,sum; while(~scanf("%d", &n)) {if(!n) Break; k=Log(4*1.0); for(intI=1960; i<=n;i+=Ten) k*=2; sum=0;intCnt=1; while(sum<k) {++cnt; sum+=Log((Double) (CNT)); }printf("%d\n", cnt-1); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 2661-factstone Benchmark (Application of log ())