Google Code Jam 2010 Qualification Round 资格赛 Problem A. Snapper Chain 问题A.按扣链条

Google Code Jam 2010
Qualification Round 资格赛

Problem A. Snapper Chain 问题A.按扣链条

Problem
The Snapper is a clever little device that, on one side, plugs its input plug into an output socket, and, on the other side, exposes an output socket for plugging in a light or other device.
问题
按扣是一个聪明的小设备,一方面把它的输入插头插到一个输出插口上,另一方面,露出一个输出插口可以用来插入一个灯泡或者其他的设备。

When a Snapper is in the ON state and is receiving power from its input plug, then the device connected to its output socket is receiving power as well. When you snap your fingers -- making a clicking sound -- any Snapper receiving power at the time of the snap toggles between the ON and OFF states.
当一个按扣处于ON状态并且从它的输入插头接收到能量的时候,连接到这个按扣的输出插口的设备也会同样的接收到能量。当你敲击你的手指——制造出一个敲击的响声——任何按扣在ON和OFF之间切换状态的时候都会接收到能量。

In hopes of destroying the universe by means of a singularity, I have purchased N Snapper devices and chained them together by plugging the first one into a power socket, the second one into the first one, and so on. The light is plugged into the Nth Snapper.
希望依靠奇点摧毁宇宙,我购买了N个按扣设备并且把它们串联起来,把第一个按扣插到电源接口上,第二个插到第一个上面,以此类推。把灯泡插到第N个按扣上。


Initially, all the Snappers are in the OFF state, so only the first one is receiving power from the socket, and the light is off. I snap my fingers once, which toggles the first Snapper into the ON state and gives power to the second one. I snap my fingers again, which toggles both Snappers and then promptly cuts power off from the second one, leaving it in the ON state, but with no power. I snap my fingers the third time, which toggles the first Snapper again and gives power to the second one. Now both Snappers are in the ON state, and if my light is plugged into the second Snapper it will be on.
最开始的时候,所有的按扣都在OFF状态,所以只有第一个按扣从插头上接受能量,并且灯是熄灭的。我敲了一个手指头,触发了第一个按扣到ON状态,把能量传给了第二个按扣。我再敲击了一次,触发了这两个按扣,然后立即切断了第二个的能量,它停留在ON状态上,但是没有能量。我第三次敲击手指头,触发了第一个按扣,然后能量传递给第二个按扣,现在这两个按扣都在ON状态上,如果我的灯泡插在第二个按扣上,它现在将会被点亮。

I keep doing this for hours. Will the light be on or off after I have snapped my fingers K times? The light is on if and only if it's receiving power from the Snapper it's plugged into.
我一直重复这个动作几个小时。当我敲击手指头K次之后,灯是亮的还是灭的?灯处于亮的状态仅当它从连接它的按扣处接受到能量。

Input 输入
The first line of the input gives the number of test cases, T. T lines follow. Each one contains two integers, N and K.
输入的第一行给出了测试用例的数量,T。下面跟着T行。每一行包含两个整数,N和K。

Output 输出
For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is either "ON" or "OFF", indicating the state of the light bulb.
对于每一个测试用例,输出一行,包含“Case #x: y",x是用例号码,从一开始,y是ON或者OFF,表示灯的亮灭状态。

Limits 限制
1 ≤ T ≤ 10,000.

Small dataset 小数据集
1 ≤ N ≤ 10;
0 ≤ K ≤ 100;

Large dataset 大数据集
1 ≤ N ≤ 30;
0 ≤ K ≤ 108;

Sample 例子

Input Output
  
4    
1 0   Case #1: OFF
1 1   Case #2: ON
4 0   Case #3: OFF
4 47  Case #4: ON

分析:
整个过程如同一个二进制的加1操作,从0x0开始加K次1。而N可以作为预定的位数。
比如一个8位的二进制数,每一位上都有权值的,128,64,32,16,8,4,2,1.
因此存在一个2^(N-1)和K之间的关系。
对于小数据集来说,N=10的时候2^9=512比K的100大多了,而灯亮的时候只有N位上全为1的情况下才会亮。
因此把K作为十进制数,判断低N位,看看是不是都是1,如果是,则灯是亮的,如果不是,则灯是灭的。
进而,问题转换为一个进制转换的问题。
比如K=47,转换为二进制K=0010 1111,而N=4,满足条件,灯是亮的,因此状态为ON。
进一步,不管K=100还是K=108,K都小于128这个数,要把N每一位的权值加起来。
N   K
1   1
2   3
3   7
4   15
5   31
6   63
7   127
8   255
因此,N有效的最大值为6,当N大于6的时候,只要K在限定范围内,都没法形成足够长度的连续的1序列。因此,进一步限制了数据的取值。
D100-->B0110 0100
D108-->B0110 1100

可以定义函数char D2B(K),一个char类型的,按位存取1或者0,8位长度足够了。
然后需要一个函数来知道char里面有多少个低位连续的1。
int GetLen(D2B(K)) 返回一个整数.然后与N比较一下就可以了。
比较char的时候,可以按位比,用与或之类的,从长到短比较1111 1111~ 0000 0001。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值