时间限制: 1 Sec 内存限制: 128 MB
提交: 320 解决: 52
[提交] [状态] [讨论版] [命题人: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
测试出的那个bug是1 1,不太清楚为什么我这么多随机都没随机到类似的。。
。。。。暂时先这样吧,代码在计蒜客能过,中石油不知道为什么过不了。
我自己随机了128MB的输入,和网上ac代码比较,没有问题。。。
随机输入代码
#include
using namespace std;
typedef long long ll;
int main()
{
srand(time(0));
FILE *pf = fopen("input","w");
for(int i=0;i<10000000;i++)
{
ll x = (rand()%10000+100)*100000;
if(x<0)
{
while(x<0)
x = (rand()%10000+100)*100000;
}
fprintf(pf,"%lld %d\n",x,rand()%59+1);
}
fclose(pf);
return 0;
}
代码
#include
#define ll long long
using namespace std;
bool a[100],b[100];
int main()
{
ll n,ans = 0;
int k,len = 0,c = 0,n1 = 0;
scanf("%lld%d",&n,&k);
while(n)
a[len++] = n%2,n/=2;
for(int i=len-1;i+1&&a[i];i--)c++;
for(int i=len-1;i+1;i--)if(a[i])n1++;
if(k>=len)
{
for(int i=0;ilen)
len = k;
}
else
{
int x = 0,i;
for(i=len-1;i+1;i--)
{
if(x==k-1)break;
if(a[i])x++;
b[i] = a[i];
}
if(k>n1)
{
for(int i=0;i=len)len=i+1;
}
}
else if(i!=-1 && a[i])
{
i++;
int cn = 0;
while(b[i])b[i++] = 0,cn++;
b[i] = 1;
while(cn)b[cn-1] = 1,cn--;
if(i>=len)len = i+1;
}
else
{
while(i+1&&!a[i])i--;
b[i+1] = 1;
}
}
//for(int i=len-1;i+1;i--)printf("%d",a[i]);printf("\n");
//for(int i=len-1;i+1;i--)printf("%d",b[i]);printf("\n");
ll x = 1;
for(int i=0;i