hdu 4430 Yukari's Birthday(二分+枚举)

来源:互联网 发布:淘宝小二会旺旺联系吗 编辑:程序博客网 时间:2024/04/29 22:55

Yukari's Birthday

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


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
 

Source
2012 Asia ChangChun Regional Contest
 

Recommend
zhuyuanchen520

思路:本题数据是10^12用二分就行了,最多枚举不会超过64位,因为2^64>>10^12,因此枚举某个数的次数,看看是否有满足条件的r和k即可。

失误点:中心点可以放也可以不放。

#include<iostream>using namespace std;const long long oo=1000001;///二分查找是否有满足条件的klong long bserch(int x,long long n){  if(x==1)return n;  long long l=2,r=oo,mid,sum,bit;  while(l<=r)  {    mid=(l+r)/2;sum=0;bit=1;    for(int i=0;i<x;i++)    {bit*=mid;sum+=bit;     if(sum>n)     { sum=oo*oo;       break;     }    }    if(sum==n)return mid;    else if(sum>n)r=mid-1;    else l=mid+1;  }  return 0;}int main(){ long long r,k,ar,ak,n;  while(cin>>n)  { ar=ak=oo;    for(int i=1;i<64;i++)    {      k=bserch(i,n);      if(k!=0)      {        r=i;        if(r*k<ar*ak)          ar=r,ak=k;        else if(r*k==ar*ak&&r<ar)          ar=r,ak=k;      }      k=bserch(i,n-1);      if(k!=0)      {        r=i;        if(r*k<ar*ak)          ar=r,ak=k;        else if(r*k==ar*ak&&r<ar)          ar=r,ak=k;      }    }    cout<<ar<<" "<<ak<<"\n";  }}




原创粉丝点击