codeforces 460D Little Victor and Set(构造、枚举)
最近的CF几乎都没打,感觉挺水的一个题,不过自己仿佛状态不在,看题解才知道做法。
输入l, r, k (1 ≤ l ≤ r ≤ 1012; 1 ≤ k ≤ min(106, r - l + 1)).
从[l,r]选至多k个数使得选出的数的异或值最小,输出最小异或值和方案。
分类讨论,首先如果r-l+1<=4,枚举集合解决之。
先面讨论r-l+1>=5的情况:
此时有至少5个数可以选择,故至少有连续的4个数满足2x,2x+1,2x+2,2x+3。
k==1时显然方案为{l}。k==2时,显然方案为{2x,2x+1}。k>=4时,显然方案为{2x,2x+1,2x+2,2x+3}。
k==3时再另外考虑:
首先,异或值至多为1(参考k==2)
我们现在来找异或值可否为0。先假设可以,则显然是选3个数。不妨设x>y>z。
111...1111
111...1110
000...0001
显然x,y,z前半部分必定是如上这样的,但由于我们要使得x,y,z尽量靠近,所以x,y,z前半部分必然是如下
11
10
01
之后,每添加一位,有可能是yi=zi=1,xi=0或xi=zi=1,yi=0或xi=yi=1,zi=0。
由于要x,y,z尽量靠近,所以显然采取yi=zi=1,zi=0。
所以x,y,z的二进制形式如下
110...0
101...1
011...1
至此,问题大致解决,剩下的就是些细节问题,问题不大。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std; #define ll long long int cnt(int i){
int ret=0;
while(i) i-=i&(-i), ++ret;
return ret;
}
int main(){
ll l,r;
int k;
while(~scanf("%I64d%I64d%d",&l,&r,&k)){
if(r-l+1<5){
int n=r-l+1;
ll ansxor=1ll<<60;
vector<ll>val;
for(int i=1;i<(1<<n);++i){
ll xx=0;
for(int j=0;j<n;++j)
if(i&(1<<j)) xx^=l+j;
if(xx<ansxor && cnt(i)<=k){
ansxor=xx;
val.clear();
for(int j=0;j<n;++j) if(i&(1<<j)) val.push_back(l+j);
}
}
printf("%I64d\n",ansxor);
printf("%d\n",val.size());
for(int i=0;i<val.size();++i) printf("%I64d%c",val[i],i==val.size()-1?'\n':' ');
}
else if(r-l+1>=5){
if(k==1){printf("%I64d\n1\n%I64d\n",l,l);continue;}
if(k==2){
if(l&1) l++;
puts("1");
puts("2");
printf("%I64d %I64d\n",l,l+1);
}
else if(k>=4){
if(l&1) l++;
puts("0");
puts("4");
printf("%I64d %I64d %I64d %I64d\n",l,l+1,l+2,l+3);
}
else if(k==3){
ll x=-1,y,z;
for(ll i=3;i<=r;i=i<<1){
if((i^(i-1))>=l){
x=i;
y=i - 1;
z=i^(i-1);
break;
}
}
if(x!=-1){
puts("0");
puts("3");
printf("%I64d %I64d %I64d\n",x,y,z);
}
else {
if(l&1) l++;
puts("1");
puts("2");
printf("%I64d %I64d\n",l,l+1);
}
}
}
}
return 0;
}
codeforces 460D Little Victor and Set(构造、枚举)的更多相关文章
- Codeforces 460D Little Victor and Set --分类讨论+构造
题意:从区间[L,R]中选取不多于k个数,使这些数异或和尽量小,输出最小异或和以及选取的那些数. 解法:分类讨论. 设选取k个数. 1. k=4的时候如果区间长度>=4且L是偶数,那么可以构造四 ...
- Codeforces 460D Little Victor and Set(看题解)
Little Victor and Set 其他都很好求, 只有k == 3的时候很难受.. 我们找到第一个不大于l的 t, 答案为 l, 3 * t, (3 * t) ^ l 感觉好像是对的, 感觉 ...
- Codeforces 460D. Little Victor and Set
D. Little Victor and Set time limit per test:1 second memory limit per test:256 megabytes input:stan ...
- codeforces 460D:Little Victor and Set
Description Little Victor adores the sets theory. Let us remind you that a set is a group of numbers ...
- Codeforces 1276C/1277F/1259F Beautiful Rectangle (构造)
题目链接 http://codeforces.com/contest/1276/problem/C 题解 嗯,比赛结束前3min想到做法然后rush不出来了--比赛结束后又写了15min才过-- 以下 ...
- Codeforces 512E - Fox And Polygon(构造)
Codeforces 题面传送门 & 洛谷题面传送门 中规中矩的构造题一道. 首先考虑将两张图都向一个中间状态转化.方便起见我们取所有点都连向 \(1\) 号点的情形作为中间状态. 考虑怎样从 ...
- Codeforces Gym 100187K K. Perpetuum Mobile 构造
K. Perpetuum Mobile Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...
- Codeforces Gym 100425H H - Football Bets 构造
H - Football BetsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...
- 【hihocoder1255 Mysterious Antiques in Sackler Museum】构造 枚举
2015北京区域赛现场赛第2题. 题面:http://media.hihocoder.com/contests/icpcbeijing2015/problems.pdf OJ链接:http://hih ...
随机推荐
- 如何判断PHP 是线程安全还是非线程安全的
什么是线程安全与非线程安全? 线程安全就是在多线程环境下也不会出现数据不一致,而非线程安全就有可能出现数据不一致的情况. 线程安全由于要确保数据的一致性,所以对资源的读写进行了控制,换句话说增加了系统 ...
- 自定义select控件开发
目的:select下拉框条目太多(上百),当用户选择具体项时会浪费用户很多时间去寻找,因此需要一个搜索框让用户输入关键字来匹配列表,便于用户选择 示例图: 1.html结构 <div class ...
- linq学习
最全的linq学习文章: http://www.cnblogs.com/heyuquan/p/Linq-to-Objects.html
- centos bad ELF interpreter: No such file or directory
sudo yum install glibc.i686
- sass兼容IE8透明度方法
你可以轻松的利用 {Sass::Script::Functions#ie_hex_str ie_hex_str} 函数对其做转换.$translucent-red: rgba(, , , 0.5); ...
- JVM内存监控工具 Jconsole
-------------Jconsole监视远程的linux服务器上的tomcat ----------------------------- 1.linux服务器上的tomcat 的bin/cat ...
- 5分钟教你Windows 10中将“运行”固定到开始菜单
导读 “运行”功能深受很多资深IT之家用户喜爱,因为它简约.方便.实用.在Win7等旧版系统中,用户可以让该功能直接在开始菜单显示,方便操作.但在Win10中,由于开始菜单已经重新编写,原有的设定已经 ...
- 关于promise(一)
该新特性属于 ECMAScript 2015(ES6)规范,在使用时请注意浏览器兼容性. 由于ES6原生提供Promise,所以无需安装Promise库.但在ES5环境下我们可以使用bluebird库 ...
- linux kernel 杂谈
首先介绍一下背景吧,工作三个星期了.复习了一波u-boot,跟了一下事件上报,搞了下平台设备,扣了一个内存检查代码. 想想生活是不是有点无聊.对啊,真的很无聊!!!! 无聊也没有办法啊,所以找点方法去 ...
- 字符串驱动技术—— MethodAddress , MethodName , ObjectInvoke
首先看一段Delphi帮助中的介绍(After Delphi 6 ): Returns the address of a published method. class function Method ...