light oj 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

 


PROBLEM SETTER: MUNTASIR MUZAHID CHOWDHURY
SPECIAL THANKS: JANE ALAM JAN


題意:給一個二進制數n,找出一個比n大的數m,要求m和n二進制下1的數目相等

首先將數轉換成二進制形式,要使這個數比原數大,可以通過將低位的1和高位的0交換使值變大(當然要求相鄰,想想爲什麼),比如10011變爲10101,如果沒有可以交換,就將最高位進1,第二位變爲0比如1111變爲10111,確定變換位置後要將變換位置後位數的0 1 換一下位置,1放在最後,比如1001110應該變爲1010011,111110應該變爲1001111。


#pragma comment(linker,"/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<cmath>
#include<vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;

#define pi acos(-1.0)
#define eps 1e-10
#define pf printf
#define sf scanf
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
#define e tree[rt]
#define _s second
#define _f first
#define all(x) (x).begin,(x).end
#define mem(i,a) memset(i,a,sizeof i)
#define for0(i,a) for(int (i)=0;(i)<(a);(i)++)
#define for1(i,a) for(int (i)=1;(i)<=(a);(i)++)
#define mi ((l+r)>>1)
#define sqr(x) ((x)*(x))

const int inf=0x3f3f3f3f;
int a[100],num,n,t;
ll ans;

bool cmp(int x,int y)
{
    return x>y;
}

int main()
{
    sf("%d",&t);
    for1(i,t)
    {
        num=0,ans=0;
        sf("%d",&n);
        while(n)
            a[num++]=n%2,n/=2;
        int j,tag=0;
        for(j=0;j<num-1;j++)
            if(a[j]&&!a[j+1])
            {
                swap(a[j],a[j+1]),tag=1;//如果找到滿足 0 1的,交換找到第一個,退出
                break;
            }
        if(!tag)a[num-1]=0,a[num++]=1;//如果找不到交換的,最高位進1
        sort(a,a+j,cmp);//將變換後的位置的1放在最後(包括加0的情況)
        for(int i=num-1;i>=0;i--)
            ans=(ans<<1)+a[i];
        pf("Case %d: %lld\n",i,ans);//輸出最好用long long ,防止爆int
    }
    return 0;
}


發佈了73 篇原創文章 · 獲贊 7 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章