912B - New Year's Eve

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 12 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落凡尘.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值