2909. [ACM][2018新生赛]

GNU C 1000 MS,32768 KB
GNU C++ 1000 MS,32768 KB
Java 2000 MS,262144 KB
Python 2 1000 MS,65536 KB
Python 3 1000 MS,65536 KB

Brother Ming likes to chew gum while playing games.
“Chewing gum is good for concentration, for achieving high levels of concentration quickly, for relieving stress and so on.”
"Brother Ming said to me as he chewed his gum and looked at the lovely black and white picture.
It is known that brother Ming has n boxes of chewing gum, and the number of chewing gum in the boxes is 1-n.
Each day, he chooses the number m and eats m pieces of gum in all the boxes with the number greater than or equal to m.
So how soon can Ming eat all the gum in the box?


Input
Brother Ming likes to chew gum while playing games.
There are multiple sets of input data
Enter a T in the first row to indicate that there is a T set of data next
The second row contains an integer n (1 <= n <= 231), representing the number of boxes.

Output
Each output takes two lines.
The first line outputs Case # I:, where I represents the set of samples
The second line outputs the number of days Ming ate all the gum

Examples

Input
4
1
2
3
4

Output
Case #1:
1
Case #2:
2
Case #3:
2
Case #4:
3

思路
这是一道水题
由于这n个盒子中有1-n个口香糖
因此,我们只要确保所有盒子中,口香糖数目最多的为0就好
我们假设t为每一次取完m个口香糖后所有盒子中最多的口香糖数
倘若m = 1或者m = t,不难想象,次数就是n
所以我们应尽量往两边取值
当n为奇数时,我们可以直接取m = (t + 1) / 2
则剩下为t = t - (t + 1) / 2 = (t - 1) / 2 = t // 2
当n为偶数时,我们可以直接取m = t / 2
则剩下为t = t - t / 2 = t / 2
而我们已知
当t = 1时,次数为1
当t = 2时,次数为2
当t = 3时,次数为2
而任何大于3的整数在除2的过程中肯定会变成2或3
因此,我们可以以2和3为循环终点

程序演示

#include 

int main(int argc, char *argv[]) {
	int T,a;
	while(~scanf("%d",&T)){
		for(int i=1; i<=T; i++){
			int times = 2,b = 1;
			scanf("%d",&a);
			if(a == 1){
				times = 1;
				b = 0;
			}
			while((a-2) && (a-3) && b){
				times += 1;
				a /= 2;
			}
			printf("Case #%d:\n%d\n",i,times);
		}
	}
}

你可能感兴趣的:(2909. [ACM][2018新生赛])