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

题目

题目链接:http://codeforces.com/contest/689/problem/C

题目来源:Codeforces#361

题解

m 内等比数列的个数要等于输入的n且最小。

由于个数是单调的,可以二分答案,计算等比数列的个数。

计算的时候枚举公比 q ,然后n/q3就是个数(整数除)。

由于 n3 的规模不大,所以时间足够,上界没仔细算,不过 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值