题意:

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

向其中塞进一个数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. Qbot回归,已感染5.4万台计算机

    Qbot回归,已感染5.4万台计算机 近日,BAESystems的安全人员发表了一篇关于Qbot网络感知蠕虫回归的调查报告,指出已经感染了5.4万台计算机. FreeBuf百科 Qbot蠕虫,也叫Qa ...

  2. java创建并配置多module的maven项目

    1 使用idea创建(推荐) 这篇博客写的特别好,很详细: https://blog.csdn.net/sinat_30160727/article/details/78109769 2 使用ecli ...

  3. avalonJS-源码阅读(三) VMODEL

    avalon的重头戏.终于要到我最期待的vmodel了. ps:这篇博文想做的全一点,错误少一点,所以会有后续的更新在这篇文章中. 状态:一稿 目录[-] avalon dom小结 数据结构 观察者模 ...

  4. mac 下安装pip

    pip是常用的Python包管理工具,类似于Java的maven.用python的同学,都离不开pip. 在新mac中想用home-brew安装pip时,遇到了一些小问题: bogon:~ wangl ...

  5. ipad webapp禁止长按选择

    1.禁止长按屏幕弹出对话框并选中文字 /*禁止长按选择文字事件*/ * { -webkit-touch-callout: none; -webkit-user-select: none; -khtml ...

  6. python网络编程-Select\Poll\Epoll异步IO

    首先列一下,sellect.poll.epoll三者的区别 select select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监视多个文件描述符的数组,当select ...

  7. python3中文转码方法

    python3中的转码,必须是byte类型的,str类型的会返回未定义方法. 示例代码如下 doc = pq(start_html.content) print("orig text=&qu ...

  8. Django配置https协议

    本博客来自https://blog.csdn.net/huplion/article/details/52892901 1.首先我们需要得到一张证书文件 参考:WINDOWS系统下创建自签名SSL证书 ...

  9. 实际工作与JAVA面试题

    1.String 和StringBuilder.StringBuffer 的区别? 答:Java 平台提供了两种类型的字符串:String和StringBuffer / StringBuilder,它 ...

  10. [Torch]的安装

    1 安装Torch 本文介绍Torch7的安装方法,因为本人安装Torch前安装了caffe,所以可能CUDA.cudnn.Blas等Torch可能需要用来的库的安装就不再重复介绍了,相关依赖出现问题 ...