首页 > 其他 > 详细

Envious Exponents

时间:2018-08-24 18:43:05      阅读:163      评论:0      收藏:0      [点我收藏+]

题目描述

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 speci?c 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 #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

 

Envious Exponents

原文:https://www.cnblogs.com/SoulSecret/p/9531336.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!