The 2012 ACM-ICPC Asia Changchun Regional Contest-k
题目大意 就是 求k^0+k^1+……+k^r=n 最小化r*k,且r最小。k^0这项可有可无
k=1时 显然 r=n-1,当k>=2 时 由于n<=10^12 ,所以r估算不会超过40,可以枚举r,然后二分算出k。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
LL n,l,r,mid,ansr,ansk,k,i,ans;
LL calc(LL w){
l=2;r=LL(pow(n,1.0/w)+1);
ans=-1;
while (l<=r){
LL mid=(l+r)/2;
LL value=mid;
for (int i=2;i<=w;++i){
value++;
value*=mid;
if (value>n) break;
}
if (value == n) ans=mid;
if (value<n) l=mid+1;else
r=mid-1;
}
return ans;
}
int main()
{
while (cin>>n){
ansr=1;ansk=n-1;
for (LL i=2;i<=40;i++) {
k=calc(i);
if (k>=2)
if (ansr*ansk>i*k || ansr*ansk==i*k && i<ansr)
ansr= i,ansk=k;
}
--n;
for (LL i=2;i<=40;i++) {
k=calc(i);
if (k>=2)
if (ansr*ansk>i*k || ansr*ansk==i*k && i<ansr)
ansr= i,ansk=k;
}
cout <<ansr<<" "<<ansk<<endl;
}
return 0;
}