【打CF,学算法——三星级】CodeForces 689C Mike and Chocolate Thieves (二分)

来源:互联网 发布:协方差矩阵主对角线 编辑:程序博客网 时间:2024/05/16 12:06

【CF简介】

提交链接:CF 689C


题面:

C. Mike and Chocolate Thieves
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 exactlyk times more than the previous one. The value ofk (k > 1) is a secret integer known only to them. It is also known that each thief's bag can carry at mostn chocolates (if they intend to take more, the deal is cancelled) and that there wereexactly 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 fixedn, but not fixedk) ism. 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 ofn will help him in that. Please findthe 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 onen satisfying the rumors,print the smallest one.

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

Examples
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 isn = 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 exactly8 ways isn = 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.


题意:

    给定一个数字n,问能不能求得一个最小的整数x,使得在1-x范围内,可以取的n组T*k^3<=x。T,k为任意整数,需要在小于等于x的情况下,凑够n对T,k,使其满足T*k^3<=x。若x存在,则输出x,若x不存在,则输出-1。


解题:

     解题的大致流程是这样的。

     先看k=2,(1,2,4,8)/(2,4,8,16)/(3,6,12,24)....

            k=3,(1,3,9,27)/(2,6,18,54)......

            对应某个k,在x范围内,其能取到的最大组数为x/(k^3),因为合法组数具有单调性,可以采用二分的方法求解。

     两种情况很好理解,当当前数量已大于要求数量,那便移动右边界,当当前数量小于要求数量,则移动左边界,但相等时,并不一定就是解。因为,题目要求是合法时最小的整数解,故而继续移动右边界。


代码:

#include <iostream>#include <vector>#include <cstdio>#include <cstring>#include <cmath>#define LL long longusing namespace std;LL time3[1000001];int main(){bool flag=0;LL le=1,ri=1e18,mid,lim,cnt,m,ans;    for(int i=0;i<1000001;i++)    {time3[i]=1;time3[i]=1LL*i*i*i;}cin>>m;    while(le<=ri){      mid=(le+ri)>>1;      lim=pow(1.0*mid,1.0/3)+1;  cnt=0;  for(int i=2;i<=lim;i++)  {        cnt+=mid/time3[i];  }  if(cnt>m)  ri=mid-1;  else if(cnt<m)  le=mid+1;  else  {  flag=1;  ans=mid;  ri=mid-1;  }}if(flag)cout<<ans<<endl;elsecout<<"-1\n";return 0;}


0 0