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

版权声明:岂曰无衣,与子同袍 https://blog.csdn.net/sizaif/article/details/82024404

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;
 
}

猜你喜欢

转载自blog.csdn.net/sizaif/article/details/82024404
UPC