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;
}
时间: 2024-10-14 20:44:03

CodeForces 689C Mike and Chocolate Thieves (二分)的相关文章

CodeForces 689C Mike and Chocolate Thieves (二分最大化最小值)

题目并不难,就是比赛的时候没敢去二分,也算是一个告诫,应该敢于思考…… #include<stdio.h> #include<iostream> using namespace std; int main() { long long n; scanf("%I64d",&n); long long left=1,right=1e18,mid,num,m,s; long long ans=-1; while(left<=right) { mid=(le

Codeforces 689C - Mike and Chocolate Thieves

题意:有四个小偷要偷巧克力,每个小偷偷的巧克力是前一个小偷的k倍.例:a,ak,ak^2,ak^3.然后给出你小偷头巧克力的方案数,问:符合条件的条件下,背包中巧克力量最大值最小是多少. 分析:最大值最小.所以二分n. 1 /************************************************ 2 Author :DarkTong 3 Created Time :2016/7/7 17:06:12 4 File Name :C.cpp 5 **************

CodeForces 689C  Mike and Chocolate Thieves

题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=412145 题目大意:给定一个数字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

Codeforces Round #256 (Div. 2)D 二分答案

D. Multiplication Table time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Bizon the Champion isn't just charming, he also is very smart. While some of us were learning the multiplication tabl

CodeForces 689C(二分查找)

题意:有四个贼偷糖,后面一个贼偷糖的数量是前一个贼的k倍,现在给你一个方案数m,求满足m种偷取方案数的最小总糖数n. 题意:假设有n颗糖,偷取的方案数为( n/(2^3)+n/(3^3)+....+n(k^3) )种(k^3<=n),二分查找最小的n. #include <iostream> using namespace std; long long solve(long long x) { long long ans=0; for(long long k=2;;k++) { if(x

Codeforces 798D Mike and distribution - 贪心

Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, 

Codeforces 778A:String Game(二分暴力)

http://codeforces.com/problemset/problem/778/A 题意:给出字符串s和字符串p,还有n个位置,每一个位置代表删除s串中的第i个字符,问最多可以删除多少个字符使得s串依旧包含p串. 思路:想到二分,以为二分做法依旧很暴力.但是别人的做法确实就是二分暴力搞啊. 枚举删除字符数,然后判断的时候如果s串包含p串,那么可以往右区间找,否则左区间找. 1 #include <bits/stdc++.h> 2 using namespace std; 3 #def

Codeforces 729C Road to Cinema(二分)

题目链接 http://codeforces.com/problemset/problem/729/C 题意:n个价格c[i],油量v[i]的汽车,求最便宜的一辆使得能在t时间内到达s,路途中有k个位置在g[i]的加油站,可以免费加满油,且不耗时间.每辆车有两种运行模式可以随时切换:1.每米一分钟两升油:2.每米两分钟一升油. 看到10^5次加上循环两次就想到二分或者线段树或者看错题意了. 这题二分查找一下汽油就可以了,找到最少多少汽油够到达,然后再for一遍找汽油量大的且价格便宜的车即可. 还

Codeforces 484B Maximum Value(排序+二分)

题目链接: http://codeforces.com/problemset/problem/484/B 题意: 求a[i]%a[j] (a[i]>a[j])的余数的最大值 分析: 要求余数的最大值非常明显a[i]越接近a[j]的倍数则余数越大 ,因此我们将全部的元素从大到小排序 : 然后枚举a[j]的倍数 ,二分查找小于a[i]倍数的最大值,然后更新余数的最大值. 代码例如以下: #include <iostream> #include <cstdio> #include