HDU 4430 Yukari's Birthday(二分)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4430

题目大意:给定n个蜡烛,围绕蛋糕的中心插同心圆,从里往外分别是第1圈、第2圈、...第r圈,第 i 圈要插k^i 个蜡烛,且r*k最小。求出r和k。注意,中心点可以放,也可以不放

Sample Input
18
111
1111
 
Sample Output
1 17
2 10
3 10

 

代码如下:

 1 # include<cstdio>
 2 # include<cstring>
 3 # include<cmath>
 4 # include<algorithm>
 5 # define LL long long
 6 using namespace std;
 7 
 8 LL pow1(LL a,int b){
 9     LL ans=1;
10     for(int i=0;i<b;i++)
11         ans*=a;
12     return ans;
13 }
14 int main(){
15     LL n,r,k;
16     while(scanf("%I64d",&n)!=EOF){
17         r=1;
18         k=n-1;
19         LL left,right,mid;
20         for(int i=2;i<=41;i++){
21             left=2;
22             right=(long long)pow(n,1.0/i);
23             while(left<=right){
24                 mid=(long long)(left+right)/2;
25                 LL temp=(mid-pow1(mid,i+1))/(1-mid);
26                 if(temp==n||temp==n-1){
27                     if(i*mid<r*k){
28                         r=i;
29                         k=mid;
30                     }
31                     break;
32                 }
33                 else if(temp<n) left=mid+1;
34                 else right=mid-1;
35             }
36         }
37         printf("%I64d %I64d\n",r,k);
38     }
39     return 0;
40 }

 

你可能感兴趣的:(birt)