1192: [HNOI2006] The money bag of the ghost millet Time limit:10 Sec Memory limit:162 MB
submit:2149 solved:1567
[Submit] [Status] [Discuss] Description
Ghost Millet is very clever, because of this, he is very busy, often have the Commissioner of the car's special agent came to him to consult politics. One day, when he traveled in Xianyang, his friend told him that the largest auction house in Xianyang (Ju Bao) is going to hold an auction, in which one of the treasures aroused his great interest, that is no word heavenly book. However, his itinerary is very full, he has bought a long-distance coach to Handan, unfortunately, the departure time is at the end of the auction. So, he decided to prepare in advance, the number of his own gold and a small bag good, so that in his existing gold coins under the ability to pay, any number of gold coins he can use these closed a combination of good money for the bill. Ghost Valley is also a very frugal person, he tried to make himself in the premise of satisfying the above requirements, the use of the minimum number of bags, and no two money bags have the same number of gold more than 1. Suppose he had m gold coins, could you guess how many money bags he would use, and how many gold coins were in each bag?
Input
Contains an integer that represents the total number of gold coins existing in the ghost millet m. Among them, 1≤m≤1000000000.
Output
Only an integer h, indicating the number of pockets of money
Sample Input3Sample Output2HINTSource[Submit][status][discuss]Analysis at the beginning of this question is not really, violence will not write,, the Internet to find the problem after the. The answer is the number of bits in the binary ...
For Example:
When n=13, the number of money bags is 4,
There are two ways of doing this:
1, 2, 3, 7
1, 2, 4, 6
Obviously, according to the binary system is considered optimal;
For example:
1, 2, 3, 7
when required 1 o'clock, just take: 1
when required 2 o'clock, just take: 2
when required 3 o'clock, just take: 3
when required 4 o'clock, simply take: 1,3
when required 5 o'clock, simply take: 2,3
when required 6 o'clock, just take:
when required 7 o'clock, just take: 7
when required 8 o'clock, simply take: 1,7
when required 9 o'clock, simply take: 2,7
when required 10 o'clock, simply take: 3,7
when required 11 o'clock, simply take: 1,3,7
when required 12 o'clock, simply take: 2,3,7
when required 13 o'clock, just take, 1,2,3,7
1, 2, 4, 6
when required 1 o'clock, just take: 1
when required 2 o'clock, just take: 2
when required 3 o'clock, just take:
when required 4 o'clock, just take: 4
when required 5 o'clock, simply take: 1,4
when required 6 o'clock, simply take: 2,4
when required 7 o'clock, simply take: 1,6
when required 8 o'clock, simply take: 2,6
when required 9 o'clock, simply take: 1,2,6
when required 10 o'clock, simply take: 4,6
when required 11 o'clock, simply take: 1,4,6
when required 12 o'clock, simply take: 2,4,6
when required 13 o'clock, simply take: 1,2,4,6
But I'm not going to prove why binary is the best solution, and no one seems to prove it on the web, but the binary must be able to meet all the numbers.
#include <iostream>#include<cstdio>using namespacestd;intMain () {intm,s=1; scanf ("%d",&m); for(intI=1;; i++) {s*=2; if(s>m) {printf ("%d", i); Break; } } return 0;}
[HNOI2006] The money bag of the ghost millet