题意:

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

向其中塞进一个数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. iOS学习笔记(2)— UIView用户事件响应

    UIView除了负责展示内容给用户外还负责响应用户事件.本章主要介绍UIView用户交互相关的属性和方法. 1.交互相关的属性 userInteractionEnabled 默认是YES ,如果设置为 ...

  2. PyText

    Facebook开源了自家工程师们一直在用的NLP建模框架PyText.这个框架,每天要为Facebook旗下各种应用处理超过10亿次NLP任务,Facebook AI的工业级NLP开源框架.(简化部 ...

  3. imperva命令行查看流量值大小

    watch -d -n 1 /proc/hades/status echo clear > /proc/hades/status     //清除这些记录

  4. Tslib移植与分析【转】

    转自:http://blog.csdn.net/water_cow/article/details/7215308 目标平台:LOONGSON-1B开发板(mips32指令集)编译平台:x86PC-- ...

  5. python3中内建函数map()与reduce()的使用方法

    map()的使用    map()的使用方法形如map(f(x),Itera).对,它有两个参数,第一个参数为某个函数,第二个为可迭代对象.如果不懂什么是函数,不懂什么是可迭代对象没关系,记住下面的例 ...

  6. 25 The Go image/draw package go图片/描绘包:图片/描绘包的基本原理

    The Go image/draw package  go图片/描绘包:图片/描绘包的基本原理 29 September 2011 Introduction Package image/draw de ...

  7. 【前端vue开发】vue子调父 $emit (把子组件的数据传给父组件)

    ps:App.vue 父组件 Hello.vue 子组件 <!--App.vue :--> <template> <div id="app"> ...

  8. day09作业

    一.填空题 1.方法 2.堆内存 3.构造方法 4.this 5.this 6.static 7.使用类名进行访问 8.package import class 9.关键字 10.lang 二.选择题 ...

  9. 常见四大类型视频接线DP、HDMI、DVI、VGA的比较

    如今是新的“视”界,生活中总与各种屏幕打交道,难免会遇到选择视频接线的问题,要想搞清楚这点,我们只要通过了解现今常用的几种视频接线就会有个大致的认识.   281VGA.DVI.HDMI三种视频信号接 ...

  10. Eclipse 配置语言环境

    一.打开https://www.eclipse.org/babel/downloads.php 选择一下版本的Bable(通天塔) 选择 解压 打开Eclipse 软件 选择Help->inst ...