Interesting Set 记个题目233

探讨了一种有趣的算法挑战,即寻找与给定整数二进制表示中0和1数量相同的较大整数。通过一种形象的“毛毛虫”移动方法,实现了对所有可能组合的有效遍历。

链接: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代码呢?不存在的
这里写图片描述

7-15 Words Fascinating 分数 35 作者 罗中天 单位 中国计量大学 As we all know the fact, you can use several Source Words to compose an Interesting Word. Little Gyro has already learnt this fantastic idea from one of his best friends Brother Yu. On Little Gryo's birthday, fortunately, Little Gryo received a gift from Brother Yu. The gift consists of n Interesting Words S 1 ​ ,S 2 ​ ,...,S n ​ . Little Gyro also thought those words were really fantastic. So he decided to play with these words. For each Interesting Word S i ​ , Little Gyro will select a period of consecutive letters P i ​ , and splice into a new word T within the given order, and then he defined these kind of words "Fascinating Words". It means you can take apart a Fascinating Word T and form n period of consecutive letters P 1 ​ +P 2 ​ +...+P n ​ from the given Interesting Words. Specially, if Little Gyro considers the Interesting Word really useless, he'll not choose any letter from this Interesting Word either. For example, supposed that there are some Interesting Words: S 1 ​ ="telephone", S 2 ​ ="microscope". Little Gyro may select a period of consecutive letters from each given word such as: P 1 ​ ="tele", P 2 ​ ="scope". And then form the Fascinating Word T=P 1 ​ +P 2 ​ ="telescope". Specially, Little Gyro also can only select P 2 ​ ="scope" and form the Fascinating Word T=P 1 ​ +P 2 ​ ="scope", if he dislikes the first Interesting Word. Now given all the Interesting Words that Little Gyro has received on his birthday, Little Gyro wants to know the total number of the different Fascinating Words that he can generate. Input Specification: Each input file only contains one test case. The first line contains an integer n (1 ≤ n ≤ 2000), indicating the number of the Interesting Words. Then, the following n lines, each line contains an Interesting Word S i ​ (1 ≤ ∑ i=1 n ​ ∣S i ​ ∣ ≤ 5×10 4 ). It's guaranteed that the Interesting Words can only be made up by the lowercase letters, and the sum of the length of the Interesting Words S i ​ of all test cases will not exceed 5×10 4 . Output Specification: For each test case, output the number of the different Fascinating Words that Little Gyro can generate. Because the number may be very large, just output the number mod 10 9 +7. Sample Input 1: 2 aa ab Sample Output 1: 8 Sample Input 2: 3 abb aca aba Sample Output 2: 139 Hint: For the first sample, Little Gyro can generate 8 different Fascinating Words in total, including "a", "b", "aa", "ab", "aaa", "aab", "aaab" and the empty word. 代码长度限制 16 KB Java (javac) 时间限制 2000 ms 内存限制 64 MB 其他编译器 时间限制 1000 ms 内存限制 64 MB 栈限制 8192 KB
10-15
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值