题意:给出生日n,向多层蛋糕插蜡烛,每层插k^i(i=1,2,3....),求r层蛋糕使k*r最小,若多组k*r相同取r最小的,最上面一层可以选择插1根蜡烛。
思路:枚举+二分
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3665
View Code
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstdlib> 5 #include <cstring> 6 #include <string> 7 using namespace std; 8 #define LL long long 9 #define inf 1000000000000 10 11 LL num[10005][70]; 12 LL n,R,K; 13 14 void Init(){ 15 for(LL i=1;i<=10000;i++){ 16 LL p=i; 17 for(LL j=1;j<=64;j++){ 18 num[i][j]=num[i][j-1]+p; 19 p*=i; 20 if(p>1000000000000LL) break; 21 } 22 } 23 } 24 25 int main(){ 26 27 // freopen("data.in","r",stdin); 28 // freopen("data.out","w",stdout); 29 30 Init(); 31 while(scanf("%lld",&n)!=EOF){ 32 LL l=1,r=10000,i; 33 LL sq=sqrt(n); 34 R=1; K=n-1; 35 if(sq*(1+sq)==n||sq*(1+sq)==n-1){R=2; K=sq;} 36 for(i=3;i<=64;i++){ 37 LL l=2,r=10000; 38 while(l<=r){ 39 LL mid=(l+r)>>1; 40 if(num[mid][i]==n||num[mid][i]==n-1){ 41 R=i; 42 K=mid; 43 } 44 if(num[mid][i]>n||num[mid][i]==0) r=mid-1; 45 else l=mid+1; 46 } 47 } 48 printf("%lld %lld\n",R,K); 49 } 50 return 0; 51 }