最近的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(构造、枚举)的更多相关文章

  1. Codeforces 460D Little Victor and Set --分类讨论+构造

    题意:从区间[L,R]中选取不多于k个数,使这些数异或和尽量小,输出最小异或和以及选取的那些数. 解法:分类讨论. 设选取k个数. 1. k=4的时候如果区间长度>=4且L是偶数,那么可以构造四 ...

  2. Codeforces 460D Little Victor and Set(看题解)

    Little Victor and Set 其他都很好求, 只有k == 3的时候很难受.. 我们找到第一个不大于l的 t, 答案为 l, 3 * t, (3 * t) ^ l 感觉好像是对的, 感觉 ...

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

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

  5. Codeforces 1276C/1277F/1259F Beautiful Rectangle (构造)

    题目链接 http://codeforces.com/contest/1276/problem/C 题解 嗯,比赛结束前3min想到做法然后rush不出来了--比赛结束后又写了15min才过-- 以下 ...

  6. Codeforces 512E - Fox And Polygon(构造)

    Codeforces 题面传送门 & 洛谷题面传送门 中规中矩的构造题一道. 首先考虑将两张图都向一个中间状态转化.方便起见我们取所有点都连向 \(1\) 号点的情形作为中间状态. 考虑怎样从 ...

  7. Codeforces Gym 100187K K. Perpetuum Mobile 构造

    K. Perpetuum Mobile Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...

  8. Codeforces Gym 100425H H - Football Bets 构造

    H - Football BetsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...

  9. 【hihocoder1255 Mysterious Antiques in Sackler Museum】构造 枚举

    2015北京区域赛现场赛第2题. 题面:http://media.hihocoder.com/contests/icpcbeijing2015/problems.pdf OJ链接:http://hih ...

随机推荐

  1. linux 打造man中文帮助手册

    博客转自:http://my.oschina.net/hbzhangmao/blog/354533 学IT的同学都知道, Linux是一个好东西, 但初学者往往会因为太多的命令觉得头疼, 更头疼的是所 ...

  2. 利用PHP从淘宝采集评论和成交数据

    如果不想通过淘宝开放平台API获取数据,那么另外一个很好的办法就是采集了.一般来说,采集一个网页上的内容,只需要用CURL获取源代码,然后用正则表达式取出需要的内容就可以,不过如果这样载入一个淘宝的页 ...

  3. django 初级(一) 配置与周边

    一.下载安装 从 https://www.djangoproject.com/download/ 下载最新的 django 版本,写本文时最新版本为 django 1.7,官方说只需要 python6 ...

  4. 解决pydev报unsolved import的问题

    安装Flask_RESTful-0.2.11包后, 并在pydev 对应的 interpreter 重新刷新了System PYTHONPATH, 看见Lib\site-packages\flask_ ...

  5. ajax状态

    ajax的几个状态 Uninitialized 初始化状态.XMLHttpRequest 对象已创建或已被 abort() 方法重置. Open open() 方法已调用,但是 send() 方法未调 ...

  6. 小米手机无法打开程序报错Unable to instantiate application com.android.tools.fd.runtime.BootstrapApplication的解决办法

    打开studio的setting 然后 Preferences -> Build, Execution, Deployment -> Instant Run -> Enable In ...

  7. leetcode 82. Remove Duplicates from Sorted List II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  8. Android学习笔记(十八)——再谈升级数据库

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 之前我们为了保证数据库中的表是最新的,只是简单地在 onUpgrade()方法中删除掉了当前所有的表,然后强制 ...

  9. mysql 之基本操作

    已经在板子内部把所有的环境都搭好了,现在的要求是对Mysql 进行一些基本的操作. 在这之前,记录几个要点以防以后忘记. 一,准备工作. 1. 将usr/share/mysql 目录下的 my-lag ...

  10. [转]ubuntu 14.04 如何开启和关闭触控板

    转自:http://lxn348567248.blog.163.com/blog/static/1201759252014649443513/ 禁用触摸板的命令:  sudo rmmod psmous ...