链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=121473#problem/C
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
1
8
8
54
10
-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\n",f); 40 } 41 else 42 printf("-1\n"); 43 return 0; 44 }