题目链接: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. https填坑之旅

    Boss说,我们买了个权威证书,不如做全站式的https吧,让用户打开主页就能看到受信任的绿标.于是我们就开始了填坑之旅. [只上主域好不好?] 不好...console会报出一大堆warning因为 ...

  2. 设计模式:状态(State)模式

    设计模式:状态(State)模式 一.前言     状态模式在某些场合中使用是非常方便的,什么叫做状态,如果大家学过<编译原理>就会明白DFA M和NFA M,在确定有限状态机和非确定有限 ...

  3. ZT 或许你一辈子都是个小人物

    或许你一辈子都是个小人物 分类: 程序人生 2013-06-04 22:39 483人阅读 评论(2) 收藏 举报 程序人生 本文摘自:http://www.nowamagic.net/library ...

  4. std::string,std::vector,std::accumulate注意事项

    在用string做字符串拼接时,会发现随着string的增大越来越慢,原因主要是string(和vector)是基于现行内存的数据结构,在海量数据时,经常会申请新的一块内存,把原有的数据拷贝过去然后再 ...

  5. MySQL理解索引、添加索引的原则

    索引用于快速找出在某个列中有一特定值的行.不使用索引,MySQL必须从第1条记录开始然后读完整个表直到找出相关的行,还需要考虑每次读入数据页的IO开销.而如果采取索引,则可以根据索引指向的页以及记录在 ...

  6. Java 学习笔记1

    最近开始学习Java. <%@ page language="java" import="java.util.*" pageEncoding=" ...

  7. Python常用库之一:Numpy

    Numpy支持大量的维度数组和矩阵运算,对数组运算提供了大量的数学函数库! Numpy比Python列表更具优势,其中一个优势便是速度.在对大型数组执行操作时,Numpy的速度比Python列表的速度 ...

  8. centos7 firewall指定IP与端口访问(常用)

    1.启动防火墙 systemctl start firewalld.service 2.指定IP与端口 firewall-cmd --permanent --add-rich-rule="r ...

  9. iPhone 耳机在PC电脑上使用方法

    把主声道(Master)从正中间调整到最左或者最右就行了

  10. BootstrapTable返回结果集递增序号

    { 'field': '', 'title': '序号', 'align': 'center', 'width': '5%', 'formatter': function (value, row, i ...