lightoj 1042 Secret Origins(位运算符号的应用)


1042 - Secret Origins
Time Limit: 0.5 second(s)Memory Limit: 32 MB

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.

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.

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

5

23

14232

391

7

8

Case 1: 27

Case 2: 14241

Case 3: 395

Case 4: 11

Case 5: 16




题目大意:对于输入的N,输出比N大的第一个二进制中1的个数相同的数。(copy的加上我的理解)

分析:N最大可以去到1e9,所以可以知道,暴力一个个枚举是行不通的。题目要求输出二进制表示中1的位数相同的首个大于N的数,所以应该不难想到要用位运算去寻找答案。

在《挑战程序设计竞赛》这本书中,在谈到集合的那一部分,有一个关于用位运算表示集合的专栏,里面的介绍就有本题解法的相关知识。

我们可以知道,0101110之后的是0110011,0111110之后的是1001111。那么怎么通过位运算快速得得到我们想要的数呢。

可以通过一下步骤:

(1)求出最低位的1开始的连续的1的区间(0101110->0001110) 

(2)将这一区间全部变为0,并将区间左侧的那个0变为1(0101110->0110000)

(3)将第(1)步里取出的区间右移,直到剩下的1的个数减少了1个(0001110->0000011)

(4)将第(2)步和第(3)步的结果按位取或(0110000|0000011=0110011)
(以上思路就是最一般的方法,有点贪心的思想)
对于为零的整数,N&(-N)的值就是将其最低位的1独立出来后的值。这是由于计算机中负数采用二进制补码表示,-N实际上对应于(~N)+1(将N按位取反然后加上1)。

将最低位的1取出后,设它为x。那么通过计算y=N+x,就将N从最低位的1开始的连续的1都置为0了。我们来比较一下~y和N。在N中加入x后没有变化的位,在~y中全部取相反的值。而最低位1开始的连续区间在~y中依然是1,区间左侧的那个0在~y中也依然是0。于是通过计算z=N&~y就得到了最低位1开始的连续区间。比如如果N=0101110,则x=0000010,y=0110000,z=0001110。

同时,y也恰好是第(2)步要求的值那么首先将z不断右移,知道最低位为1,这通过计算z/x即可完成。这样再将z/x右移1位就得到了第(3)要求的值。

(这里就是位运算的应用了,很精髓)

读完发现二进制z/x并不会做,百度了下做法,发现还是有一个讲的比较清楚的


 
(这张图很形象的讲明白了,跟10进制除法一样的,只不过因为是2进制,所以不用除只需要减就行)
二进制除法中,除到最后有余数怎么办?
 
如果是 定点数(整数),那就舍掉了。
如果是 浮点数,则继续加 位运算,直到 精度达到后舍掉。
 
 
二进制乘法远比十进制简单,比如乘数是1011,只需将将被乘数分别左移3位、1位,移动后补入0,并将这三个数(被乘数左移3位的、被乘数左移1位的及未移位的被乘数)在累加器中相加,所得总和就是积,根据需要积可再转化为十进制。 
除法与乘法类似,只不过将左移改为右移,加改成减。实际上减也是通过取补码后再加
  
  
除法一般不好优化,直接按照笔算步骤来算就可以了:  1、根据被除数(余数)和除数的大小来上商;  2、被除数(余数)低位补0,再减去右移后的除数,也可以改为左移余数,减去除数,这样可以确保参与运算的寄存器具有相同的位数;  3、商写到寄存器的最低位,然后商左移1位。
 
连续做减法,现在公认的就是这个,让被除数连续减去n个除数,直到差小于除数时为止,这样减去的次数就是商,剩下的差就是余数。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
    int l = 0, t, n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int x = n & (-n);
        int y = x + n;
        n = (n & ~y) / x >> 1 | y;
        printf("Case %d: %d\n",++l,n);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值