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 where all numbers are pairwise distinct. Today Victor wants to find a set of integers S that has the following properties:
- for all x
the following inequality holds l ≤ x ≤ r; - 1 ≤ |S| ≤ k;
- lets denote the i-th element of the set S as si; value
must be as small as possible.
Help Victor find the described set.
The first line contains three space-separated integers l, r, k (1 ≤ l ≤ r ≤ 1012; 1 ≤ k ≤ min(106, r - l + 1)).
Print the minimum possible value of f(S). Then print the cardinality of set |S|. Then print the elements of the set in any order.
If there are multiple optimal sets, you can print any of them.
8 15 3
1
2
10 11
8 30 7
0
5
14 9 28 11 16
Operation
represents the operation of bitwise exclusive OR. In other words, it is the XOR operation.
正解:分类讨论
解题报告:
今天考试原题,直接上题解:
分类讨论。
首先注意到(2x) ⊕ (2x + 1) = 1。一个直接推论就是(2x) ⊕ (2x + 1) ⊕ (2x + 2) ⊕(2x + 3) = 0。而k ≥ 5的时候一定可以选出四个数其异或和为0(例如,如果l是偶数,那么选l, l + 1, l + 2, l + 3,否则选l + 1, l + 2, l + 3, l + 4。这样总是合法的,因为r ≥ l + 4)。
然后按照k的值分类讨论。
k = 1。答案就是l。
k = 2。如果r = l + 1,那么答案是min(l ⊕ r, l),其中l表示只选一个数,l ⊕ r表示选两个数。否则答案一定为1。当r > l + 1的时候,如果l是偶数,那么答案是l ⊕ (l + 1),否则答案是(l + 1) ⊕ (l + 2)。
k = 3。答案不会超过1,因为从[l, r]中选两个数一定可以使得答案为1。我们关心的是三个数的异或和为0的情况。这3个数的最高位不可能相同(否则异或起来的最高位为1)。设这三个数为x, y, z(x < y <z),且y = 2 ^k + b, z = 2^ k + c, b < c。那么x ⊕ b ⊕ c = 0。为了让l ≤ x, y, z ≤ r,我们需要使得x尽量大而c尽量小。假设x ≥ 2 ^(k−1) ,那么可以推出z ≥ 2^( k−1) + 2^ k 。我们需要使x尽量大而c尽量小,那么令x = 2 ^(k − 1), z = 2 ^(k−1) + 2^ k ,可以得到y = z − 1。满足要求。那么如果x < 2^( k−1) 呢?我们会发现:此时z没必要≥ 2^ k ,因为取y = 2^( k−1 + b), z = 2 ^(k−1 )+ c依然满足条件。
所以枚举k并检查x = 2 ^k − 1, z = 2^ k + 2^( k−1) , y = z − 1这三个数是否在[l, r]内,如果在的话,就找到了一组解。
k = 4。如果r − l ≥ 4,那么答案一定为0。否则,枚举{x|x ∈ Z ∧ l ≤ x ≤ r}的所有子集,求异或和的最小值。
我的代码后面分类讨论部分写丑了。
//It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
LL l,r,len;
int k;
LL jilu1,jilu2,jilu3,num; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} inline LL getlong()
{
LL w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} inline LL check(){
LL t=;
while( ((1LL<<t)-1LL) < l) t++;
if(( (1LL<<t)+(1LL<<(t-1LL)) )<=r) return t;
return -;
} inline void work(){
LL now;
l=getlong(); r=getlong(); k=getint(); len=r-l+;
if(k==) printf("%I64d 1\n%I64d",l,l);
else if(k==) {
if(l&) {
if(r-l+==){
if((l^r)<l) printf("%I64d 2\n%I64d %I64d",l^r,l,r);
else printf("%I64d 1\n%I64d",l,l);
}
else printf("1 2\n%I64d %I64d",l+,l+);
}
else printf("1 2\n%I64d %I64d",l,l+);
}
else if(k==) {
if((now=check())!=-) {
printf("0 3\n"); printf("%I64d ",(1LL<<now)-1LL);
printf("%I64d %I64d",(1LL<<now)+(1LL<<(now-1LL))-1LL, (1LL<<now)+(1LL<<(now-1LL) ) );
}
else { printf("1 2\n"); if(l&) printf("%I64d %I64d",l+,l+); else printf("%I64d %I64d",l,l+); }
}
else{
if(l&) {
if(r-l+>) printf("0 4\n%I64d %I64d %I64d %I64d",l+,l+,l+,l+);
else{
now=l; num=; for(int i=;i<;i++) for(int j=i+;j<;j++) if( ((l+i)^(l+j)) < now) now=((l+i)^(l+j)),num=,jilu1=l+i,jilu2=l+j;
if( (l^(l+)^(l+) ) < now) now=(l^(l+)^(l+) ),num=,jilu1=l,jilu2=l+,jilu3=l+; if( (l^(l+)^(l+) ) < now) now=(l^(l+)^(l+) ),num=,jilu1=l,jilu2=l+,jilu3=l+;
if( (l^(l+)^(l+) ) < now) now=(l^(l+)^(l+) ),num=,jilu1=l,jilu2=l+,jilu3=l+; if( ((l+)^(l+)^(l+) ) < now) now=((l+)^(l+)^(l+)),num=,jilu1=l+,jilu2=l+,jilu3=l+;
if( (l^(l+)^(l+)^(l+) ) < now) now=(l^(l+)^(l+)^(l+) ),num=;
printf("%I64d %I64d\n",now,num); if(num==) printf("%I64d",now); else if(num==) printf("%I64d %I64d",jilu1,jilu2); else if(num==) printf("%I64d %I64d %I64d",jilu1,jilu2,jilu3); else printf("%I64d %I64d %I64d %I64d",l,l+,l+,l+);
}
}
else printf("0 4\n%I64d %I64d %I64d %I64d",l,l+,l+,l+);
}
} int main()
{
work();
return ;
}
codeforces 460D:Little Victor and Set的更多相关文章
- 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 --分类讨论+构造
题意:从区间[L,R]中选取不多于k个数,使这些数异或和尽量小,输出最小异或和以及选取的那些数. 解法:分类讨论. 设选取k个数. 1. k=4的时候如果区间长度>=4且L是偶数,那么可以构造四 ...
- codeforces 460D Little Victor and Set(构造、枚举)
最近的CF几乎都没打,感觉挺水的一个题,不过自己仿佛状态不在,看题解才知道做法. 输入l, r, k (1 ≤ l ≤ r ≤ 1012; 1 ≤ k ≤ min(106, r - l + 1)). ...
- Codeforces 460D Little Victor and Set(看题解)
Little Victor and Set 其他都很好求, 只有k == 3的时候很难受.. 我们找到第一个不大于l的 t, 答案为 l, 3 * t, (3 * t) ^ l 感觉好像是对的, 感觉 ...
- Codeforces 731C:Socks(并查集)
http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...
- Codeforces 747D:Winter Is Coming(贪心)
http://codeforces.com/problemset/problem/747/D 题意:有n天,k次使用冬天轮胎的机会,无限次使用夏天轮胎的机会,如果t<=0必须使用冬轮,其他随意. ...
- Codeforces 747C:Servers(模拟)
http://codeforces.com/problemset/problem/747/C 题意:有n台机器,q个操作.每次操作从ti时间开始,需要ki台机器,花费di的时间.每次选择机器从小到大开 ...
- Codeforces 749D:Leaving Auction(set+二分)
http://codeforces.com/contest/749/problem/D 题意:有几个人在拍卖场竞价,一共有n次喊价,有q个询问,每一个询问有一个num,接下来num个人从这次拍卖中除去 ...
- Codeforces 749B:Parallelogram is Back(计算几何)
http://codeforces.com/problemset/problem/749/B 题意:已知平行四边形三个顶点,求另外一个顶点可能的位置. 思路:用向量来做. #include <c ...
随机推荐
- 小技巧之Selenium如何切换到弹出的Tab页中
今天群里讨论了一个问题,如何将selenium的操作焦点切换到浏览器中新弹出来的Tab页中,正好对应到了昨天的那篇文章“小技巧之在浏览器中打开新的页签”.今天就带大家来解决这个问题: 先封装一个Tab ...
- hdu 1398 Square Coins 分钱币问题
Square Coins Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- JSP 连接数据库JDBC有一定的了解
JSP 连接数据库 本章节假设您已经对JDBC有一定的了解.在开始学习JSP数据库访问前,请确保JDBC环境已经正确配置. 首先,让我们按照下面的步骤来创建一个简单的表并插入几条简单的记录: 创建表 ...
- 8148 8168 中移植live55 出现except rtsp 中途莫名的断流
在解码中,接了浙江宇视的ipc相机,解码一般就挂了,vlc 也是中途断流.费解? vlc异常信息如下: packetizer_h264 warning: waiting for SPS/PPS pac ...
- XtraBackup备份mysql5.1.73
一.基础介绍 mysql5.1在源码中配备了两个版本的innodb存储引擎源码:innobase和innodb_plugin,编译安装的时候可以通过参数--with-plugins=innobase, ...
- html5小趣味知识点系列(一)required
都知道这个属性是检查你 是否填写了字段也就是说咱们不用判断输入的数值是否为空的情况了 但是这个属性一定要和form配合在一起使用单独的使用是不可以实现的 <!DOCTYPE html> & ...
- 转载 【iOS开发】网页JS与OC交互(JavaScriptCore) OC ----->JS
目标 本文介绍利用苹果在iOS7时发布的JavaScriptCore.framework框架进行js与OC的交互.我们想要达到的目标是: OC调用网页上的js方法 网页js调用APP中的OC方法 ...
- 关于PHP反射
本文实例讲述了PHP反射机制原理与用法.分享给大家供大家参考,具体如下: 反射 面向对象编程中对象被赋予了自省的能力,而这个自省的过程就是反射. 反射,直观理解就是根据到达地找到出发地和来源.比如,一 ...
- SDOI2012 Round1 day2 集合(set)解题报告
//=====================以上为官方题解==============// 数据略水,暴力枚举50. 把边按照升序排一遍,在询问,水过. #include<cstdio> ...
- vue中handsontable 使用
handsontable是目前在前端界最接近excel的插件,可以执行编辑,复制粘贴,插入删除行列,排序等复杂操作 1.安装模块包 npm install handsontable-pro @hand ...