Kuro and GCD and XOR and SUM

题意:给你一个空数组。 然后有2个操作, 1是往这个数组里面插入某个值, 2.给你一个x, k, s。要求在数组中找到一个v,使得k|gcd(x,v)  (即gcd(x,v)是k的倍数,v+x <= k, x ^ v的值最大。

题解:XOR亦或问题是经典的题目,一般都是用01字典树去处理。 然后需要满足条件1, 所以我们可以对于每一个点建立一个字典树,每次添加数的时候都往这个数的因子添加这个值,这样我们直接访问对应的k就可以找到答案了。

代码:

 #include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9+;
const int N = 1e5+;
vector<int> son[N];
struct Node{
int Min;
Node * p[];
Node(){
Min = INF;
p[] = p[] = nullptr;
}
}*rt[N];
bool vis[N];
void init(){
for(int i = ; i < N; i++){
rt[i] = new Node();
for(int j = i; j < N; j+=i){
son[j].pb(i);
}
}
}
void Add(int k, int u){
Node *tmp = rt[k];
tmp -> Min = min(tmp -> Min, u);
for(int i = ; i >= ; i--){
int id = u>>i & ;
if(tmp -> p[id] == nullptr)
tmp -> p[id] = new Node();
tmp = tmp -> p[id];
tmp -> Min = min(tmp -> Min, u);
}
}
int Query(int x, int k, int s){
Node *tmp = rt[k];
if(x%k != || tmp->Min+x > s)
return -;
int ret = ;
for(int i = ; i >= ; i--){
int id = x >> i & ;
if(tmp -> p[id^] != nullptr && tmp -> p[id^] -> Min + x <= s){
tmp = tmp -> p[id^];
ret += (id^) << i;
}
else {
tmp = tmp -> p[id];
ret += id << i;
}
}
return ret;
}
int main(){
///Fopen;
init();
int q, t, u, x, s, k;
scanf("%d", &q);
while(q--){
scanf("%d", &t);
if(t == ){
scanf("%d", &u);
if(!vis[u]){
vis[u] = ;
for(int k : son[u]){
Add(k, u);
}
}
}
else{
scanf("%d%d%d", &x, &k, &s);
printf("%d\n", Query(x,k,s));
}
}
return ;
}

CodeForces 979 D Kuro and GCD and XOR and SUM的更多相关文章

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

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

  3. D. Kuro and GCD and XOR and SUM

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

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

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

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

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

  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. cf979d Kuro and GCD and XOR and SUM

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

  9. cf round 482D Kuro and GCD and XOR and SUM

    题意: 开始有个空集合,现在有两种操作: $(1,x)$:给集合加一个数$x$,$x \leq 10^5$; $(2,x,k,s)$:在集合中找一个$a$,满足$a \leq s-x$,而且$k|gc ...

随机推荐

  1. 【iOS】copy 关键字

    以前没注意过 iOS 的 copy, nonatomic, assign, weak, strong 等关键字. 偏偏今天遇到了一个问题,恰恰是关键字的问题,如图: 之前用的是 assign, 没有用 ...

  2. poj 1455 Crazy tea party

    这道题第一眼看去很难,其实不然,短短几行代码就搞定了. 说一下大概思路,如果是排成一排的n个人,如 1 2 3 4 5 6 7 8 我们要变成 8 7 6 5 4 3 2 1 需要交换 28次,找规律 ...

  3. Find out "Who" and "Where"

    Yesterday a friend of mine Kirby came to me with a smartphone and she wanted me to do her a favor. S ...

  4. Java 设置PDF文档浏览偏好

    在查看PDF文档时,可进行一些浏览偏好设置,例如是否全屏浏览.隐藏或显示菜单栏/工具栏.设置页面布局模式等,下面将通过Java编程的方式来演示如何设置. 使用工具: Free Spire.PDF fo ...

  5. WinForm开发中通用附件管理控件设计开发参考

    1.引言 在WinForm开发中,文件附件的管理几乎在任何一个应用上都会存在,是一个非常通用集中的公共模块.我们日常记录会伴随着有图片.文档等附件形式来展现,如果为每个业务对象都做一个附件管理,或者每 ...

  6. 保存localStorage并访问

    将用户的输入保存至localStorage对象的属性中,这些属性在再次访问时还会继续保持在原位置. 如果你在浏览器中按照fil://URL的方式直接打开本地文件,则文法在某些浏览器中使用存储功能(比如 ...

  7. 【游记】NOIP2019前传

    声明 我的游记是一个完整的体系,如果没有阅读过往届文章,阅读可能会受到障碍. ~~~上一篇游记的传送门~~~ 前言 比完赛后,我沉浸在胜利中长达半个月,而后才清醒过来,意识到自己需要为NOIP2019 ...

  8. weblogic10.3.6漏洞修改方案

    1.CVE-2018-2628漏洞 CVE-2018-2628漏洞利用的第一步是与weblogic服务器开放在服务端口上的T3服务建立socket连接,可通过控制T3协议的访问来临时阻断攻击行为. W ...

  9. 对Java中HashCode方法的深入思考

    前言 最近在学习 Go 语言,Go 语言中有指针对象,一个指针变量指向了一个值的内存地址.学习过 C 语言的猿友应该都知道指针的概念.Go 语言语法与 C 相近,可以说是类 C 的编程语言,所以 Go ...

  10. MyEclipse下安装FreeMark插件

    现在大多人人喜欢用FreeMark模板.但是这个模板在myeclipse或者是eclipse下却是不能只能提示,一大堆只是没有颜色区分的显示在哪里.万能天国总是有办法. 点我去官网下载(比较慢) 我的 ...