Mike and Chocolate Thieves【Codeforces 689 C】【二分答案】

Codeforces Round #361 (Div. 2) C


  题意:题目中给你一个M,是问你的是,如果我们现在方案数为M,请问小偷的背包最少应该容量为多大,求这样的N。那为什么可能存在-1呢?是因为我们不可能存在这样的方案数M。

  思路:我们可以去二分答案这样的背包上限,然后我们去询问它对所有的i^{3}的倍数关系,也就是答案的数量了。可千万要把i开的大一点,差不多要到5e5左右,一开始只开了1e5是显然不够的。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
ll trid[1000007], M;
inline ll check(ll x)
{
    ll sum = 0;
    for(int i=2; i<=1000001; i++)
    {
        sum += x / trid[i];
        if(x < trid[i]) break;
    }
    return sum;
}
int main()
{
    for(ll i=1LL; i<=1000001LL; i++) trid[i] = 1LL * i * i * i;
    scanf("%lld", &M);
    ll l = 1, r = 1e16, mid, ans = 0, cnt;
    while(l <= r)
    {
        mid = (l + r)>>1;
        cnt = check(mid);
        if(cnt >= M) { r = mid - 1LL; ans = mid; }
        else { l = mid + 1LL; }
    }
    if(check(ans) == M) printf("%lld\n", ans);
    else printf("-1\n");
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值