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. tomcat配置启动文件

    修改tomcat到指定文件夹 conf -> server.xml <Host name="localhost" appBase="webapps" ...

  2. 破解EFCore扩展Dll --- Z.EntityFramework.Extensions.EFCore

    安装 Z.EntityFramework.Extensions.EFCore Install-Package Z.EntityFramework.Extensions.EFCore -Version ...

  3. DesignPattern系列__05开闭原则

    介绍 开闭原则是编程设计中最基本.最重要的原则. 定义:一个软件实体如类.方法和模块等,应该对扩展(提供方)开放,对修改(使用方)关闭.用抽象构建框架,用实现扩展细节. 也就是说,在需求发生新的变化时 ...

  4. S2:面向对象

    面向对象七大设计原则 1. 开闭原则 2. 里氏替换原则 3. 单一职责原则 4. 接口隔离原则 5. 依赖倒置原则 6. 迪米特原则 7.组合/聚合复用原则 原则一:(SRP:Single resp ...

  5. 派胜OA二次开发笔记(1)重写主界面

    最近从派胜OA 2018 升级到 2019,为了二次开发方便,索性花了两天,反向分析 PaiOA 2019 主界面程序,重写大部分代码,方便对菜单权限进行控制. 主界面/core/index.aspx ...

  6. Linux 常用命令及详解

    1.  type   :查询命令 是否属于shell解释器2.  help  : 帮助命令3.  man : 为所有用户提供在线帮助4.  ls  : 列表显示目录内的文件及目录-l    以长格式显 ...

  7. linux环境下搭建自动化Jenkins管理工具

    一.搭建一个jak--tomcat服务器到自己的linux服务器上 具体的服务器搭建这里可以参考华华大佬的博客:https://www.cnblogs.com/liulinghua90/p/46614 ...

  8. java文字转语音播报功能的实现方法

    java文字转语音播报功能的实现方法 一.pom.xml引入jar包依赖 <!-- https://mvnrepository.com/artifact/com.jacob/jacob 文字转语 ...

  9. 【Java笔记】【Java核心技术卷1】chapter3 D1JavaStandard

    package chapter3;/*有包名,命令行编译javac -d . 名字.java(注意空格)运行时用java chapter3.JavaStandard*/ public/*访问修饰符*/ ...

  10. Activiti6系列(3)- 快速体验

    一.部署启动activiti 1.部署,将两个war包拷贝到Tomcat下即可. 2.启动tomcat,访问http://127.0.0.1:8080/activiti-app 默认账号密码:admi ...