题目
题目链接:http://codeforces.com/contest/689/problem/C
题目来源:Codeforces#361
题解
m 内等比数列的个数要等于输入的
n 且最小。由于个数是单调的,可以二分答案,计算等比数列的个数。
计算的时候枚举公比 q ,然后
n/q3 就是个数(整数除)。由于 n−−√3 的规模不大,所以时间足够,上界没仔细算,不过 8×1015 肯定可以,用 2 就够了。
二分出的答案计算等比数列个数后不等于
n 就是 −1 。
代码
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
// head
const int N = 3e5 + 5;
LL a[N];
LL ck(LL x) {
LL sum = 0;
for (int i = 2; a[i] <= x; i++) {
sum += x / a[i];
}
return sum;
}
int main() {
for (int i = 2; i < N; i++) {
a[i] = (LL)i * i * i;
}
LL n;
while (scanf("%I64d", &n) == 1) {
LL l = 0, r = a[N-1], ans;
while (l <= r) {
LL mid = (l + r) / 2;
if (ck(mid) < n) {
l = mid + 1;
} else {
ans = mid;
r = mid - 1;
}
}
printf("%I64d\n", ck(ans) == n ? ans : -1);
}
return 0;
}