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.html:
解题心得:
- 题目给了你很多条件,具体起来就是输入三个数x,k,s,在数列中找到一个数num,要求:1. GCD(x, num)%k == 0; 2. x + num <= s;3. num异或x最大
- 刚开始一看数据量这么大,条件这么多怎么搞。其实前面两个条件是用来剪枝的。首先可以开很多个set,将每一个数放在他因子的set中,这样在面对第一个条件的时候就可以直接在set[k]里面找目标数。然后我们从set[k]中找最大的num,使得x+num<=s,这里寻找第一个数可以在set中使用二分,然后向前便利记录下异或值的Max,如果遍历过程中num+x<=Max直接跳出,因为两个数的异或值<=两个数的和。
- 其实在寻找异或值最大的问题其实就是用Trie,这个题也可以用Trie,但是节点太多了,只能动态开辟内存,每次new内存速度太慢了。
按照题意剪枝代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+;
set <int> se[maxn];
set <int> :: iterator iter; int main() {
int n;
scanf("%d",&n);
while(n--) {
int ope;
scanf("%d",&ope);
if(ope == ) {
int num;
scanf("%d",&num);
int sq = sqrt(1.0*num);
for(int i=;i<=sq;i++) {
if(num%i == ) {
se[i].insert(num);
se[num/i].insert(num);
}
}
} else {
int x,k,s,ans = -, Max = -;
scanf("%d%d%d",&x,&k,&s);
if(x%k) {
printf("-1\n");
continue;
}
iter = se[k].upper_bound(s-x);
if(se[k].empty() || iter==se[k].begin()) {
printf("%d\n", ans);
continue;
}
iter--;
while(iter!=se[k].begin()) {
if(*iter + x < Max)
break;
int temp = (*iter)^x;
if(temp > Max){
Max = temp;
ans = *iter;
}
iter--;
}
int temp = (*iter)^x;
if(temp > Max)
ans = *iter;
printf("%d\n",ans);
}
}
return ;
}
Trie代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+; struct node{
int Min;
node* bit[];
node() {
bit[] = bit[] = nullptr;
Min = maxn;
}
}; node* head[maxn];
int n; set <int> se[maxn];
set <int> ::iterator iter; void init() {
scanf("%d",&n);
for(int i=;i<maxn;i++) {
for(int j=i;j<maxn;j+=i){
se[j].insert(i);
}
}
for(int i=;i<maxn;i++)
head[i] = new node();
} void insert_tree(int va, int u) {
node *cur = head[va];
cur->Min = min(cur->Min, u);
for(int i=;i>=;i--) {
int b = (u>>i)&;
if(cur->bit[b] == nullptr){
cur->bit[b] = new node();
cur = cur->bit[b];
cur->Min = min(cur->Min, u);
} else {
cur = cur->bit[b];
cur->Min = min(cur->Min, u);
}
}
} int query(int x, int k, int s) {
if(head[k]->Min > s -x || x%k != )
return -;
node *cur;
cur = head[k];
int res = cur->Min;
for(int i=;i>=;i--){
int b = (x>>i)&;
int Xor = b^;
if(cur->bit[Xor] != nullptr && cur->bit[Xor]->Min + x <= s) {
res = cur->bit[Xor]->Min;
cur = cur->bit[Xor];
} else {
cur = cur->bit[b];
res = cur->Min;
}
}
return res;
} int main() {
init();
while(n--) {
int ope;
scanf("%d",&ope);
if(ope == ) {
int u;
scanf("%d",&u);
for(iter=se[u].begin();iter!=se[u].end();iter++) {
int temp = *iter;
insert_tree(temp, u);
}
} else {
int x, k, s;
scanf("%d%d%d",&x,&k,&s);
int ans = query(x, k, s);
printf("%d\n",ans);
}
}
return ;
}
Trie
Codeforces Round #482 (Div. 2) : Kuro and GCD and XOR and SUM (寻找最大异或值)的更多相关文章
- Codeforces Round #482 (Div. 2)
D. Kuro and GCD and XOR and SUM 字典树真好玩... 牛老板提供的思路:建1e5个 字典树,每个数插入到以它的因子为根所在的字典树中,这样就实现了整除,当然gcd(k, ...
- 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 ...
- 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( ...
- 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, ...
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip
地址:http://codeforces.com/contest/766/problem/E 题目: E. Mahmoud and a xor trip time limit per test 2 s ...
- 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 ...
- 【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,并 ...
- Codeforces Round #482 (Div. 2) C 、 Kuro and Walking Route(dfs)979C
题目链接:http://codeforces.com/contest/979/problem/C 大致题意 给出n个点,有n-1个边将他们链接.给出x,y,当某一路径中出现x....y时,此路不通.路 ...
随机推荐
- python 生成器&迭代器
列表生成式 要生成[1x1, 2x2, 3x3, ..., 10x10]>>> [x * x for x in range(1, 11)]for循环后面还可以加上if判断>&g ...
- plus.webview.create mui.openWindow区别是什么呢
create 只是创建这个webview,但是不显示,而且同一个页面.同一个id你甚至能重复创建多个(严重消耗性能,作死...),其实mui里面已经封装了这个方法 mui.preload(),并且 ...
- Intellii IDEA 中快速补全main方法:psvm
psvm可以快速补全main方法 效果:
- log4j框架logger的继承关系以及使用场景
log4j框架logger的继承关系以及使用场景 log4j日志框架logger是存在继承关系的,我们一般都会在log4j.properties文件中定义log4j.rootLogger.其他所有lo ...
- 【CSS】易错
1.外边距默认是透明的,因此不会遮挡其后的任何元素.2.背景应用于由内容和内边距.边框组成的区域.3.外边距可以是负值,而且在很多情况下都要使用负值的外边距.4.不要给元素添加具有指定宽度的内边距,而 ...
- Integer类小细节随笔记录
先看一段简单的代码: Integer v1 = Integer.valueOf(12); Integer v2 = Integer.valueOf(12); Integer v3 = Integer. ...
- PAT——1008. 数组元素循环右移问题
一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN-1)变换为(AN-M …… AN-1 A0 ...
- 安全清理Xcode 缓存垃圾
安全清理Xcode缓存垃圾方法: 经验证,Xcode缓存垃圾存储在~/Library/Developer/Xcode/DerivedData/路径下,缓存和Xcode的版本有关(如同一台Mac安装2个 ...
- C# 泛型的协变和逆变 (转载)
1. 可变性的类型:协变性和逆变性 可变性是以一种类型安全的方式,将一个对象当做另一个对象来使用.如果不能将一个类型替换为另一个类型,那么这个类型就称之为:不变量. 协变和逆变是两个相互对立的概念: ...
- linux VMware使用
contos7 配置网络 使用NAT模式连接本地网络 进入Linux机器配置网络 vi /etc/sysconfig/network-scripts/ifcfg-eth0 TYPE=EthernetP ...