您的位置:首页 > 其它

HDU 4430 Yukari\'s Birthday

2016-04-21 19:03 295 查看

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


[align=left]Problem Description[/align]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.
 
[align=left]Input[/align]There are about 10,000 test cases. Process to the end of file.
Each test consists of only an integer 18 ≤ n ≤ 1012.
 
[align=left]Output[/align]For each test case, output r and k. 
[align=left]Sample Input[/align]
18
111
1111 
[align=left]Sample Output[/align]
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: