【二进制】Envious Exponents

题目描述

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

 

思路:

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

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

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

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

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

代码:

  1 #include <iostream>
  2 #include <bits/stdc++.h>
  3 using namespace std;
  4 typedef long long ll;
  5 int cnt,cnt1,cnt0;
  6 ll n,k;
  7 int a[100];
  8 void init()
  9 {
 10     memset(a,0,sizeof(a));
 11     while(n)
 12     {
 13         int temp=n%2;
 14         a[cnt++]=temp;
 15         n/=2;
 16         if(temp==0)
 17             cnt0++;
 18         else
 19             cnt1++;
 20     }
 21     /*
 22     for(int i=0;i<cnt;i++)
 23     {
 24         cout << a[i] ;
 25     }
 26     */
 27 }
 28 int main()
 29 {
 30     cin>>n>>k;
 31     init();
 32     if(k<cnt1)
 33     {
 34         int temp=cnt1;
 35         int pos=0;
 36         while(temp>k)
 37         {
 38             if(a[pos]==1)
 39             {
 40                 temp--;
 41                 a[pos]=0;
 42             }
 43             pos++;
 44         }
 45         cnt1=k;
 46     }
 47     if(k==cnt1)
 48     {
 49         bool flag=true;//前置0
 50         int cnt_1=0;
 51         int pos=0;
 52         while(flag||a[pos]==1)
 53         {
 54             if(a[pos]==1)
 55             {
 56                 cnt_1++;
 57                 flag=false;
 58             }
 59             pos++;
 60         }
 61         a[pos]=1;
 62         cnt_1--;
 63         /*for(int i=0; i<cnt; i++)
 64         {
 65  
 66             if(!flag&&a[i]==0)
 67             {
 68                 a[i]=1;
 69                 pos=i;
 70                 cnt_1--;
 71                 break;
 72             }
 73         }*/
 74         for(int i=0; i<pos; i++)
 75         {
 76             if(cnt_1)
 77             {
 78                 a[i]=1;
 79                 cnt_1--;
 80             }
 81             else
 82                 a[i]=0;
 83         }
 84     }
 85     if(k>cnt1)
 86     {
 87         int tot=cnt1;
 88         int pos=0;
 89         while(tot<k)
 90         {
 91             if(a[pos]==0)
 92             {
 93                 a[pos]=1;
 94                 tot++;
 95             }
 96             pos++;
 97         }
 98     }
 99     ll all=0;
100     ll temp=1;
101     for(int i=0; i<70; i++)
102     {
103         all+=a[i]*temp;
104         temp*=2;
105     }
106     cout << all << endl;
107     //cout << "Hello world!" << endl;
108     return 0;
109 }
View Code
原文地址:https://www.cnblogs.com/SoulSecret/p/9531336.html