zoukankan      html  css  js  c++  java
  • Mike and Chocolate Thieves(二分)

    链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=121473#problem/C

                                                            Mike and Chocolate Thieves
                      Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

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

    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.

    题意:

    有4个小偷,每个小偷偷得巧克力是前一个小偷的k倍,例如第一个小偷是i,则第二个小偷是k*i,第三个小偷k*k*i,第四个小偷是k*k*k*i

    输入一个数,表示四个小偷所偷的有几种情况,在恰好有这么多种情况下会偷到的东西的最大值。

    题解:

    可以知道,无论哪种情况,小偷所能偷到的最大值都是第四个小偷所能偷的数

        对于第四个小偷,有这几种情况

         1*2*2*2     2*2*2*2   3*2*2*2   ........   i*k*k*k<x

         对于设定的最大值x,可以知道,在x/(k*k*k)==n时,取到x的最大值最小

      采用二分法,求数的最大值最小

    源代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 using namespace std;
     5 long long check(long long m)
     6 {
     7     long long sum=0;
     8     for(long long i=2;;++i)
     9     {
    10         if(m<i*i*i)
    11             break;
    12         sum+=m/(i*i*i);
    13     }
    14     return sum;
    15 }
    16 int main()
    17 {
    18     long long n;
    19     scanf("%lld",&n);
    20     long long L=0,R=0x7fffffffffffffff;
    21     long long mid,s;
    22     while(L<R)
    23     {
    24         mid=(L+R+1)/2;
    25         s=check(mid);
    26         if(s==n) break;
    27         else if(s>n) R=mid-1;
    28         else L=mid+1;
    29     }
    30     long long f=0;
    31     if(s==n)
    32     {
    33         for(long long i=2;;++i)
    34         {
    35             if(mid<i*i*i)
    36                 break;
    37             f=max(f,(mid/(i*i*i)*i*i*i));
    38         }
    39         printf("%lld
    ",f);
    40     }
    41     else
    42         printf("-1
    ");
    43     return 0;
    44 }
  • 相关阅读:
    为什么要使用href=”javascript:void(0);”
    切图要点
    css属性学习
    CentOS7使用yum安装RabbitMQ
    Linux下,root权限才能启动1024以下端口的程序
    Gitlab备份和恢复操作记录
    rpm批量卸载所有带有Java的文件
    如何使用微软官方工具制作win10启动盘
    重装系统时,将MBR分区转为GPT 分区
    walle 2.0 上线部署
  • 原文地址:https://www.cnblogs.com/q-c-y/p/5662251.html
Copyright © 2011-2022 走看看