lightoj1042

maksyuki 发表于 oj 分类,标签:
0

1042 - Secret Origins

But we do know the story of the first time she set out to chart her own path in the time stream. Zephyr had just finished building her time machine which she named - "Dokhina Batash". She was making the final adjustments for her first trip when she noticed that a vital program was not working correctly. The program was supposed to take a number N, and find what Zephyr called its Onoroy value.This is the tale of Zephyr, the greatest time traveler the world will never know. Even those who are aware of Zephyr's existence know very little about her. For example, no one has any clue as to which time period she is originally from.

The Onoroy value of an integer N is the number of ones in its binary representation. For example, the number 13 (11012) has an Onoroy value of 3. Needless to say, this was an easy problem for the great mind of Zephyr. She solved it quickly, and was on her way.

You are now given a similar task. Find the first number after N which has the same Onoroy value as N.

Input

Input starts with an integer T (≤ 65), denoting the number of test cases.

Each case begins with an integer N (1 ≤ N ≤ 109).

Output

For each case of input you have to print the case number and the desired result.

Sample Input

Output for Sample Input

5231423239178 Case 1: 27Case 2: 14241Case 3: 395Case 4: 11Case 5: 16

 

题目类型:位运算

算法分析:如果直接枚举比n大的数并判断二进制中1的个数是否相同会TLE,此时使用的方法是将n的二进制表示存储到数组中,然后依次求长度为i的二进制位中的下一个排列,如果长度为i的二进制位存在下一个排列,则直接将该数转化为十进制输出即可。如果没有下一个合理的排列,则尝试下一个长度的二进制位的排列。由于尝试的二进制的排列的长度不超过2次,所以时间复杂度几乎为O(n)的,其中n为原十进制数的二进制表示的长度,而由已知可得n不超过32,所以不会TLE