CodeForces 689C Mike and Chocolate Thieves (二分)

原题:

Description

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 fourthieves 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 suchn.

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.

Sample Input

Input
1
Output
8
Input
8
Output
54
Input
10
Output
-1

Hint

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.

提示: 代码中fun(x)函数表示 在n=x时,返回可以构成的最大组合种数。(n=T*k^3,对于指定的一个k ,T可取1~T个)。

  所以即寻找一个最小的x,满足fun(x) == m 。

  fun(x)单调增,所以用二分查找,复杂度满足题目要求。

  二分时注意 1. while(l<=r)       要用小于等于号,缺等号wrong answe。(二分查找法的基本要求)  2.types == m_types 时,r=mid-1。(以便寻找最小的n,当多个连续数相等时,取最左侧的)。

  还有一个地方,fun(10e14) = 10e15     可知,当m取极端情况10e15,对应最大的n为10e14 ,所以定义r变量一定要至少定义成10e15,(long long 数据范围10e18)。

代码:

#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

#define  MAX(x,y) (((x)>(y)) ? (x) : (y))
#define  MIN(x,y) (((x) < (y)) ? (x) : (y))
#define ABS(x) ((x)>0?(x):-(x))
#define ll long long
const int inf = 0x7fffffff;
const int maxn=1e15+10;

ll fun(ll x)
{
    ll sum=0;
    for(ll i=2; i*i*i<=x; i++){
        sum += x/(i*i*i);
    }
    return sum;
}

int main()
{
    ll l=1,r=10e15,mid,ans=-1;//fun(10e14) == 10e15;  所以最大的n可能取值为10e15 (极端有10e15种方法的时候)
    ll m_types;
    cin>>m_types;
    while(l<=r){  //l=minimum n,  r=maximum n ;  =要加  方便找到最小的那个数
        mid=l+(r-l)/2;
        ll types=fun(mid);
        if(types < m_types)
            l=mid+1;
        else if(types > m_types)
                r=mid-1;
            else{   //相等也要取左半部分,因为要找最小的n(找最大的也有方法)
                ans=mid;
                r=mid-1;
            }
    }
    cout<<ans<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/shawn-ji/p/5661482.html