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. QScintilla下载与编译

    你好,我是大贺! Pou光明  大家好,我又回来了~~ 之前和大家分享的是在c/c++中通过python c api嵌入python解释器,主体都是和python相关的.其实最终要和大家分享的是如何做 ...

  2. restapi(3)- MongoDBEngine : MongoDB Scala编程工具库

    最近刚好有同事在学习MongoDB,我们讨论过MongoDB应该置于服务器端然后通过web-service为客户端提供数据的上传下载服务.我们可以用上节讨论的respapi框架来实现针对MongoDB ...

  3. spring学习笔记之---bean管理

    bean管理(xml) (一)spring的工厂类 FileSystemXmlApplicationContext 读取磁盘配置文件 (二)bean实例化的三种方式 (1)使用类构造器实例化(默认无参 ...

  4. 用vue2.0+vuex+vue-router+element-ui+mockjs实现后台管理系统的实践探索

    A magical vue element touzi admin. 效果演示地址 更多demo展示 分支说明 master分支:前后端统一开发的版本:可以用于学习nodejs+mongodb+exp ...

  5. .net core使用MQTT

    废话不多说,我们来直接实践…… 一.搭建mqtt控制台服务端 新建一个.net core控制台项目,然后使用Nuget添加MQTTnet包,我这里使用2.4版本,注意不同版本,代码写法不相同,如下图 ...

  6. 0x33 同余

    目录 定义 同余类与剩余系 费马小定理 欧拉定理 证明: 欧拉定理的推论 证明: 应用: 定义 若整数 $a$ 和整数 $b$ 除以正整数 $m$ 的余数相等,则称 $a,b$ 模 $m$ 同余,记为 ...

  7. js 数组对象深拷贝

    js 数组对象深拷贝 结论:对象的拷贝不能采用直接赋值的方式. 背景 踩过的坑如下: formData本来是父组件传过来的,但是我不想直接用,于是我直接赋值给一个formDataCopy的对象. 但是 ...

  8. jmh源码解析-整体架构

    我理解的jmh运行架构图 生成字节码,字节码负责维护测试的状态和调用被测试的方法 默认在fork的进程中进行测试,可以配置多个fork进程,以减少误差 通过线程池,提交每个迭代的测试任务,任务执行后, ...

  9. 【win】【qt5打包】【qt程序打包成一个可执行文件(带图标任何win都可以运行哦)】

    [前言] 业务需求将qt程序打包成win可执行文件.咱是做linux的,奈何用的麒麟系统,程序运行在win,好嘛,重新在win qtcreator编译后打包呗. [目标] 1.给qt程序添加一个图标. ...

  10. vue.js中ref及$refs的使用及讲解

    关于ref和$refs的用法及讲解,vue.js中文社区( https://cn.vuejs.org/v2/api/#ref )是这么讲解的: ref 被用来给元素或子组件注册引用信息,引用信息将会注 ...