@UPC 4897 @BAPC2017 Preliminaries : Envious Exponents(思维,二进制)

Envious Exponents

时间限制: 1 Sec  内存限制: 128 MB
提交: 252  解决: 37
[提交] [状态] [讨论版] [命题人: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  ,找到一个 新的数 p, 满足  p 严格大于n  ,并且 要满足  等与 k 个 不相等的  2^i 相加.

思路:   采用 二进制,    统计 n 二进制  中1 的个数, 分类, 分为 1 的个数 cot  小于 k 和  大于等于k

如果 小与,  那么从 最小位开始 非0 位置变成 1,,

如果大于等与,   分类:

①如果等于,  那么 保证严格大于, 我需要删除一个1 在增加一个1 ,

例如 实例:  19 ,3   19 的二进制位 10011 需要删除第二个1 ,增加一个1 变成 10101  是 21  最小的. 然后 从新增加1的位置开始,到最小位,需要把1 全部右移, 保证使得满足条件下,尽可能的小一些.

 

②如果大于, 先分类讨论一下, 如果 从最高位开始  第一个连续为 1 区域 的个数如果大于k, 那么 

然后从最小位开始删除 cot-k 个, 然后  变成等于的情况,  用等于的情况进行处理.

 

代码 有点长,,    可以优化,  用的 bitset  容器.  bitset 最小位 从0 开始.

代码:

#include <bits/stdc++.h>
#include <stdio.h>
 
#define rep(i,a,n) for(int i = a ;i <=n;i++)
#define per(i,a,n) for(int i =n;i>=a;i--)
 
typedef unsigned long long ll;
using namespace std;
const int maxn =2e6+10;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
 
int main()
{
    ll n;int k;
    scanf("%llu %d",&n,&k);
    bitset<66>B(n);
    int cot = B.count();
    /*for(int i = 0 ;i < 64 ;i++)
    {
        cout<<B[i]<<" ";
    }
    cout<<endl;*/
    ll sum = 0;
    if( cot >= k) // >= delete  || = delete 1 add 1; 
    {
        int p = cot -k; // need to delete;
        int c = 0;
        int pos = -1;
        for(int i= 65;i>=0;i--)
        {
            if(B[i]==1)
            {
                pos = i;
                for(int j = i;j>=0;j--)
                {
                    if(B[j])
                        c++;
                    else
                    {
                        break;
                    }
                }
                break;
            }
        }
        if( c >=k)
        {
            B.reset();
            B[pos+1] = 1;
            int t = k-1;
            for(int i = 0 ;i <66;i++)
            {
                if(t==0) break;
                B[i] = 1;
                t--;
            }
        }
        else
        {
            for(int i =0 ;i < 66 ;i++)//delete
            {
                if(p==0) break;
                if(B[i]==1&&p)
                {
                    B[i]=0;p--;
                }
            }
            pos = -1;
            for(int i =0 ;i < 66 ;i++)// == 
            {
                if(B[i]==1&&B[i+1]==0)
                {
                    B[i] = 0;B[i+1]=1;
                    pos = i+1;
                    break;
                }
            }   
            c = 0;
            for(int i=pos-1;i>=0;i--)
            {
                if(B[i])
                {
                    c++;B[i]=0;
                }
 
            }
            for(int i = 0;i<66;i++)
            {
                if(c==0) break;
                B[i] = 1;c--;
            }
        }
 
    }
    else if( cot < k)//< add ;
    {
        int p = k-cot;
        for(int i =0 ;i < 66 ;i++)
        {
            if(p==0) break;
            if(B[i]==0&&p)
            {
                B[i] =1 ;p--;
            }
        }
    }
    /*for(int i = 0 ;i < 64 ;i++)
    {
        cout<<B[i]<<" ";
    }
    cout<<endl;*/
     
    for(int i =0 ;i < 66 ;i++)
    {
 
        if(B[i])
        {
            sum+= (1ll<<i);
        }
    }
    printf("%llu\n", sum);
 
    return 0;
 
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值