题意:

给你一个空的可重集,支持以下操作:

向其中塞进一个数x(不超过100000),

询问(x,K,s):如果K不能整除x,直接输出-1。否则,问你可重集中所有是K的倍数的数之中,小于等于s-x,并且与x异或结果最大的数是多少(如果不存在这样的数,也输出-1)。

建立100000个二进制Trie,第i个Trie中存储i的所有倍数。

查询的时候,在Trie上从高位到低位贪心地找,如果从根到当前点的路径形成的数恰好与s-x相等,要从当前点进行一次dfs统计,看看当前子树中是否存在不超过s-x的数,如果不存在,返回-1。如果当前位恰好小于s-x的当前位,开启一个“限制解除”标记。如果已经开启了此标记,直接返回该点的子树大小是否大于零即可,不必dfs统计。如果没有开启此标记,并且当前位大于s-x的当前位,直接返回-1即可。

#include<cstdio>
using namespace std;
struct Node{
int ch[2];
Node(){
ch[0]=ch[1]=0;
}
};
Node* trees[100005];
int *sz[100005];
int tot[100005];
bool vis[100005];
void Insert(int o,int x){
int U=1;
++sz[o][1];
for(int i=18-1;i>=0;--i){
if(!trees[o][U].ch[(x>>i)&1]){
trees[o][U].ch[(x>>i)&1]=++tot[o];
}
U=trees[o][U].ch[(x>>i)&1];
++sz[o][U];
}
}
void Insert(int x){
if(vis[x]){
return;
}
vis[x]=1;
for(int i=1;i*i<=x;++i){
if(x%i==0){
if(i!=x/i){
Insert(i,x);
Insert(x/i,x);
}
else{
Insert(i,x);
}
}
}
}
bool jiechu;
bool check(int o,int Bit,int lim,int i,int U){
if(jiechu || Bit<(lim>>(i-1)&1)){
return sz[o][U];
}
if(Bit>(lim>>(i-1)&1)){
return 0;
}
int sum=0;
for(--i;i>=1;--i){
int limBit=(lim>>(i-1)&1);
if(limBit==1){
sum+=sz[o][trees[o][U].ch[0]];
}
U=trees[o][U].ch[limBit];
}
sum+=sz[o][U];
return sum>0;
}
int query(int o,int lim,int W){
jiechu=0;
int res=0,U=1;
for(int i=18;i>=1;--i){
int Bit=((W>>(i-1)&1)^1);
if(!check(o,Bit,lim,i,trees[o][U].ch[Bit])){
Bit^=1;
if(!check(o,Bit,lim,i,trees[o][U].ch[Bit])){
return -1;
}
}
if(Bit<(lim>>(i-1)&1)){
jiechu=1;
}
res+=(1<<(i-1))*Bit;
U=trees[o][U].ch[Bit];
}
return res;
}
int q;
int main(){
int op,x,K,s;
for(int i=1;i<=100000;++i){
tot[i]=1;
trees[i]=new Node[20*100000/i];
sz[i]=new int[20*100000/i];
for(int j=0;j<20*100000/i;++j){
sz[i][j]=0;
}
}
scanf("%d",&q);
for(;q;--q){
scanf("%d%d",&op,&x);
if(op==1){
Insert(x);
}
else{
scanf("%d%d",&K,&s);
if(x%K!=0 || s<=x){
puts("-1");
continue;
}
printf("%d\n",query(K,s-x,x));
}
}
return 0;
}

【Trie】【枚举约数】Codeforces Round #482 (Div. 2) D. Kuro and GCD and XOR and SUM的更多相关文章

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

  2. Codeforces Round #482 (Div. 2) C Kuro and Walking Route

    C. Kuro and Walking Route time limit per test 2 seconds memory limit per test 256 megabytes input st ...

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

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

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

  6. 【枚举】【贪心】Codeforces Round #482 (Div. 2) B. Treasure Hunt

    题意:给你3个字符串,3个人各对自己的字符串执行n轮操作,每一次选择一个字符变为任意一个和原来不同的字符.最后问你谁能使自己的串中的任意重复子串出现的次数最大化. 显然只需关注字符而非子串. 枚举每个 ...

  7. Codeforces Round #482 (Div. 2) :B - Treasure Hunt

    题目链接:http://codeforces.com/contest/979/problem/B 解题心得: 这个题题意就是三个人玩游戏,每个人都有一个相同长度的字符串,一共有n轮游戏,每一轮三个人必 ...

  8. Codeforces Round #482 (Div. 2) B题

    题目链接:http://codeforces.com/contest/979/problem/B B. Treasure Hunt time limit per test1 second memory ...

  9. 【枚举】Codeforces Round #394 (Div. 2) C. Dasha and Password

    纪念死去的智商(虽然本来就没有吧……) 三重循环枚举将哪三个fix string作为数字.字母和符号位.记下最小的值就行了. 预处理之后这个做法应该是O(n^3)的,当然完全足够.不预处理是O(n^3 ...

随机推荐

  1. XSS报警机制(前端防火墙:第二篇)

    XSS报警机制(前端防火墙:第二篇) 在第一章结尾的时候我就已经说了,这一章将会更详细的介绍前端防火墙的报警机制及代码.在一章出来后,有人会问为什么不直接防御,而是不防御报警呢.很简单,因为防御的话, ...

  2. 【codeforces】【比赛题解】#861 CF Round #434 (Div.2)

    本来是rated,现在变成unrated,你说气不气. 链接. [A]k-凑整 题意: 一个正整数\(n\)的\(k\)-凑整数是最小的正整数\(x\)使得\(x\)在十进制下末尾有\(k\)个或更多 ...

  3. C#基础学习之StreamReader和StreamWriter

    StreamReader和StreamWriter操作字符的 FileStream操作字节的 //使用StreamReader读取文件 using (StreamReader sr=new Strea ...

  4. C#基础学习之装箱,拆箱

    装箱,拆箱这两个的大条件是有继承关系. 装箱:值类型转换为引用类型 拆箱:引用类型转换为之类 但是要注意大条件. string (引用类型)  int(值类型)   这个转换因为没有继承关系,内存中没 ...

  5. 【hdu6334】【2018Multi-University-Training Contest04】Problem C. Problems on a Tree

    维护1边的联通块和2边的联通块,合并的时候直接启发式合并. cdqz的大爷好强啊. #include<bits/stdc++.h> #define lson (o<<1) #d ...

  6. Javascript中的Callback方法浅析

    什么是callback?  回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用为调用它所指向的函数时,我们就说这是回调函数.回调函数不是由该函数 ...

  7. Flask:初见

    Windows 10家庭中文版,Python 3.6.4 从Flask官网开始学起. 介绍 Flask是一个Python的Web开发微框架,基于Werkzeug.Jinja2模块(and good i ...

  8. caffe卷积操作

  9. MyEclipse自带且常用的快捷键和自己定义的快捷键方法步骤

    1.MyEclipse自带且常用的快捷键 内容提示(补全): Alt+/    导包快捷键: Ctrl+Shift+o    格式化代码: Ctrl+Shift+f    行代码位置上下调换: Alt ...

  10. HDU 3068 最长回文(manacher模板题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 题目大意:求字符串s中最长的回文子串 解题思路:manacher模板 代码 #include&l ...