传送门:http://codeforces.com/contest/912/problem/B

B. New Year’s Eve

time limit per test1 second

memory limit per test256 megabytes

Problem Description

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


解题心得:

  1. 题意就是叫你从1到n中去最多选择k个数,使你选择的数异或起来最大。只要知道异或的运算法则这个题是很简单,直接的出n的二进制数位,然后最大的数就是二进制所有的位全是1就是答案了,如果k是1,那么最大的答案就是n。因为很明显的这些数是从1到n,所以直接选择两数异或起来二进制的各个位变为全1就是最大的了。
  2. 而且这个题的数据范围也给了很大的提示啊,n和k最大都要取到1e18,如果考算法O(n)也过不了啊。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll ans; ll get_w(ll n)
{
ll ans = 0;
while(n)
{
ans++;
n>>=1;
}
return ans;
} int main()
{
ans = 0;
ll k,n;
scanf("%lld%lld",&n,&k);
ll w = get_w(n);//得到n的二进制的位数
if(k >= 2)
ans = pow(2,w)-1;
else
ans = n;
printf("%lld",ans);
return 0;
}

Codeforces Round #456 (Div. 2) B. New Year's Eve的更多相关文章

  1. Codeforces Round #456 (Div. 2) B. New Year's Eve

    B. New Year's Eve time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  2. Codeforces Round #456 (Div. 2)

    Codeforces Round #456 (Div. 2) A. Tricky Alchemy 题目描述:要制作三种球:黄.绿.蓝,一个黄球需要两个黄色水晶,一个绿球需要一个黄色水晶和一个蓝色水晶, ...

  3. Codeforces Round #456 (Div. 2) A. Tricky Alchemy

    传送门:http://codeforces.com/contest/912/problem/A A. Tricky Alchemy time limit per test1 second memory ...

  4. Codeforces Round #456 (Div. 2) 912E E. Prime Gift

    题 OvO http://codeforces.com/contest/912/problem/E 解 首先把这个数字拆成个子集,各自生成所有大小1e18及以下的积 对于最坏情况,即如下数据 16 2 ...

  5. Codeforces Round #456 (Div. 2) 912D D. Fishes

    题: OvO http://codeforces.com/contest/912/problem/D 解: 枚举每一条鱼,每放一条鱼,必然放到最优的位置,而最优位置即使钓上的概率最大的位置,即最多的r ...

  6. 【Codeforces Round #456 (Div. 2) A】Tricky Alchemy

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 统计需要的个数. 不够了,就买. [代码] #include <bits/stdc++.h> #define ll lo ...

  7. 【Codeforces Round #456 (Div. 2) B】New Year's Eve

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然10000..取到之后 再取一个01111..就能异或成最大的数字了. [代码] /* 1.Shoud it use long ...

  8. 【Codeforces Round #456 (Div. 2) C】Perun, Ult!

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] set1 < pair < int,int > > set1;记录关键点->某个人怪物永远打不死了,第 ...

  9. Codeforces Round #456 (Div. 2) B题

    B. New Year's Evetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputout ...

随机推荐

  1. synchronized重入后抛出异常,锁释放了吗

    synchronized: 用于同步方法或者代码块,使得多个线程在试图并发执行同一个代码块的时候,串行地执行.以达到线程安全的目的. 允许重入: 在多线程的时候是这样的,但是对于单线程,是允许重入的, ...

  2. window mysql5.7 zip 安装

    第一步 ? 1 2 3 4 5 6 7 8 9 10 11 12 my-default.ini 添加配置: #绑定IPv4和3306端 bind-address = 127.0.0.1 port = ...

  3. Oracle 11g服务详细介绍

    按照windows 7 64位 安装oracle 11g R2中的方法成功安装Oracle 11g后,共有7个服务,这七个服务的含义分别为: 1. Oracle ORCL VSS Writer Ser ...

  4. Eucalyptus-NC管理

    1.前言 Elastic Utility Computing Architecture for Linking Your Programs To Useful Systems (Eucalyptus) ...

  5. restesay部署学习过程中遇到的问题

    Exception starting filter org.jboss.resteasy.plugins.serer.servlet.Filter30Dispatcherjava.lang.Class ...

  6. python3基础01(常见语法基础汇总)

    #!/usr/bin/env python# -*- coding:utf-8 -*- # 换行\n 续行\ s[:i] + s[i:] 等于 s#转义 \e 空 \000 八进制 \oyy 十六进制 ...

  7. Linux常用命令-1

    内部命令:属于Shell解释器的一部分(已调入内存) 外部命令:独立于Shell解释器之外的程序文件(在磁盘上) 获得命令帮助 1)内部命令help 查看Bash内部命令的帮助信息 2)命令的“--h ...

  8. 05、Win7上openSSH的安装与配置

    05.Win7上openSSH的安装与配置 1.概述 linux上的ssh命令在网络通信场景下非常方便.现在windows也支持ssh方式和远程主机进行访问.如果只是使用ssh简单的访问功能,就需要很 ...

  9. cms-框架搭建

    1.web.xml中的配置,在配置中主要有: 1.1.过滤器: 1.1.1:shiro权限过滤器 1.1.2:字符编码过滤器 1.2.监听器: 1.2.1:spring监听器 1.3.servlet ...

  10. jquery UI_tabs

    1.tab使用 <!doctype html> <html lang="en"> <head> <meta charset="u ...