Codeforces Round #456 (Div. 2) B. New Year's Eve
传送门: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到n中去最多选择k个数,使你选择的数异或起来最大。只要知道异或的运算法则这个题是很简单,直接的出n的二进制数位,然后最大的数就是二进制所有的位全是1就是答案了,如果k是1,那么最大的答案就是n。因为很明显的这些数是从1到n,所以直接选择两数异或起来二进制的各个位变为全1就是最大的了。
 - 而且这个题的数据范围也给了很大的提示啊,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的更多相关文章
- 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 ...
 - Codeforces Round #456 (Div. 2)
		
Codeforces Round #456 (Div. 2) A. Tricky Alchemy 题目描述:要制作三种球:黄.绿.蓝,一个黄球需要两个黄色水晶,一个绿球需要一个黄色水晶和一个蓝色水晶, ...
 - Codeforces Round #456 (Div. 2) A. Tricky Alchemy
		
传送门:http://codeforces.com/contest/912/problem/A A. Tricky Alchemy time limit per test1 second memory ...
 - Codeforces Round #456 (Div. 2) 912E E. Prime Gift
		
题 OvO http://codeforces.com/contest/912/problem/E 解 首先把这个数字拆成个子集,各自生成所有大小1e18及以下的积 对于最坏情况,即如下数据 16 2 ...
 - Codeforces Round #456 (Div. 2) 912D D. Fishes
		
题: OvO http://codeforces.com/contest/912/problem/D 解: 枚举每一条鱼,每放一条鱼,必然放到最优的位置,而最优位置即使钓上的概率最大的位置,即最多的r ...
 - 【Codeforces Round #456 (Div. 2) A】Tricky Alchemy
		
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 统计需要的个数. 不够了,就买. [代码] #include <bits/stdc++.h> #define ll lo ...
 - 【Codeforces Round #456 (Div. 2) B】New Year's Eve
		
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然10000..取到之后 再取一个01111..就能异或成最大的数字了. [代码] /* 1.Shoud it use long ...
 - 【Codeforces Round #456 (Div. 2)  C】Perun, Ult!
		
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] set1 < pair < int,int > > set1;记录关键点->某个人怪物永远打不死了,第 ...
 - Codeforces Round #456 (Div. 2) B题
		
B. New Year's Evetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputout ...
 
随机推荐
- idea关闭sonar自动扫描
			
file-setting-other setting-sonar相关的setting全部关闭
 - 生产消费者模式与python+redis实例运用(中级篇)
			
上一篇文章介绍了生产消费者模式与python+redis实例运用(基础篇),但是依旧遗留了一个问题,就是如果消费者消费的速度跟不上生产者,依旧会浪费我们大量的时间去等待,这时候我们就可以考虑使用多进程 ...
 - LaTeX 符号大全
			
常用数学符号的 LaTeX 表示方法 2016-10-31 16:22 | 黄荣生 常用数学符号的 LaTeX 表示方法 1.指数和下标可以用^和_后加相应字符来实现.比如: 2.平方根(squa ...
 - Apache Spark 2.2.0 中文文档 - GraphX Programming Guide | ApacheCN
			
GraphX Programming Guide 概述 入门 属性 Graph 示例属性 Graph Graph 运算符 运算符的汇总表 Property 运算符 Structural 运算符 Joi ...
 - MySQL-数据类型及选择
			
一.数据类型 详见:http://www.runoob.com/mysql/mysql-data-types.html 二.类型选择 整形>date,time>enum,char>v ...
 - /usr/local/sbin/dsniff
			
/usr/local/sbin/dsniff 捕获可用的密码
 - python语法之一
			
Python 标识符 在 Python 里,标识符由字母.数字.下划线组成. 在 Python 中,所有标识符可以包括英文.数字以及下划线(_),但不能以数字开头. Python 中的标识符是区分大小 ...
 - ubuntu   自动启动程序
			
首先打开终端ctrl + alt + t sudo -i 输入密码:ubuntu chmod 777 /etc/rc.local 打开 vi /etc/rc.local 按 i 键进入输入 ...
 - VMware-Ubuntu16.04LTS-安装ssh
			
1,检查是否安装ssh 2,安装ssh
 - java Vamei快速教程13   String类
			
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 之前的Java基础系列中讨论了Java最核心的概念,特别是面向对象的基础.在Jav ...