Codeforces Round #482 (Div. 2) : Kuro and GCD and XOR and SUM (寻找最大异或值)
题目链接:http://codeforces.com/contest/979/problem/D
参考大神博客:https://www.cnblogs.com/kickit/p/9046953.html:
解题心得:
- 题目给了你很多条件,具体起来就是输入三个数x,k,s,在数列中找到一个数num,要求:1. GCD(x, num)%k == 0; 2. x + num <= s;3. num异或x最大
- 刚开始一看数据量这么大,条件这么多怎么搞。其实前面两个条件是用来剪枝的。首先可以开很多个set,将每一个数放在他因子的set中,这样在面对第一个条件的时候就可以直接在set[k]里面找目标数。然后我们从set[k]中找最大的num,使得x+num<=s,这里寻找第一个数可以在set中使用二分,然后向前便利记录下异或值的Max,如果遍历过程中num+x<=Max直接跳出,因为两个数的异或值<=两个数的和。
- 其实在寻找异或值最大的问题其实就是用Trie,这个题也可以用Trie,但是节点太多了,只能动态开辟内存,每次new内存速度太慢了。
按照题意剪枝代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+;
set <int> se[maxn];
set <int> :: iterator iter; int main() {
int n;
scanf("%d",&n);
while(n--) {
int ope;
scanf("%d",&ope);
if(ope == ) {
int num;
scanf("%d",&num);
int sq = sqrt(1.0*num);
for(int i=;i<=sq;i++) {
if(num%i == ) {
se[i].insert(num);
se[num/i].insert(num);
}
}
} else {
int x,k,s,ans = -, Max = -;
scanf("%d%d%d",&x,&k,&s);
if(x%k) {
printf("-1\n");
continue;
}
iter = se[k].upper_bound(s-x);
if(se[k].empty() || iter==se[k].begin()) {
printf("%d\n", ans);
continue;
}
iter--;
while(iter!=se[k].begin()) {
if(*iter + x < Max)
break;
int temp = (*iter)^x;
if(temp > Max){
Max = temp;
ans = *iter;
}
iter--;
}
int temp = (*iter)^x;
if(temp > Max)
ans = *iter;
printf("%d\n",ans);
}
}
return ;
}
Trie代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+; struct node{
int Min;
node* bit[];
node() {
bit[] = bit[] = nullptr;
Min = maxn;
}
}; node* head[maxn];
int n; set <int> se[maxn];
set <int> ::iterator iter; void init() {
scanf("%d",&n);
for(int i=;i<maxn;i++) {
for(int j=i;j<maxn;j+=i){
se[j].insert(i);
}
}
for(int i=;i<maxn;i++)
head[i] = new node();
} void insert_tree(int va, int u) {
node *cur = head[va];
cur->Min = min(cur->Min, u);
for(int i=;i>=;i--) {
int b = (u>>i)&;
if(cur->bit[b] == nullptr){
cur->bit[b] = new node();
cur = cur->bit[b];
cur->Min = min(cur->Min, u);
} else {
cur = cur->bit[b];
cur->Min = min(cur->Min, u);
}
}
} int query(int x, int k, int s) {
if(head[k]->Min > s -x || x%k != )
return -;
node *cur;
cur = head[k];
int res = cur->Min;
for(int i=;i>=;i--){
int b = (x>>i)&;
int Xor = b^;
if(cur->bit[Xor] != nullptr && cur->bit[Xor]->Min + x <= s) {
res = cur->bit[Xor]->Min;
cur = cur->bit[Xor];
} else {
cur = cur->bit[b];
res = cur->Min;
}
}
return res;
} int main() {
init();
while(n--) {
int ope;
scanf("%d",&ope);
if(ope == ) {
int u;
scanf("%d",&u);
for(iter=se[u].begin();iter!=se[u].end();iter++) {
int temp = *iter;
insert_tree(temp, u);
}
} else {
int x, k, s;
scanf("%d%d%d",&x,&k,&s);
int ans = query(x, k, s);
printf("%d\n",ans);
}
}
return ;
}
Trie
Codeforces Round #482 (Div. 2) : Kuro and GCD and XOR and SUM (寻找最大异或值)的更多相关文章
- Codeforces Round #482 (Div. 2)
D. Kuro and GCD and XOR and SUM 字典树真好玩... 牛老板提供的思路:建1e5个 字典树,每个数插入到以它的因子为根所在的字典树中,这样就实现了整除,当然gcd(k, ...
- Codeforces 979 D. Kuro and GCD and XOR and SUM(异或和,01字典树)
Codeforces 979 D. Kuro and GCD and XOR and SUM 题目大意:有两种操作:①给一个数v,加入数组a中②给出三个数x,k,s:从当前数组a中找出一个数u满足 u ...
- CodeForces 979 D Kuro and GCD and XOR and SUM
Kuro and GCD and XOR and SUM 题意:给你一个空数组. 然后有2个操作, 1是往这个数组里面插入某个值, 2.给你一个x, k, s.要求在数组中找到一个v,使得k|gcd( ...
- CF 979D Kuro and GCD and XOR and SUM(异或 Trie)
CF 979D Kuro and GCD and XOR and SUM(异或 Trie) 给出q(<=1e5)个操作.操作分两种,一种是插入一个数u(<=1e5),另一种是给出三个数x, ...
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip
地址:http://codeforces.com/contest/766/problem/E 题目: E. Mahmoud and a xor trip time limit per test 2 s ...
- D. Kuro and GCD and XOR and SUM
Kuro is currently playing an educational game about numbers. The game focuses on the greatest common ...
- CodeForces979D:Kuro and GCD and XOR and SUM(Trie树&指针&Xor)
Kuro is currently playing an educational game about numbers. The game focuses on the greatest common ...
- 【Trie】【枚举约数】Codeforces Round #482 (Div. 2) D. Kuro and GCD and XOR and SUM
题意: 给你一个空的可重集,支持以下操作: 向其中塞进一个数x(不超过100000), 询问(x,K,s):如果K不能整除x,直接输出-1.否则,问你可重集中所有是K的倍数的数之中,小于等于s-x,并 ...
- Codeforces Round #482 (Div. 2) C 、 Kuro and Walking Route(dfs)979C
题目链接:http://codeforces.com/contest/979/problem/C 大致题意 给出n个点,有n-1个边将他们链接.给出x,y,当某一路径中出现x....y时,此路不通.路 ...
随机推荐
- Hbase集群部署及shell操作
本文详述了Hbase集群的部署. 集群部署 1.将安装包上传到集群并解压 scp hbase-0.99.2-bin.tar.gz mini1:/root/apps/ tar -zxvf hbase-0 ...
- C语言main函数的参数
在Windows下使用gcc编译器: 1.首先介绍下MinGW MinGW(Minimalist GNU for Windows),又称mingw32,是将GCC编译器和GNU Binutils移植到 ...
- IOS AFN (第三方请求)
什么是AFN全称是AFNetworking,是对NSURLConnection.NSURLSession的一层封装虽然运行效率没有ASI高,但是使用比ASI简单在iOS开发中,使用比较广泛 AFN的g ...
- 「bzoj 3944: Sum」
题目 杜教筛板子了 #include<iostream> #include<cstring> #include<cstdio> #include<cmath& ...
- PHP----练习------球队列表
题目:页面上有一个ul球队列表当鼠标移动到某个li上的时候改行背景颜色变红,当点击某个li的时候,让该li之前的所有li背景色变黄,之后的所有li背景色变蓝.自己不变色. <!DOCTYPE h ...
- Vue通过input筛选数据
<div id="app"> <input v-model='search' /> <ul> <li v-for="item i ...
- Linux网络配置&进程管理
原理图 查看ip和网关
- Linux的vi&vim
vi和vim的基本介绍 1.基本介绍 所有的 Linux 系统都会内建 vi 文本编辑器. Vim 具有程序编辑的能力,可以看做是Vi的增强版本,可以主动的以字体颜色辨别 语法的正确性,方便程序设计. ...
- ROBOCOPY——Windows 的可靠文件复制
复制指定类型文件 (-s :含子目录 不包括空目录) 复制所有 (-e :含子目录 包括空目录) 复制指定成层级内的 (-lev:n 仅复制源目录树的前 n 层) 复制排除给定类型后的 (-xf) ...
- NodeJS学习日记--VSCode下调试
在vscode中打开项目文件夹 点击左侧的调试菜单,在打开的页面中点击下拉框并点击添加配置 在弹出框中选择 node.js vscode 会自动在项目文件夹下添加.vscode文件夹,并创建launc ...