Interesting Set 記個題目233

鏈接:https://www.nowcoder.com/acm/contest/89/I
來源:牛客網

題目描述
Mr.Frog is researching integers.

He converts two integers X and Y into binary system without leading zeros, if they have the same quantity of 0 and the same quantity of 1 in the binary system, Mr.Frog will think these two integers are friendly.

For example, 5(10) = 101(2) and 6(10) = 110(2), both of them have two 1 and one 0 in the binary system, so Mr.Frog thinks 5 and 6 are friendly.

Now, Mr.frog gets an integer N and defines a sorted set S. The sorted set S consists of integers which are friendly with N and bigger than N. He wants to know what is the Kth integer in S.

Because Mr.Frog is on his way back from Hong Kong to Beijing, he wants to ask you for help.
輸入描述:

The first line contains an integer T, where T is the number of test cases. T test cases follow.
For each test case, the only line contains two integers N and K.

• 1 ≤ T ≤ 200.
• 1 ≤ N ≤ 1018.
• 1 ≤ K ≤ 1018

輸出描述:

For each test case, print one line containing “Case #x: y”, where x is the test case number (startingfrom 1) and y is the Kth integer in the sorted set S. If the integer he wants to know does not exist, y is“ IMPOSSIBLE”.

示例1
輸入

2
9 2
7 4

輸出

Case #1: 12
Case #2: IMPOSSIBLE

備註:

For the first case, the sorted set S is {10, 12}, so the 2nd integer is 12.
For the second case, the sorted set S is ∅, so the answer is “IMPOSSIBLE”.

昨天參加了浙財的校賽,感覺這題挺有意思的。
題目大概是說要將一個數轉爲二進制,然後看一下有幾個0和1,再找一個下一個比他大的二進制數,要求0和1的數量要和前者相同。
這個題我在想的時候突然想笑出來,因爲聯想到了毛毛蟲哈哈哈。一串二進制數字比如說10011000111,怎麼去找比他大的下一個數呢?因爲有要求0和1的個數要相同,就可以向前移動他的1來得到符合要求的數,但如果一直抓住這串數中最後的一個1向前移動,是無法得到所有的組合的,然後我就想到了一種很好玩的移動方法。
每次移動的時候,把串中最後一串1作爲一個毛毛蟲,毛毛蟲的頭開始向前移動,然後是下一節的1移動,只要每次選擇最後一串1的第一個1向前移動一格就行了。(一個1也要認爲是一串1),這樣就可以得到升序的所有符合要求的排列了。

10011000111
10011001011
10011001101
10011001110
10011010110
10011011010
10011011100
10011101100
10011110100
10011111000
算了不爬了,毛毛蟲累了,這邊可以算出總共可以爬幾次,只要數一下每個0後面有幾個1,將每個0後面的1的個數加起來就是總共可以爬的次數了。
AC代碼呢?不存在的
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章