Since Grisha behaved well last year, at New Year's Eve he was visited by Ded Moroz who brought an enormous bag of gifts with him! The bag contains n sweet candies from the good ol' bakery, each labeled from 1 to n corresponding to its tastiness. No two candies have the same tastiness.
The choice of candies has a direct effect on Grisha's happiness. One can assume that he should take the tastiest ones — but no, the holiday magic turns things upside down. It is the xor-sum of tastinesses that matters, not the ordinary sum!
A xor-sum of a sequence of integers a1, a2, ..., am is defined as the bitwise XOR of all its elements:
, here
denotes the bitwise XOR operation; more about bitwise XOR can be found here.
Ded Moroz warned Grisha he has more houses to visit, so Grisha can take no more than k candies from the bag. Help Grisha determine the largest xor-sum (largest xor-sum means maximum happiness!) he can obtain.
The sole string contains two integers n and k (1 ≤ k ≤ n ≤ 1018).
Output one number — the largest possible xor-sum.
4 3
7
6 6
7
In the first sample case, one optimal answer is 1, 2 and 4, giving the xor-sum of 7.
In the second sample case, one can, for example, take all six candies and obtain the xor-sum of 7.
这题真是醉了,我无语,把不超过k个翻译成了一定要k个,之前一直做不出来,看清题目后立马会
如果只呢去一家的话,肯定是n最大,因为Max=0^Max
如果大于等于2那么就是它的所有位都为1,这个也是很好证明的
因为最大那个最高位肯定是1,那么它其它是0的只要异或1就ok,那么你一定能在1-n这范围里找到这样一个数,因为二进制进位
#include<cstdio>
using namespace std;
long long f[61]={1};
int main(){
for(int i=1;i<=60;i++)
f[i]=f[i-1]*2;
long long n,k;
scanf("%I64d%I64d",&n,&k);
if(k==1)
printf("%I64d\n",n);
else{
int i;
for(i=0;i<60;i++)
if(f[i]<=n&&n<f[i+1])
break;
printf("%I64d\n",f[i+1]-1);
}
return 0;
}
在这里我想问一下各位大佬,如果一定要去k家,那么要怎么办?
本文探讨了一个有趣的问题:如何从编号1到n的不同甜度糖果中选取不超过k个糖果,使得这些糖果甜度的异或和最大。文章给出了具体的解决思路与实现代码。
1353

被折叠的 条评论
为什么被折叠?



