题意:

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

向其中塞进一个数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. Android手动回收bitmap,引发Canvas: trying to use a recycled bitmap处理

    在做Android的开发的时候,在ListView 或是 GridView中需要加载大量的图片,为了避免加载过多的图片引起OutOfMemory错误,设置了一个图片缓存列表 Map<String ...

  2. JS函数的几种用法

    1.正常使用:

  3. VMware 增加硬盘ubuntu

    http://blog.csdn.net/Timsley/article/details/50742755

  4. 数据库名(DB_NAME)、实例名(Instance_name)、以及操作系统环境变量(ORACLE_SID)

    数据库名(DB_NAME).实例名(Instance_name).以及操作系统环境变量(ORACLE_SID) 在ORACLE7.8数据库中只有数据库名(db_name)和数据库实例名(instanc ...

  5. PE文件结构及其加载机制

    一.PE文件结构 PE即Portable Executable,是win32环境自身所带的执行体文件格式,其部分特性继承自Unix的COFF(Common Object File Format)文件格 ...

  6. Wood Cut

    Given n pieces of wood with length L[i] (integer array). Cut them into small pieces to guarantee you ...

  7. Tslib步骤以及出现问题的解决方案【转】

    转自:http://forum.eepw.com.cn/thread/267828/1 嵌入式设备中触摸屏使用非常广泛,但触摸屏的坐标和屏的坐标是不对称的,需要校准.校准广泛使用的是开源的tslib. ...

  8. Python缓存技术,装x新高度。

    一段非常简单代码 普通调用方式 def console1(a, b): print("进入函数") return (a, b) print(console1(3, 'a')) pr ...

  9. 从一个局长使用BS系统的无奈看测试点

    今天我点名买了个B/S系统,听说只要有浏览器就能用.我最讨厌装客户端了,用浏览器就是方便啊. 下面就是我使用这个系统碰到的麻烦事: 我登录失败的时候没有任何提示,这没什么,反正提示也只是说失败…… 进 ...

  10. 学习网站总结->

    慕课大巴网:这是一个学习各类技术视频的网站 慕课大巴网点我-> 吾爱破解: 这是一个破解各类软件的网站 吾爱破解点我-> 鸠摩搜书:可以搜一些免费的书,我喜欢的都能搜到 鸠摩搜书点我-&g ...