HDU 4430 Yukari\'s Birthday

Yukari's Birthday

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


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 k i 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 ≤ 10 12.
 

Output
For each test case, output r and k.
 

Sample Input
     
     
     
     
18 111 1111
 

Sample Output
     
     
     
     
1 17 2 10 3 10
 
题目大意就是给出数n 求满足条件的r,k 使得    k^1+k^2+k^3.......k^i(1<=i<=n)=n 或n-1  且在k*r最小.
思路:我们可以估计r最大不超过40   因为2^40>10^12
枚举r  通过r求k的大致范围 二分k
代码如下:

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 62;
const double eps=1e-5;
using namespace std;

bool cmp(int a,int b){
return a>b;
}
ll sum;
bool judge(ll x,ll k,ll n){
sum=0;
ll temp=1;
for(int i=1;i<=x;i++){
temp*=k;
sum+=temp;
if(sum>n)return true;
}
return false;
}
ll check(ll x,ll n){
ll l=2,r=(ll)pow(n,1.0/x);//二分k,枚举k r = (LL)pow(n, 1.0/level);
while(l<=r){
ll mid=(l+r)/2;
if(judge(x,mid,n)){//分大了
r=mid-1;
}
else{
if(sum==n){
return mid;
}
else l=mid+1;
}
}
return 0;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
ll n;
while(scanf("%I64d",&n)!=EOF){
ll ans=inf;
ll a,b;
ll R,K;
for(ll r=1;r<=45;r++){
a=check(r,n);
b=check(r,n-1);
if(a&&a*r<ans)ans=a*r,K=a,R=r;
if(b&&b*r<ans)ans=b*r,K=b,R=r;
}
printf("%I64d %I64d\n",R,K);
}
return 0;
}

你可能感兴趣的:(HDU 4430 Yukari\'s Birthday)