Fat brother and Maze are playing a kind of special (hentai) game with a piece of gold of length N where N is an integer. Although, Fat Brother and Maze can buy almost everything in this world (except love) with this gold, but they still think that is not convenient enough. Just like if they would like to buy the moon with M lengths of the gold, the man who sold the moon need to pay back Fat Brother and Maze N-M lengths of the gold, they hope that they could buy everything they can afford without any change. So they decide to cut this gold into pieces. Now Fat Brother and Maze would like to know the number of the pieces they need to cut in order to make them fulfill the requirement. The number of the gold pieces should be as small as possible. The length of each piece of the gold should be an integer after cutting.
Input
The first line of the data is an integer T (1 <= T <= 100), which is the number of the text cases.
Then T cases follow, each case contains an integer N (1 <= N <= 10^9) indicated the length of the gold.
For each case, output the case number first, and then output the number of the gold pieces they need to cut.
1 3Sample Output
Case 1: 2Hint
In the first case, the gold can be cut into 2 pieces with length 1 and 2 in order to buy everything they can afford without change.
题意:求一个数的划分,使得1到n内的数都可以有划分里的若干个数字表示思路:和之前做过的人民币兑换问题一样,将整的人民币换成零钱,所有面值的人民币都可以有这些零钱构成1不能由其他构成,应该包含1;2不能由其他构成,应该包含2;3可以由1,2构成,不必包含;
4不能由其他构成,应该包含4;5可以由1,4构成,不必包含;6可以由2,4构成不必包含.............
规律:划分应该是2^0 , 2^1 , 2^2 , 2^3.......
代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int q_pow(int a,int b){
int ans=1;
while(b){
if(b&1) ans=ans*a;
a*=a;
b/=2;
}
return ans;
}
int main(){
int T,Cas=0;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
int x=(int)log2(n+1);
printf("Case %d: ",++Cas);
if(q_pow(2,x)==n+1) printf("%d\n",x);
else printf("%d\n",x+1);
}
return 0;
}