Envious Exponents(思维+模拟)

                                    Envious Exponents

                                                                时间限制: 1 Sec  内存限制: 128 MB
                                                                                提交: 302  解决: 48
                                                                [提交] [状态] [讨论版] [命题人:admin]

Alice and Bob have an integer N. Alice and Bob are not happy with their integer. Last night they went to a cocktail party and found that another couple had the exact same integer! Because of that they are getting a new integer.

Bob wants to impress the other couple and therefore he thinks their new integer should be strictly larger than N.
Alice herself is actually fond of some specific integer k. Therefore, Alice thinks that whatever integer they pick, it should be possible to write it as a sum of k distinct powers of 2.

Bob is also a cheapskate, therefore he wants to spend as little money as possible. Since the cost of an integer is proportional to its size, he wants to get an integer that is as small as possible.

输入

• A single line containing two integers N and k, with 1 ≤ N ≤ 1018 and 1 ≤ k ≤ 60.

输出

Output M, the smallest integer larger than N that can be written as the sum of exactly k distinct powers of 2.

样例输入

1 2

样例输出

3

 

                                                                           [提交]          [状态]

题意

给你一个 n 和一个 k 找一个比n的大并且它的二进制中1的个数为k的最小的数

 

题解

         对每个数字进行二进制分解,统计二进制表示中1的个数,那么有三种情况

  (1)cnt1>k   转换为cnt1=k的情况处理,即将低位多的1清为0

  (2)cnt1=k   将第一个非前置0变成1,剩下的低位清0,从低位开始将剩下的1填上

  (3)cnt1<k   直白的操作,从低位开始填1

     最后将二进制转换回来,毕竟不爆long long

PS  比赛的时候想炸了,当时没有把 cnt1>k 和 cnt1=k 的情况分开,然后,,,,就没有然后了,,,

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e6+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b)
{
    return a/gcd(a,b)*b;
}
inline ll qpow(ll a,ll b)
{
    ll r=1,t=a;
    while(b)
    {
        if(b&1)r=(r*t)%mod;
        b>>=1;
        t=(t*t)%mod;
    }
    return r;
}
inline ll inv1(ll b)
{
    return qpow(b,mod-2);
}
inline ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(!b)
    {
        x=1;
        y=0;
        return a;
    }
    ll r=exgcd(b,a%b,y,x);
    y-=(a/b)*x;
    return r;
}
inline ll read()
{
    ll x=0,f=1;
    char c=getchar();
    for(; !isdigit(c); c=getchar()) if(c=='-') f=-1;
    for(; isdigit(c); c=getchar()) x=x*10+c-'0';
    return x*f;
}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
const int maxn = 1e4 + 10;
ll n,k;
int a[100];
int cnt,cnt1,cnt0;
int main()
{

    cnt=cnt1=cnt0=0;
    memset(a,0,sizeof(a));
    scanf("%lld%lld",&n,&k);
    while(n)
    {
        int temp=n%2;
        a[cnt++]=temp;
        n/=2;
        if(temp)
            cnt1++;
    }
    if(k<cnt1)
    {
        int temp=cnt1;
        int pos=0;
        while(temp>k)
        {
            if(a[pos]==1)
            {
                temp--;
                a[pos]=0;
            }
            pos++;

        }
        cnt1=k;
    }
    if(k==cnt1)
    {
        int flag=1;
        int cnt2=0;
        int pos=0;

        while(flag || a[pos]==1)
        {
            if(a[pos]==1)
            {
                cnt2++;
                flag=0;
            }
            pos++;
        }
        a[pos]=1;


        cnt2--;
        for(int i=0; i<pos; i++)
        {
            if(cnt2)
            {
                a[i]=1;
                cnt2--;
            }
            else
                a[i]=0;
        }


    }
    if(k>cnt1)
    {
        int temp=cnt1;
        int pos=0;
        while(temp<k)
        {
            if(!a[pos])
            {
                a[pos]=1;
                temp++;
            }
            pos++;
        }
    }
    ll ans=0;
    ll x=1;
    for(int i=0; i<100; i++)
        ans+=(x<<i)*a[i];

    printf("%lld\n",ans);

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值