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\) 的二进制形式列出:

\[\begin{cases} A=00...00100...00100...00\\
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 题解的更多相关文章

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

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

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

  3. Victor and World(spfa+状态压缩dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5418 Victor and World Time Limit: 4000/2000 MS (Java/ ...

  4. 【HDU5421】Victor and String(回文树)

    [HDU5421]Victor and String(回文树) 题面 Vjudge 大意: 你需要支持以下操作: 动态在前端插入一个字符 动态在后端插入一个字符 回答当前本质不同的回文串个数 回答当前 ...

  5. HDU 5418 Victor and World 允许多次经过的TSP

    题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5418 bestcoder(中文): http://bestcoder.hdu.edu.cn ...

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

  7. HDU5421 Victor and String 和 APIO2014 回文串

    两道差不多的题,都是回文自动机right集合处理相关. Victor and String Victor loves to play with string. He thinks a string i ...

  8. CSUST 集训队选拔赛题解

    选拔赛的题解,~~~ 题目链接:请点击 A题 素数筛 + 线段树(树状数组) 先用素数筛打表,然后线段树更新,遍历求出值,O(1)查询即可 AC代码: /*num数组 是把记录 数是否存在 存在即为1 ...

  9. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  10. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

随机推荐

  1. ShardingSphere5入门到实战

    ShardingSphere5入门到实战 第01章 高性能架构模式 互联网业务兴起之后,海量用户加上海量数据的特点,单个数据库服务器已经难以满足业务需要,必须考虑数据库集群的方式来提升性能.高性能数据 ...

  2. python接口自动化封装导出excel方法和读写excel数据

    一.首先需要思考,我们在页面导出excel,用python导出如何写入文件的 封装前需要确认python导出excel接口返回的是一个什么样的数据类型 如下:我们先看下不对返回结果做处理,直接接收数据 ...

  3. Federated Learning002

    联邦学习笔记--002 2022.11.26周六 今天学习了联邦学习中又一篇很经典的论文--Federated Machine Learning: Concept and Applications(联 ...

  4. Struts2 小知识点

    配置struts.xml文件,选择开发模式 在实际应用开发或者是产品部署的时候,对应着两种模式: 开发模式(devMode):此时 DevMode=ture: 产品模式(proMode):此时 Dev ...

  5. Linux 命令:gpasswd 管理用户组

    工作中经常需要将用户加入docker组,可执行如下操作: sudo gpasswd -a ec2-user docker newgrp # 不用加sudo gpasswd gpasswd -h Usa ...

  6. upload-libs通关攻略

    pass01 第一关是一个前端验证,只要把Javascript禁止再上传就可以绕过了. 上传成功,到上传的地址查看结果如下: pass-02 这是一个MIME绕过,用bp抓包后修改文件类型即可将下面这 ...

  7. Django: You are trying to add a non-nullable field 'name' to mainnav without a default; we can't do that (the database needs something to populate existing rows).

    错误原因: 语句中缺少默认值 class Main(models.Model): img = models.CharField(max_length=255) name = models.CharFi ...

  8. [jenkins]简介与安装

    前言 jenkins是一种代码构建平台,一般用于CI/CD中的CI部分,当然也可以集成CD功能. 安装 环境 IP:192.168.0.10 系统:centos 7 快速安装步骤 官网下载jenkin ...

  9. Unity UGUI的Button组件的介绍及使用

    UGUI的Button(按钮)组件的介绍及使用 1. 什么是UGUI的Button组件? UGUI(Unity GUI)是Unity引擎中的一套用户界面系统,Button(按钮)是其中的一个常用组件. ...

  10. 02.中台框架前台项目 admin.ui.plus 学习-介绍与简单使用

    中台框架前台项目 admin.ui.plus 的初识 基于 vue3.x + CompositionAPI setup 语法糖 + typescript + vite + element plus + ...