hdu 4430

二分

#include
#include
#define INF 1000000005
using namespace std;
long long C(long long d,long long i,long long n){
long long sum=0;
long long ans=1;
for(int j=1;j<=i;j++){
if(n/ans<d)
return -1;
ans*=d;
sum+=ans;
if(sum>n)
{
    return -1;}
}
if(sum<n)
{
    return 0;}
else
{
    return sum;}
}
int main(){
long long n;
while(cin>>n){
    long long ii=1,jj=n-1;
for(int i=2;i<=60;i++){
long long lb=2;
long long ub=n;
long long mid;
int flag=0;
while(ub>=lb){
mid=(lb+ub)/2;
long long temp=C(mid,i,n);
if(temp==n)
{
    flag=1;break;}
if(temp==-1){ub=mid-1;}
else lb=mid+1;
}
if(flag)
{
    if(ii*jj>=mid*i){
        if(ii*jj==mid*i&&ii>i)
           {ii=i;jj=mid;}
        else if(ii*jj!=mid*i)
         {ii=i;jj=mid;}
    }
}
lb=2;ub=n;flag=0;
while(ub>=lb){
mid=(lb+ub)/2;
long long temp=C(mid,i,n-1);
if(temp==n-1){
flag=1;break;
}
if(temp==-1)ub=mid-1;
else  lb=mid+1;
}
if(flag)
{
    if(ii*jj>=mid*i){
        if(ii*jj==mid*i&&ii>i)
          {ii=i;jj=mid;}
        else if(ii*jj!=mid*i)
         {ii=i;jj=mid;}
    }
}
}
cout<<ii<<" "<<jj<<endl;
}
return 0;
}


你可能感兴趣的:(二分搜索,二分搜索)