建议降绿。
题目大意:
给定一个
n
n
n,你需要求出最小的
t
t
t,使得在
t
t
t 里有
n
n
n 组四项的等比数列成立。若没有,则输出 -1
。
解析:
本题可以使用二分去做。
我们可以将
l
l
l 设为
1
1
1,
r
r
r 设为
2
63
−
1
2^{63}-1
263−1,即 C++ 中的 LONG_LONG_MAX
(需开 long long
)。
我们可以定义一个 check
函数,采用枚举公比法用来判断有没有。
下面给出 check
函数:
ll check(ll x){
ll ret=0;
for(ll i=2;i*i*i<=x;i++){
ret+=x/(i*i*i);
}
return ret;
}
如果 check
函数中的
r
e
t
ret
ret 等于输入的
n
n
n,输出
t
t
t。
否则输出 -1
。
注意点:
本题必须全程开 long long
,否则直接炸精度。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; // 全程开 long long
ll check(ll x){
ll ret=0;
for(ll i=2;i*i*i<=x;i++){
ret+=x/(i*i*i);
}
return ret;
}
int main(){
ios_base::sync_with_stdio(false); // cin、scanf 断开连接
cin.tie(NULL),cout.tie(NULL); // 可以稍微快一点
ll n;
cin>>n;
ll l=1,r=LONG_LONG_MAX,mid; // r 取正无穷
ll ans=0; // 题目中的 t
while(l<=r){ // 二分
mid=(l+r)/2;
if(check(mid)>=n) ans=mid,r=mid-1;
else l=mid+1;
}
if(check(ans)==n) cout<<ans;
else cout<<-1;
return 0;
}