Mike and Chocolate Thieves CodeForces - 689C (二分思想)

Mike and Chocolate Thieves

CodeForces - 689C

Bad news came to Mike's village, some thieves stole a bunch of chocolates from the local factory! Horrible!

Aside from loving sweet things, thieves from this area are known to be very greedy. So after a thief takes his number of chocolates for himself, the next thief will take exactly k times more than the previous one. The value of k (k > 1) is a secret integer known only to them. It is also known that each thief's bag can carry at most n chocolates (if they intend to take more, the deal is cancelled) and that there were exactly four thieves involved.

Sadly, only the thieves know the value of n, but rumours say that the numbers of ways they could have taken the chocolates (for a fixed n, but not fixed k) is m. Two ways are considered different if one of the thieves (they should be numbered in the order they take chocolates) took different number of chocolates in them.

Mike want to track the thieves down, so he wants to know what their bags are and value of n will help him in that. Please find the smallest possible value of n or tell him that the rumors are false and there is no such n.

Input

The single line of input contains the integer m (1 ≤ m ≤ 1015) — the number of ways the thieves might steal the chocolates, as rumours say.

Output

Print the only integer n — the maximum amount of chocolates that thieves' bags can carry. If there are more than one n satisfying the rumors, print the smallest one.

If there is no such n for a false-rumoured m, print  - 1.

Example
Input
1
Output
8
Input
8
Output
54
Input
10
Output
-1
Note

In the first sample case the smallest n that leads to exactly one way of stealing chocolates is n = 8, whereas the amounts of stealed chocolates are (1, 2, 4, 8) (the number of chocolates stolen by each of the thieves).

In the second sample case the smallest n that leads to exactly 8 ways is n = 54 with the possibilities: (1, 2, 4, 8),  (1, 3, 9, 27),  (2, 4, 8, 16),  (2, 6, 18, 54),  (3, 6, 12, 24),  (4, 8, 16, 32),  (5, 10, 20, 40),  (6, 12, 24, 48).

There is no n leading to exactly 10 ways of stealing chocolates in the third sample case.


题意:四个强盗偷巧克力,四个人分别偷a,  a*k,  a*k*k,  a*k*k*k个,给定m,m是他们四个人偷巧克力的方式,即

四个数的不同的组合数,让我们求他们的背包容量n至少多少

思路:很巧妙的运用二分,首先我们知道这个题目每次以一种方式偷只和三个参数有关,a,k,n,那么如果我们知道了其中两个参数就可以表示出第三个,我们可以利用二分,二分n,然后枚举k,因为枚举k的规模要远小于枚举a的规模,因为由题意,我们知a*k*k*k<=n所以枚举k时的边界只需要k^3<=n即可,然后n/k^3就到了k在当前值下,a的可能的个数,即a的值可以取

1 ~ n/k^3的任意整数都满足条件,也就是说a的种类数就代表了组合数,然后枚举所有k的满足条件的情况,把a的所有可能加起来就得到了总的组合数,和给定的m作比较,如果得到的总数sum>=m,说明n取得太大导致得到的a的可能个数多,所以选取左边区间继续二分,反之右边。需要特别注意的是如果相等了,只能说明这只是n的一种可能情况,并不是最小,所以要继续二分下去,所以等于这个判断条件要单独判断,保存答案(具体意思看代码)。

code:

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;

ll check(ll n){
    ll res = 0,k;
    for(k = 2; k*k*k <= n; k++){
        res += n/(k*k*k);
    }
    return res;
}
int main(){
    ll m,res = -1;
    ll l = 1,r = 1e18;
    scanf("%lld",&m);
    while(l <= r){
        ll mid = (l+r)/2;
        ll num = check(mid);
        if(num == m)//单独判断
            res = mid;
      
       if(num >= m)//注意这里一定是if不能是else if因为找到一个等于m的n后
            r = mid-1;//要继续查找,找最小的n,如果是else if后两句就会一直跳过去
        else
            l = mid+1;
    }
    printf("%lld\n",res);
    return 0;
}


没有更多推荐了,返回首页