Little Victor and Set 题解
题目大意
在 \([l,r]\) 中选不超过 \(k\) 个相异的数使得异或和最小,输出方案。
思路分析
分类讨论:
- 当 \(k=1\) 时:
显然选 \(l\) 是最优的。
- 当 \(r-l+1\le 10\) 时:
直接 \(O(n2^n)\) 暴力枚举每个数选或不选即可。
(判了这个之后后面的很多讨论会简单很多。)
- 当 \(k=2\) 时:
我们发现两个不同的数的异或和最小为 \(1\),因为当且仅当两个数相同时异或和为 \(0\)。
所以我们可以在 \([l,r]\) 内任找一个偶数 \(x\),那么方案就是 \(x\) 和 \(x+1\)。
(因为 \(r-l+1>10\) 所以一定能找到)
- 当 \(k\ge 4\) 时:
容易发现对于任意 \(k\in N\),均有 \(4k\oplus(4k+1)\oplus(4k+2)\oplus (4k+3)=0\),所以我们只需要任取一个 \(k\) 就行了。
(因为 \(r-l+1>10\) 所以一定能找到)
- 当 \(k=3\) 时:
首先,我们可以按照 \(k=2\) 的方法得到异或和为 \(1\) 的答案,我们只需要考虑是否存在异或和为 \(0\) 的方案即可。
我们枚举 \(i,j\,(i>j)\),构造 \(A=2^i+2^j,B=2^i+2^j-1,C=2^{j+1}-1\),容易发现 \(A\oplus B\oplus C=0,A>B>C\),考虑证明这样构造的合法性:
证明:
我们只需要证明如果存在异或和为 \(0\) 的选法,一定存在一种选法满足以上的形式即可。
将 \(A,B,C\) 的二进制形式列出:
B=00...00100...00011...11\\
C=00...00000...00111...11\\
\qquad\;\;\;\;{\color{red}^1}\;\;\;\;\; i\;\;\;\;\;{\color{red}^2}\;\;\;\;j\;\;\;\;{\color{red}^3}\end{cases}\]
当 \(A\) 固定时,\(C\) 不可能更大,因为当 \(C\) 增大时,\({\color{red}2}\) 部分会多出若干 \(1\),那么 \(B\) 就必须也在 \(2\) 部分增加若干 \(1\),那么 \(B\) 就大于 \(A\) 了,不符合题设。
若 \(A\) 的二进制表示中 \(1\) 的个数大于 \(2\),那么 \({\color{red}3}\) 部分会多出若干 \(1\),\(B,C\) 中必要有一个在对应的位置去掉若干个 \(1\) 来满足异或和为 \(0\) 的条件,故要么 \(B\) 变小要么 \(C\) 变小,如果这时的 \(A,C\) 在 \([l,r]\) 的范围内,那么之前的 \(A,C\) 也一定在 \([l,r]\) 的范围内。
若 \(A\) 的二进制表示中只有一个 \(1\),那么不存在满足条件的 \(B,C\)。
代码
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int N = 200200, V = 40;
#define inf 0x3f3f3f3f3f3f3f3f
#define int long long
int l, r, k;
vector <int> ans;
void add(int x){
ans.push_back(x);
}
template <typename types, typename... Args> void add(types x, Args... args){
add(x), add(args...);
}
signed main(){
cin >> l >> r >> k;
if (k == 1) add(l);
else if (r - l + 1 <= 10) {
int len = r - l + 1, minans = inf, way = 0;
for (int i = 1; i < (1ll << len); i ++) {
int ans = 0, cnt = 0;
for (int j = 0; j < len; j ++)
if (i >> j & 1) {
ans ^= (l + j);
cnt ++;
}
if (cnt <= k && ans < minans) {
minans = ans;
way = i;
}
}
for (int i = 0; i < len; i ++)
if (way >> i & 1) add(l + i);
}
else if (k == 2) {
if (l & 1) l ++;
add(l, l + 1);
}
else if (k == 3) {
int flag = 0;
for (int i = 0; i <= V && !flag; i ++)
for (int j = i + 1; j <= V; j ++) {
int x = (1ll << i) | (1ll << j), y = x - 1, z = (x ^ y);
if (x <= r && z >= l) {add(x, y, z); flag = 1; break;}
}
if (!flag) {
if (l & 1) l ++;
add(l, l + 1);
}
}
else if (k >= 4) {
for (int i = l; i <= l + 4; i ++)
if (i % 4 == 0) {
add(i, i + 1, i + 2, i + 3); break;
}
}
int res = 0;
for (auto it : ans) res ^= it;
cout << res << '\n';
cout << ans.size() << '\n';
for (auto it : ans) cout << it << ' ';
return 0;
}
Little Victor and Set 题解的更多相关文章
- Codeforces 460D Little Victor and Set(看题解)
Little Victor and Set 其他都很好求, 只有k == 3的时候很难受.. 我们找到第一个不大于l的 t, 答案为 l, 3 * t, (3 * t) ^ l 感觉好像是对的, 感觉 ...
- CF460D Little Victor and Set (找规律)
D - Little Victor and Set Codeforces Round #262 (Div. 2) D D. Little Victor and Set time limit per t ...
- Victor and World(spfa+状态压缩dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5418 Victor and World Time Limit: 4000/2000 MS (Java/ ...
- 【HDU5421】Victor and String(回文树)
[HDU5421]Victor and String(回文树) 题面 Vjudge 大意: 你需要支持以下操作: 动态在前端插入一个字符 动态在后端插入一个字符 回答当前本质不同的回文串个数 回答当前 ...
- HDU 5418 Victor and World 允许多次经过的TSP
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5418 bestcoder(中文): http://bestcoder.hdu.edu.cn ...
- 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 ...
- HDU5421 Victor and String 和 APIO2014 回文串
两道差不多的题,都是回文自动机right集合处理相关. Victor and String Victor loves to play with string. He thinks a string i ...
- CSUST 集训队选拔赛题解
选拔赛的题解,~~~ 题目链接:请点击 A题 素数筛 + 线段树(树状数组) 先用素数筛打表,然后线段树更新,遍历求出值,O(1)查询即可 AC代码: /*num数组 是把记录 数是否存在 存在即为1 ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
随机推荐
- TiDB简述及TiKV的数据结构与存储
1 概述 TiDB 是 PingCAP 公司自主设计.研发的开源分布式关系型数据库,是一款同时支持在线事务处理与在线分析处理 (Hybrid Transactional and Analytical ...
- YOLOv6在LabVIEW中的推理部署(含源码)
前言 YOLOv6 是美团视觉智能部研发的一款目标检测框架,致力于工业应用.如何使用python进行该模型的部署,官网已经介绍的很清楚了,但是对于如何在LabVIEW中实现该模型的部署,笔者目前还没有 ...
- 记一次 zabbix item became not supported 问题处理
现象 配置了一个自定义监控项,超过 5m nodata 就会告警,最近触发了两次.通过日志查看,有一对相隔10分钟的告警: 时间t0: item xxx became not support: Tim ...
- SSH远程主机执行命令:s2c
#!/bin/bash ip=$1 ip_num=$(echo $ip | awk -F\. '{print NF}') if [ $ip_num -eq 2 ]; then ip=192.168.$ ...
- linux 脚本:iptables-secure.sh
#!/bin/bash # 2022.2.28 by dewan # secutiry configuration. usage () { echo "$0 start # setup se ...
- 一文详述流媒体传输网络MediaUni
一张「多元融合」的网络. 黄海宇|演讲者 大家好,我是阿里云视频云的黄海宇,今天分享主题是MediaUni--面向未来的流媒体传输网络设计与实践. 下面我将会从应用对流媒体传输网络的要求.MediaU ...
- JavaScript的Map和WeakMap
熟悉JavaScript的Map和WeakMap Map Map的键/值可以是任何类型 基本API 初始化映射: //使用new关键字和Map构造函数进行初始化 const m1 = new Map( ...
- k8s发布应用
前言 首先以SpringBoot应用为例介绍一下k8s的发布步骤. 1.从代码仓库下载代码,比如GitLab: 2.接着是进行打包,比如使用Maven: 3.编写Dockerfile文件,把步骤2产生 ...
- Programming abstractions in C阅读笔记:p127-p129
<Programming Abstractions In C>学习第51天,p127-p129,总结如下: 一.技术总结 1. string library 掌握常用函数如strlen,s ...
- AgileConfig-1.7.0 发布,支持 SSO 🎉🎉🎉
AgileConfig 已经好久好久没有更新过比较大的功能了.一是 AgileConfig 本身的定位就是比较轻量,不想集成太多的功能.二是比较忙(懒).但是本次升级给大家带来了一个比较有用的功能 S ...