CodeForces 979 D 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的更多相关文章
- 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 ...
- 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, ...
- D. Kuro and GCD and XOR and SUM
Kuro is currently playing an educational game about numbers. The game focuses on the greatest common ...
- 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 ...
- 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 ...
- codeforces 979D Kuro and GCD and XOR and SUM
题意: 给出两种操作: 1.添加一个数字x到数组. 2.给出s,x,k,从数组中找出一个数v满足gcd(x,k) % v == 0 && x + v <= s && ...
- 【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,并 ...
- cf979d Kuro and GCD and XOR and SUM
set做法 正解是trie-- 主要是要学会 \(a\ \mathrm{xor}\ b \leq a+b\) 这种操作 #include <iostream> #include <c ...
- 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 ...
随机推荐
- ld: warning: directory not found for option ''
iOS开发中经常遇到这样的警告,如图所示: 原因是存在未用到的目录. 解决方法:选择Build Settings,找到Search Paths中的Library Search Paths,如下图 删除 ...
- javascript基础入门知识点整理
学习目标: - 掌握编程的基本思维 - 掌握编程的基本语法 typora-copy-images-to: media JavaScript基础 HTML和CSS 京东 课前娱乐 众人皆笑我疯癫,我笑尔 ...
- Extjs的textfield的颜色设置和出现的问题笔记
Ext.getCmp('alarmsLevelVal').setFieldStyle('background-color:#ff0000;background-p_w_picpath: none; ' ...
- 前端面试 js 你有多了解call,apply,bind?
函数原型链中的 apply,call 和 bind 方法是 JavaScript 中相当重要的概念,与 this 关键字密切相关,相当一部分人对它们的理解还是比较浅显,所谓js基础扎实,绕不开这些基础 ...
- Docker入门学习笔记
Docker 什么是Docker 虚拟化技术 在计算机中,虚拟化是一种资源管理技术,将计算机中的各种实体资源如:CPU.硬盘.内存等予以抽象.转换后呈现出来打破实体结构间的不可切割的障碍,使用户可以比 ...
- JavaFX 集成 Sqlite 和 Hibernate 开发爬虫应用
目录 [隐藏] 0.1 前言: 0.2 界面 0.3 Maven 环境 0.4 项目结构 0.5 整合 Hibernate 0.5.1 SQLiteDialect.java 数据库方言代码 0.5.2 ...
- 4如何用PHP给MySQL数据库添加记录
首先连接数据库(依旧用第二篇的方法) 假设数据库表里只有id,name,email三列 添加以下代码 $inputemail=写你要的email;$inputname=写你要的name;//先设定你要 ...
- JAVA基础知识(三):input.nextLine() 和input.next()
next()方法在读取内容时,会过滤掉有效字符前面的无效字符,对输入有效字符之前遇到的空格键.Tab键或Enter键等结束符,next()方法会自动将其过滤掉:只有在读取到有效字符之后,next()方 ...
- WebGL简易教程(二):向着色器传输数据
目录 1. 概述 2. 示例:绘制一个点(改进版) 1) attribute变量 2) uniform变量 3) varying变量 3. 结果 4. 参考 1. 概述 在上一篇教程<WebGL ...
- 搞定java String校招面试题
今天大致的阅读了String类的源码,并刷了常见的面试题,在此做个笔记. 面试题一:判断下列程序运行结果 package String_test; public class test_1 { publ ...