Codeforces Round #456 B. New Year's Eve

本文探讨了一个有趣的问题:如何从编号1到n的不同甜度糖果中选取不超过k个糖果,使得这些糖果甜度的异或和最大。文章给出了具体的解决思路与实现代码。

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 k candies 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.




这题真是醉了,我无语,把不超过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家,那么要怎么办?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值