Light OJ 1042 – Secret Origins


/*

Link : http://www.lightoj.com/volume_showproblem.php?problem=1042
*/

#include <stdio.h>

int cnt(long long n)
{
    int temp=0;
    for(long long i=1; i<=n; i*=2)
    {
        if(n & i)
            temp++;
    }
    return temp;
}

int main()
{
    int t,cases=0;
    scanf("%d",&t);

    while(t--)
    {
        long long n,ans;
        scanf("%lld",&n);

        for(long long i=1; i<=n; i*=2)
            if(n & i)
            {
                ans=n+i;
                break;
            }

        int diff=cnt(n)-cnt(ans);

        for(int i=0; i<diff; i++)
            ans+=(1<<i);

        printf("Case %d: %lld\n",++cases,ans);
    }
    return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s