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时,此路不通.路 ...
随机推荐
- Spring学习之-各注解的含义总结
注解配置 @ComponentScan("spittr.web"):/在加载Spring上下文时,会扫描spittr.web包查找组件 @ComponentScan注解扫描的组件有 ...
- 如何使用Flashfxp上传下载文件
一.首先您本地电脑需要安装flashfxp软件,您可以通过百度搜索下载. 二.我们打开flashfxp,然后在右上角点击“会话”,再点击“快速连接”,如下图. 三.弹出“如下图”窗口.请输入FTP连接 ...
- 入门摄影——尼康DX
学习链接 单反应当怎样入门? - Williams的回答 - 知乎 [摄影教程]尼康数码单反相机使用视频教程_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili 图像品质与图像大小 图像品质:暂选JP ...
- sort给文件按照大小排序
ls -l|sort -n -k5 -n 表示以数值排序-k5 表示以第几列排序还可以用 -t参数指定行内容的分隔符 参考链接:http://www.cnblogs.com/myd620/p/6002 ...
- Powershell split file
#网上所得非原创 function split($inFile, $outPrefix, [Int32] $bufSize){ $stream = [System.IO.File]::OpenRead ...
- BZOJ 3289: Mato的文件管理 【莫队 + 树状数组】
任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=3289 3289: Mato的文件管理 Time Limit: 40 Sec Memory ...
- c#用链表来存储并读取写好的配置文件
别用arraylist,效果没有list好(因为要装箱拆箱,所以会影响性能) 使用list,那我们就来先声明一个List 1) 声明 List<元素类型> myList = new Lis ...
- springmvc需要掌握的面试知识
1:讲下Spr ingMvc和Struts1,Struts2的比较的优势 性能上Struts1>SpringMvc>Struts2 开发速度上SpringMvc和Struts2差不多,比 ...
- Anaconda安装与常用命令及方法(深度学习入门1)
Anaconda是一个软件发行版,它附带了 conda.Python 和 150 多个科学包及其依赖项. 安装Anaconda Anaconda分为Linux.Windows.Mac等版本,去 htt ...
- 使用jQuery实现option的上移和下移
基本思路: 上移:(1)获取当前选中的元素的索引值 (2)判断当前元素是否为第一个元素 (3)如果是,则不执行上移操作,如果不是,则则调用ins ...