HDU 4430


Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5505    Accepted Submission(s): 1320


Problem Description
Today is Yukari's n-th birthday. Ran and Chen hold a celebration party for her. Now comes the most important part, birthday cake! But it's a big challenge for them to place n candles on the top of the cake. As Yukari has lived for such a long long time, though she herself insists that she is a 17-year-old girl.
To make the birthday cake look more beautiful, Ran and Chen decide to place them like r ≥ 1 concentric circles. They place ki candles equidistantly on the i-th circle, where k ≥ 2, 1 ≤ i ≤ r. And it's optional to place at most one candle at the center of the cake. In case that there are a lot of different pairs of r and k satisfying these restrictions, they want to minimize r × k. If there is still a tie, minimize r.
 
Input
There are about 10,000 test cases. Process to the end of file.
Each test consists of only an integer 18 ≤ n ≤ 1012.
 
Output
For each test case, output r and k.
 
Sample Input
18 111 1111
 
Sample Output
1 17 2 10 3 10
 1 #include<cstdio>
 2 #include <cmath>
 3 typedef __int64 LL; 
 4 LL int_pow(const LL x,const int n) 
 5 {
 6     LL ans=1;
 7     for(int i=1;i<=n;i++) ans*=x;
 8     return ans;
 9 }
10 LL find_k(int r,LL n)
11 {
12     LL st=2,ed=pow(n,1.0/r),mid,sum; //注意上界取到n的r次方就够了,多了会溢出 
13     while(st<=ed)
14     {
15         mid=st+(ed-st)/2;
16         sum=mid*(1-int_pow(mid,r))/(1-mid); //计算r层蜡烛加起来总共多少根 
17         if(sum > n) ed=mid-1;
18         else if(sum < n) st=mid+1;
19         else return mid;
20     }
21     return 0;
22 }
23 int main()
24 {
25     LL n,r,k;
26     while(scanf("%lld",&n)!=EOF)
27     {
28         r=1,k=n-1;
29         for(int i=2;i<=50;i++) //对于接下来的每层,尝试是否能更新r、k
30         {
31             LL k1=find_k(i,n) , k2=find_k(i,n-1); //得到中心不插蜡烛的k,以及中间插一根蜡烛的k 
32             if(k1 != 0){ //若是对i存在有中心不插蜡烛的k,尝试更新r,k 
33                 if(i * k1 == r * k && i < r) {
34                     r = i; k = k1;
35                 }
36                 else if(i * k1 < r * k){
37                     r = i; k = k1;
38                 }
39             }
40             if(k2 != 0){ //若是对i存在有中心插蜡烛的k,尝试更新r,k 
41                 if(i * k2 == r * k && i < r) {
42                     r = i; k = k2;
43                 }
44                 else if(i * k2 < r * k){
45                     r = i; k = k2;
46                 }
47             }
48         }
49         printf("%I64d %I64d
",r,k);
50     }
51 }


原文地址:https://www.cnblogs.com/dilthey/p/6804168.html