Envious Exponents

4897: Envious Exponents

时间限制: 1 Sec  内存限制: 128 MB
提交: 320  解决: 52
[提交] [状态] [讨论版] [命题人: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

 

调出来了。。。居然是-2写成-1了。。代码已更新

测试出的那个bug是1 1,不太清楚为什么我这么多随机都没随机到类似的。。

。。。。暂时先这样吧,代码在计蒜客能过,中石油不知道为什么过不了。

我自己随机了128MB的输入,和网上ac代码比较,没有问题。。。

随机输入代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main()
{
    srand(time(0));
    FILE *pf = fopen("input","w");

    for(int i=0;i<10000000;i++)
    {
        ll x = (rand()%10000+100)*100000;

        if(x<0)
        {
            while(x<0)
                x = (rand()%10000+100)*100000;
        }

        fprintf(pf,"%lld %d\n",x,rand()%59+1);
    }
    fclose(pf);

    return 0;
}

 

代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
bool a[100],b[100];
int main()
{
    ll n,ans = 0;
    int k,len = 0,c = 0,n1 = 0;
    scanf("%lld%d",&n,&k);

    while(n)
        a[len++] = n%2,n/=2;

    for(int i=len-1;i+1&&a[i];i--)c++;
    for(int i=len-1;i+1;i--)if(a[i])n1++;

    if(k>=len)
    {
        for(int i=0;i<k;i++)b[i]=1;
        if(k==c && c==len)
        {
            b[len++] = 1;
            b[len-2] = 0;///就是这里。。
        }
        if(k>len)
            len = k;
    }
    else
    {
        int x = 0,i;
        for(i=len-1;i+1;i--)
        {
            if(x==k-1)break;
            if(a[i])x++;
            b[i] = a[i];
        }
        if(k>n1)
        {
            for(int i=0;i<len;i++)b[i]=a[i];
            int l = k-n1;

            for(int i=0;l;i++)
            {
                if(!a[i])l--;
                b[i] = 1;
                if(i>=len)len=i+1;
            }

        }
        else if(i!=-1 && a[i])
        {
            i++;
            int cn = 0;
            while(b[i])b[i++] = 0,cn++;

            b[i] = 1;

            while(cn)b[cn-1] = 1,cn--;
            if(i>=len)len = i+1;
        }
        else
        {
            while(i+1&&!a[i])i--;
            b[i+1] = 1;
        }
    }

    //for(int i=len-1;i+1;i--)printf("%d",a[i]);printf("\n");
    //for(int i=len-1;i+1;i--)printf("%d",b[i]);printf("\n");

    ll x = 1;
    for(int i=0;i<len;i++)
        ans+=(x<<i)*b[i];
    printf("%lld\n",ans);

    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值