B. New Year's Eve
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 kcandies from the bag. Help Grisha determine the largest xor-sum (largest xor-sum means maximum happiness!) he can obtain.
Input
The sole string contains two integers n and k (1 ≤ k ≤ n ≤ 1018).
Output
Output one number — the largest possible xor-sum.
Examples
input
4 3
output
7
input
6 6
output
7
Note
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.
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 kcandies 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.
感谢网上的博主们的思路参考,大概明白做的方法和原理了。cf上的题,很有技巧啊~就是很多时候题贼难读懂,跟绕口令的是的,英语没到六级不靠翻译,是不可能读懂的,当然这个题还是能读懂(有时候翻译都读不懂。。。)。就是没思路。
题意:输入n和k表示,表示 1到n中选k个数进行异或加法,让他们的结果最大。
题解:贪心思路 如果k=1,我们就选最大的那个数n。如果看k>=2,n化为二进制后每一位都换为1的值即为结果,二进制全为1的数。例如n=8,k=2,8->转化为二进制为1000,那么最大结果应该是1111,所以选择7->0111,进行异或相加就是答案1111。即答案是2^4-1,(4为最大值n的二进制位数)。只要k>=2,可以选则n和一个数和n互补的数,异或相加,为最大值,即答案。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll n,k,ans;
while(cin>>n>>k)
{
ans=1;
if(k==1)
{
cout<<n<<endl;
return 0;
}
while(n)
{
n>>=1; ///位运算等同于n=n/2,判断n的二进制位数,没运行一次,就代表有一位
ans<<=1;
}
cout<<ans-1<<endl;
}
return 0;
}