您的位置:首页 > 其它

Codeforces #361C. Mike and Chocolate Thieves 二分 数学

2016-07-07 17:50 387 查看

题目

题目链接: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
;

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: