题意:
给定一个n,计算满足x^1+x^2+……+x^r=n且使x*r最小(如果有多个,取r最小的那个)的x,r值。
由于2^40大于1e12,所以我们可以枚举r从1到40,但这样可能会超时,所以可以根据3^26大于2e12,我们可以枚举r从1到26,并单独计算x=2的情况。
对于给定的每个r,x^1+x^2+……+x^r这个式子就关于x单调递增,所以就可以用二分来做。
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#define msc(X) memset(X,-1,sizeof(X))
#define ms(X) memset(X,0,sizeof(X))
typedef long long LL;
using namespace std;
LL res,p2;
int p1;
void cal(LL n)
{
if(((n+2)&(n+1))==0){
int k=1;
for(int i=2;i<=40;i++)
if(!(n&(1ll<<i)))
{
k=i-1;
break;
}
if(res>=(k<<1)) {
res=(k<<1);
p1=k;
p2=2;
}
}
for(int i=1;i<27;i++)
{
LL l=(LL)pow(2.0*n,1.0/(i+1.0)),
r=(LL)pow((double)n,1.0/i)+5ll;
while(l<r){
LL m=(l+r)>>1;
LL tmp=m;
for(int j=1;j<i;j++)
tmp=tmp*m+m;
if(tmp<n) l=m+1;
else if(tmp>n) r=m;
else{
if(res>m*i){
res=m*i;
p1=i;
p2=m;
}
break;
}
}
}
}
int main(int argc, char const *argv[])
{
LL n;
while(scanf("%I64d",&n)!=EOF){
res=1ll<<40;
cal(n);
cal(n-1);
printf("%d %I64d\n",p1,p2 );
}
return 0;
}