三分
注意区间右端点的取值,太大了会爆精度。
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
using ld=long double;
using ll=long long;
const ll INF=4e18;
const ld one=1.0;
ll n,m,k;
ll ksm(ll x,ll y)
{
ll ans=1;
for(;y>0;y>>=1)
{
if(y&1)
ans*=x;
x*=x;
}
return ans;
}
ll cal(ll x)
{
return llabs(n-ksm(x,k));
}
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0);
ll ans,idx,l,r,fl,fr,fm;
int t;
cin>>t;
while(t--)
{
cin>>n>>k;
if(k==1)
{
cout<<n<<'\n';
continue;
}
l=1;
r=(ll)powl(INF,one/k);
while(r-l>2)
{
fm=(r-l)/3;
fl=l+fm;
fr=fl+fm;
if(cal(fl)<cal(fr))
r=fr;
else
l=fl;
}
ans=INF;
idx=max(1ll,l-2);
for(m=idx;m<=r;++m)
if(cal(m)<ans)
{
ans=cal(m);
idx=m;
}
cout<<idx<<'\n';
}
return 0;
}

京公网安备 11010502036488号