题意:

开始有个空集合,现在有两种操作:

$(1,x)$:给集合加一个数$x$,$x \leq 10^5$;

$(2,x,k,s)$:在集合中找一个$a$,满足$a \leq s-x$,而且$k|gcd(a,x)$;现在需要找满足条件的$a$,它异或$x$的值最大。$x,k,s \leq 10^5$

操作数$q \leq 10^5$

这道题就是看你想到一个算法有没有去算算实际复杂度

我们发现,对于所有在$[1,10^5]$的$i$,$10^5$之内的$i$的倍数的个数和,并不是很大,只有$2*10^7$左右

然后就维护$10^5$个trie就好了……

//Serene
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
#define ll long long
#define db double
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
const int maxn=2e5+7,maxm=2e7+7,W=1e5,U=16,INF=0x3f3f3f3f;
int n,root[maxn],tot=W;
int son[maxm][2],minnum[maxm];
bool vis[maxn]; char cc; ll ff;
template<typename T>void read(T& aa) {
aa=0;cc=getchar();ff=1;
while((cc<'0'||cc>'9')&&cc!='-') cc=getchar();
if(cc=='-') ff=-1,cc=getchar();
while(cc>='0'&&cc<='9') aa=aa*10+cc-'0',cc=getchar();
aa*=ff;
} int prime[maxn],totp,num[maxn];
bool ok[maxn];
void get_p() {
For(i,2,W) {
if(!ok[i]) prime[++totp]=i,num[i]=i;
For(j,1,totp) {
if(prime[j]>W/i) break;
ok[i*prime[j]]=1;
num[i*prime[j]]=prime[j];
if(i%prime[j]==0) break;
}
}
} void add(int pos,int x) {
minnum[pos]=min(minnum[pos],x);
int r;
Rep(i,U,0) {
r=(x>>i)&1;
if(!son[pos][r]) minnum[son[pos][r]=++tot]=x;
pos=son[pos][r]; minnum[pos]=min(minnum[pos],x);
}
} int zz[maxn];
void get_add(int x) {
if(vis[x]) return; vis[x]=1;
int s=1,t=1,p,now,y,o=x; zz[1]=1;
while(x!=1) {
p=num[x]; now=0; y=1;
while(x%p==0) x/=p,now++;
For(i,1,now) {
y*=p;
For(j,1,s) zz[++t]=zz[j]*y;
}
s=t;
}
For(i,1,t) add(zz[i],o);
} int get_ans(int x,int pos,int v) {
if(x%pos||minnum[pos]>v) return -1;
int r;
Rep(i,U,0) {
r=(x>>i)&1;
if(minnum[son[pos][r^1]]<=v) pos=son[pos][r^1];
else pos=son[pos][r];
}
return minnum[pos];
} int main() {
read(n); int op,k,x,v;
get_p();
For(i,0,W) minnum[i]=INF;
For(i,1,n) {
read(op); read(x);
if(op==1) get_add(x);
else {
read(k); read(v);
printf("%d\n",get_ans(x,k,v-x));
}
}
return 0;
}

  

cf round 482D Kuro and GCD and XOR and SUM的更多相关文章

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

  2. 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( ...

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

  4. D. Kuro and GCD and XOR and SUM

    Kuro is currently playing an educational game about numbers. The game focuses on the greatest common ...

  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) : Kuro and GCD and XOR and SUM (寻找最大异或值)

    题目链接:http://codeforces.com/contest/979/problem/D 参考大神博客:https://www.cnblogs.com/kickit/p/9046953.htm ...

  7. 【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,并 ...

  8. codeforces 979D Kuro and GCD and XOR and SUM

    题意: 给出两种操作: 1.添加一个数字x到数组. 2.给出s,x,k,从数组中找出一个数v满足gcd(x,k) % v == 0 && x + v <= s && ...

  9. cf979d Kuro and GCD and XOR and SUM

    set做法 正解是trie-- 主要是要学会 \(a\ \mathrm{xor}\ b \leq a+b\) 这种操作 #include <iostream> #include <c ...

随机推荐

  1. OpenCASCADE 平面求交

    OpenCASCADE 平面求交 eryar@163.com OpenCASCADE提供了类IntAna_QuadQuadGeo用来计算两个二次曲面quadric(球面.圆柱面.圆锥面及平面,平面是二 ...

  2. PAT甲级——A1092 To Buy or Not to Buy【20】

    Eva would like to make a string of beads with her favorite colors so she went to a small shop to buy ...

  3. 《DSP using MATLAB》Problem 7.35

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...

  4. Synchronized理解及用法

    加锁: 1.同步实例方法,锁是当前实例对象 2.同步类方法,锁的是当前类对象 3.同步代码块,锁是括号里面的对象 原理: JVM内置锁通过synchronized使用,通过内部对象Monitor(监视 ...

  5. python collections模块 之 ChainMap

    ChainMap提供了一种多个字典整合的方式,它没有去合并这些字典,而是将这些字典放在一个 maps (一个列表)里,内部实现了很多 dict 的方法,大部分 dict 的方法,ChainMap 都能 ...

  6. PKU--3628 Bookshelf 2(01背包)

    题目http://poj.org/problem?id=3628 分析:给定一堆牛的高度,把牛叠加起来的高度超过牛棚的高度. 且是牛叠加的高度与牛棚高度之差最小. 把牛叠加的高度看作是背包的容量,利用 ...

  7. Compile_Netgen_WITH_OpenCascade

    title: Compile_Netgen_WITH_OpenCascade date: 2016-04-23 21:14:42 tags: 结合OCCT编译Netgen date: 2016-04- ...

  8. 重装一次CM的坑爹记录

    今天同事要对测试环境进行降级(测试高于生产所以要求降级),自己不经常搞运维,但是无奈测试环境没运维管理只能自己上了. 流程和遇到问题按数字表示. 1.重装CM(clouder manager)这个过程 ...

  9. 图解nginx配置负载均衡

    1. 在Linux上准备两份tomcat 2. 修改两份tomcat的端口号 修改的端口如图所示: 3. 启动两个tomcat服务器 4. 修改两个服务器上的主页方便测试区分 5. 在nginx配置文 ...

  10. 未A,或用水法,或不熟的题

    今天是2017.11.25 1. 用栈实现dfs JZOJ_senior 3467 2. 链表加堆或线段树乱搞 JZOJ_senior 3480 3. 求每个边所在的奇环.偶环 JZOJ_senior ...