hdu 4430 Yukari's Birthday

来源:互联网 发布:工单提交系统源码php 编辑:程序博客网 时间:2024/06/06 02:56

题目地址:

http://acm.hdu.edu.cn/showproblem.php?pid=4430

题目:

Yukari's Birthday

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


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
181111111
 

Sample Output
1 172 103 10


题意:

将n支生日蛋糕的蜡烛像同心圆那样围成一圈一圈的,最里面的那个圈只能是1支蜡烛,其他圈按k^i(i=1~r)的数量增长,问给定n支,求实r*k最小的r,k序列,如果有多个r*k值相同的最小值,进一步求r最小的序列。


题解:

抽象成数学问题就是:有符合以下条件的式子:k^0 + k^1 + k^2 + k^3 +......+k^r=n,求 对任意的r,k使r*k最小。注意下取值范围,k是>=2的 n<=10^12,那么很容易能估算出r最大值在40左右。时间要求也很充足,可以一次穷举r和k 以此求出最小值,这里r顺序枚举,在确定k值时用二分枚举,直接用公式求k不是很好求,并且要考虑浮点数误差的问题,这点很容易导致无限的WA中。


代码:

/*hdu :Yukari's Birthday*/#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<iostream>#include<string>using namespace std;__int64 R=0,K=0,Value=0;//R 's max is about 40__int64 MinR=0,MinK=0,MinValue=0;__int64 n=0;/*init the var*/int InitVar(){MinR=1;MinK=n-1;MinValue=n-1;return(0);}/*for test*/int test(){return(0);}/*main process*/int MainProc(){while(scanf("%I64d",&n)!=EOF){InitVar();for(R=2;R<=40;R++){__int64 KLow=2;__int64 KHigh=(__int64)pow((double)n,1.0/(double)R);while(KLow<=KHigh){__int64 KMid=(KLow+KHigh)/2;//__int64 N=(__int64)((pow((double)KMid,(double)(R+1))-1)/(double)(KMid-1)+0.0001);__int64 N=0;int i=0;__int64 RR=1;for(i=0;i<R;i++){RR*=KMid;N+=RR;}if(N==n||N==n-1){K=KMid;Value=K*R;break;}else if(N>n)//K is too big{KHigh=KMid-1;}else{KLow=KMid+1;}}if(Value<=MinValue&&KLow<=KHigh){if(Value<MinValue){MinR=R;MinK=K;MinValue=Value;}else{if(R<MinR){MinR=R;MinK=K;MinValue=Value;}}}}printf("%I64d %I64d\n",MinR,MinK);}return(0);}int main(){MainProc();return(0);}







原创粉丝点击