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 divisor (GCD), the XOR value, and the sum of two numbers. Kuro loves the game so much that he solves levels by levels day by day.
Sadly, he's going on a vacation for a day, and he isn't able to continue his solving streak on his own. As Katie is a reliable person, Kuro kindly asked her to come to his house on this day to play the game for him.
Initally, there is an empty array aa. The game consists of qq tasks of two types. The first type asks Katie to add a number uiui to aa. The second type asks Katie to find a number vv existing in aa such that ki∣GCD(xi,v)ki∣GCD(xi,v), xi+v≤sixi+v≤si, and xi⊕vxi⊕v is maximized, where ⊕⊕ denotes the bitwise XOR operation, GCD(c,d)GCD(c,d) denotes the greatest common divisor of integers cc and dd, and y∣xy∣x means xx is divisible by yy, or report -1 if no such numbers are found.
Since you are a programmer, Katie needs you to automatically and accurately perform the tasks in the game to satisfy her dear friend Kuro. Let's help her!
Input
The first line contains one integer qq (2≤q≤1052≤q≤105) — the number of tasks the game wants you to perform.
qq lines follow, each line begins with an integer titi — the type of the task:
- If ti=1ti=1, an integer uiui follow (1≤ui≤1051≤ui≤105) — you have to add uiui to the array aa.
- If ti=2ti=2, three integers xixi, kiki, and sisi follow (1≤xi,ki,si≤1051≤xi,ki,si≤105) — you must find a number vv existing in the array aa such that ki∣GCD(xi,v)ki∣GCD(xi,v), xi+v≤sixi+v≤si, and xi⊕vxi⊕v is maximized, where ⊕⊕ denotes the XOR operation, or report -1 if no such numbers are found.
It is guaranteed that the type of the first task is type 11, and there exists at least one task of type 22.
Output
For each task of type 22, output on one line the desired number vv, or -1 if no such numbers are found.
Examples
5
1 1
1 2
2 1 1 3
2 1 1 2
2 1 1 1
2
1
-1
10
1 9
2 9 9 22
2 3 3 18
1 25
2 9 9 20
2 25 25 14
1 20
2 26 26 3
1 14
2 20 20 9
9
9
9
-1
-1
-1
Note
In the first example, there are 5 tasks:
- The first task requires you to add 11 into aa. aa is now {1}{1}.
- The second task requires you to add 22 into aa. aa is now {1,2}{1,2}.
- The third task asks you a question with x=1x=1, k=1k=1 and s=3s=3. Taking both 11and 22 as vv satisfies 1∣GCD(1,v)1∣GCD(1,v) and 1+v≤31+v≤3. Because 2⊕1=3>1⊕1=02⊕1=3>1⊕1=0, 22is the answer to this task.
- The fourth task asks you a question with x=1x=1, k=1k=1 and s=2s=2. Only v=1v=1satisfies 1∣GCD(1,v)1∣GCD(1,v) and 1+v≤21+v≤2, so 11 is the answer to this task.
- The fifth task asks you a question with x=1x=1, k=1k=1 and s=1s=1. There are no elements in aa that satisfy the conditions, so we report -1 as the answer to this task.
题意:开始有个空数组,现在有两种操作:
(1,x):给数组加一个数a[]=x;
(2,x,k,s):在k|x的情况下,在数组中找一个a[i],满足a[i]<=s-x,而且k|a[i];现在需要找满足条件的a[],它异或x的值最大。
思路:最大异或,想到Trie树,树上可以贪心地走和x不相同的方向,使得异或最大,而且树上不难满足a[i]<=s-x的条件。
主要问题在于如何满足a[i]是k的倍数。如果操作1,假如a[i],把a[i]的每个的因子都走一遍Trie树,YY一下感觉复杂度很高,所以不敢写,而且不会用指针,空间是肯定要爆炸的。 然而标解就是如此:
时间上:O=1e5*lg1e5*18=2*1e7;ok!
空间上:和时间复杂度差不多大。
(不会写指针,所以比赛的时候只有暴力Trie写了发回溯来满足a[i]是k的倍数。。。第一次写指针,感觉可以入门了!奶思!
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
vector<int>G[maxn];
int vis[maxn];
void read(int &x){
x=; char c=getchar();
while(c>''||c<'') c=getchar();
while(c>=''&&c<='') x=(x<<)+(x<<)+c-'',c=getchar();
}
struct Trie
{
struct node
{
int Min,val;
node *ch[];
node(){
Min=maxn;
ch[]=ch[]=NULL;
}
}*rt[maxn];
void init()
{
for(int i=;i<maxn;i++)
for(int j=i;j<maxn;j+=i)
G[j].push_back(i);
for(int i=;i<maxn;i++) rt[i]=new node;
}
void insert(int x)
{
int Len=G[x].size();
for(int i=;i<Len;i++){
node *cur=rt[G[x][i]];
cur->Min=min(cur->Min,x);
for(int j=;j>=;j--){
if(cur->ch[x>>j&]==NULL) cur->ch[x>>j&]=new node;
cur=cur->ch[x>>j&];
cur->Min=min(cur->Min,x);
} cur->val=x;
}
}
int query(int x,int k,int s)
{
if(x%k!=) return -;
node *cur=rt[k];
if(cur->Min>s-x) return -;
for(int i=;i>=;i--){
int tb=x>>i&;
if(cur->ch[tb^]!=NULL&&cur->ch[tb^]->Min<=s-x) cur=cur->ch[tb^];
else cur=cur->ch[tb];
}
return cur->val;
}
}T;
int main()
{
//cout<<100000*18*log(100000);=2e7
int N,i,j,opt,x,k,s;
T.init();
read(N); while(N--){
read(opt);
if(opt==){
read(x);
if(!vis[x]) vis[x]=,T.insert(x);
}
else{
read(x); read(k); read(s);
printf("%d\n",T.query(x,k,s));
}
}
return ;
}
CodeForces979D:Kuro and GCD and XOR and SUM(Trie树&指针&Xor)的更多相关文章
- hdu 4825 Xor Sum trie树
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Proble ...
- HDU4825 Xor Sum —— Trie树
题目链接:https://vjudge.net/problem/HDU-4825 Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- LightOJ 1269 - Consecutive Sum Trie树
题意:给出一串序列,求区间连续异或值的最大和最小. 思路:如果不是出在专题里,想不到可以用字典树做.先求前缀异或值,转为二进制,加入Trie树中,如果要求最大,就是尽可能走和当前位数字相反的,这样异或 ...
- Xor - Trie树
题目描述 求一棵带边权的树的一条最大 Xor 路径的值.这里的"路径"不一定从根到叶子结点,中间一段路径只要满足条件也可以. 输入格式 第一行,一个整数 N ,表示一颗树有 N 个 ...
- 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 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( ...
- 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 979D Kuro and GCD and XOR and SUM
题意: 给出两种操作: 1.添加一个数字x到数组. 2.给出s,x,k,从数组中找出一个数v满足gcd(x,k) % v == 0 && x + v <= s && ...
- 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 ...
随机推荐
- django cookie session操作
Cookie是什么? cookie说的直白点就是保存在用户浏览器端的一个键值对,举个例子,你现在登录了京东商城,你把浏览器关闭之后,你再打开京东,你还是可以对你的账户继续操作,已经购买的商品,订单都是 ...
- git使用快速入门
git简介 git是一种版本控制器,更直白的说,团队开发的时候,管理代码使用的软件 git安装 Windows安装 到 https://www.git-for-windows.github.io/ 下 ...
- 读取编码器信息Python2.7和Python3.3版本差异及解决,一次订阅多次调用callback的解决
1. Python3.3以字节类型返回编码器信息,b'...',BUF: b'\xc3KOO\x00OO\x00OO\x00OO\x00OO\x00\x03\x00\x00\x00\x00\x99R\ ...
- Object源码
1.Object是所有类的父类,默认会继承Object. 2.Object类中常用的方法有:getClass().hashCode().equals().clone().toString().fina ...
- Java成长之路
怎样学习才能从一名Java初级程序员成长为一名合格的架构师,或者说一名合格的架构师应该有怎样的技术知识体系,这是不仅一个刚刚踏入职场的初级程序员也是工作三五年之后开始迷茫的老程序员经常会问到的问题.希 ...
- Ubuntu 16.04下更新Atom
在Ubuntu下Atom好像不会自动更新,但是可以通过这些方法去实现: 1.安装插件:https://atom.io/packages/up2date 2.使用apt源更新: sudo apt-get ...
- 【java】java 中 byte[]、File、InputStream 互相转换
========================================================================= 使用过程中,一定要注意close()掉各个读写流!! ...
- 【Android开发—智能家居系列】(一):智能家居原理
来到JCZB公司的第二天,就接到了开发类似于小米智能家庭APP的任务.组长让我在手机上安装上此款APP,给了我个小米智能插座,就让我开始了解需求.这便开启了我的智能家居旅程.说实话,我也真是out的无 ...
- Sentinel实现Redis高可用
实现目标: 一主两从,集群起始VIP在master上边,如果当前master挂了,sentinel自动选出一个slave当选master,并把VIP漂移到这台机器,然后把另一台slave指向的mast ...
- Android源代码解析之(三)-->异步任务AsyncTask
转载请标明出处:一片枫叶的专栏 上一篇文章中我们解说了android中的异步消息机制. 主要解说了Handler对象的使用方式.消息的发送流程等.android的异步消息机制是android中多任务处 ...