【二進制】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的個數,那麼有三種狀況ios

  (1)cnt1>k   轉換爲cnt1=k的狀況處理,即將低位多的1清爲0c++

  (2)cnt1=k   將第一個非前置0變成1,剩下的低位清0,從低位開始將剩下的1填上app

  (3)cnt1<k   直白的操做,從低位開始填1ide

  最後將二進制轉換回來,畢竟不爆long longspa

代碼:

  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
相關文章
相關標籤/搜索