题目链接: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 (寻找最大异或值)的更多相关文章

  1. Codeforces Round #482 (Div. 2)

    D. Kuro and GCD and XOR and SUM 字典树真好玩... 牛老板提供的思路:建1e5个 字典树,每个数插入到以它的因子为根所在的字典树中,这样就实现了整除,当然gcd(k, ...

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

  3. 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( ...

  4. 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, ...

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

  6. D. Kuro and GCD and XOR and SUM

    Kuro is currently playing an educational game about numbers. The game focuses on the greatest common ...

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

  8. 【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,并 ...

  9. 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时,此路不通.路 ...

随机推荐

  1. tree 向上查找(更新删除后页面的数据)

    需求 : 根据选择的id,需要找到一整条tree,id以及id数据的子集都已被删除(向下查找-----上一篇笔记),此时需要更新页面的数据(向上查找) //知道最底层的节点的id,查找满足id的整个t ...

  2. TIA Portal 和 scout 之间的驱动器地址分配

    TIA Portal集成了scout.在使用simotion控制器时,分配驱动装置的地址可能会碰到问题. 解决方法: 1)在配置驱动时,TIA Portal软件的语言需要选择为应为中文 2)unico ...

  3. GPRS研究(3):NO CARRIER错误的含义解释

    NO CARRIER(必须是大写)是一个由猫向其所附着的设备(典型的就是一个计算机)发来的文本响应信息,表示猫没有连接到远程系统.NO CARRIER是由Hayes指令集定义的,由于Hayes猫的普及 ...

  4. 关于函数指针与c++多态

    原文  https://www.cnblogs.com/zhchngzng/p/4013031.html 虚函数是实现多态的重要元素,请看: class A { public: void a0(){c ...

  5. webpack导学

    随着前端工程越来越复杂,单独建几个文件写业务代码,这样的方式已经无法保证项目的可维护性了. 所以我们就想把不同的逻辑拆成模块,然后分开引入这些模块,每个模块自己做自己的事情,这样就可以保证项目的可维护 ...

  6. 关闭 XXXXX 前你必须关闭所有会话框

    这个问题应该是和Microsoft管理控制台(Microsoft Management Console,MMC)有关系 我是在使用 任务计划程序 时碰到这个问题的,网上也有其他人在使用 事件查看器 的 ...

  7. ajax 三级联动商品分类(转载)

    转载  自  jines     http://www.cnblogs.com/lijinblogs/p/5759399.html 思路分析:效果:当页面加载时,利用ajax异步向后台请求数据,加载一 ...

  8. PHP数组 转 对象/对象 转 数组

    /** * 数组 转 对象 * * @param array $arr 数组 * @return object */ function array_to_object($arr) { if (gett ...

  9. APICloud的App怎么在手机上测试运行

    方式一: 工程->右键->云编译自定义 AppLoader,如图: 点击[编译iOS自定义loader]或者[编译Android自定义loader],会生成相应的二维码,手机扫描二维码点击 ...

  10. idea删除module

    先remove , 再点击delete