codeforces 979D Kuro and GCD and XOR and SUM
题意:
给出两种操作:
1.添加一个数字x到数组。
2.给出s,x,k,从数组中找出一个数v满足gcd(x,k) % v == 0 && x + v <= s && (x xor v 最大),如果没有,输出-1.
思路:
有两种做法。
第一种,首先用若干个set存因子中有k的数字。
然后每次在set[k]中二分找到大于s-x的第一个数,然后从大到小开始找,假设sum为当前x xor v的最大值,那么当当前的*it +x < sum,那么就直接break,原理是因为a ^ b <= a + b。
这个没有优化之前是会tle,但是加了优化之后比字典树跑得快了许多。。。
第二种,对于每一个数,都把它添加到它的因子的01字典树当中去,然后根据x xor v最大这个经典问题,找 xor 最大,字典树是个好东西。
代码1;
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <set>
#include <math.h>
using namespace std;
const int N = 1e5 + ;
set<int> mmp[N];
int main()
{
int n;
scanf("%d",&n);
while (n--)
{
int t;
scanf("%d",&t);
if (t == )
{
int u;
scanf("%d",&u);
int m = sqrt(1.0 * u);
for (int i = ;i <= m;i++)
{
if (u % i == )
{
mmp[i].insert(u);
mmp[u/i].insert(u);
}
}
}
else
{
int x,k,s;
scanf("%d%d%d",&x,&k,&s);
int sum = -;
int ans = -;
if (x % k)
{
printf("%d\n",ans);
continue;
}
auto it = mmp[k].upper_bound(s-x);
if (mmp[k].empty() || it == mmp[k].begin())
{
printf("%d\n",ans);
continue;
}
--it;
for (;it != mmp[k].begin();--it)
{
if (sum > x + *it) break;
if (sum < (x ^ *it))
{
ans = *it;
sum = x ^ *it;
}
}
if (sum < (x ^ *it))
{
ans = *it;
sum = x ^ *it;
}
printf("%d\n",ans);
}
}
return ;
}
代码2:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
#include <set>
using namespace std;
const int N = 1e5 + ;
set<int> g[N];
struct node
{
int mi;
struct node* bit[];
node()
{
mi = N;
bit[] = bit[] = nullptr;
}
};
node *a[N];
void update(int x,int u)
{
node* cu = a[x];
cu -> mi = min(cu -> mi,u);
for (int i = ;i >= ;i--)
{
int b = u >> i & ;
if (cu -> bit[b] != nullptr)
{
cu = cu -> bit[b];
cu -> mi = min(cu -> mi,u);
}
else
{
cu -> bit[b] = new node();
cu = cu -> bit[b];
cu -> mi = min(cu -> mi,u);
}
}
}
int que(int x,int k,int s)
{
node *cu = a[k];
if (x % k || cu -> mi > s - x) return -;
int ret = cu -> mi;
for (int i = ;i >= ;i--)
{
int b = x >> i & ;
if (cu -> bit[b^] != nullptr && cu -> bit[b^] -> mi + x <= s)
{
cu = cu -> bit[b^];
ret = cu -> mi;
}
else
{
cu = cu -> bit[b];
ret = cu -> mi;
}
}
return ret;
}
int main()
{
int q;
for (int i = ;i < N;i++)
{
for (int j = i;j < N;j += i)
{
g[j].insert(i);
}
}
for (int i = ;i < N;i++) a[i] = new node();
scanf("%d",&q);
while (q--)
{
int t;
scanf("%d",&t);
if (t==)
{
int u;
scanf("%d",&u);
for (auto x : g[u])
{
update(x,u);
}
}
else
{
int x,s,k;
scanf("%d%d%d",&x,&k,&s);
int ans = que(x,k,s);
printf("%d\n",ans);
}
}
return ;
}
codeforces 979D Kuro and GCD and XOR and SUM的更多相关文章
- 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(异或和,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( ...
- 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 ...
- 【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 ...
随机推荐
- Material Design之CollapsingToolbarLayout使用
CollapsingToolbarLayout作用是提供了一个可以折叠的Toolbar,它继承至FrameLayout,给它设置layout_scrollFlags,它可以控制包含在Collapsin ...
- LeetCode之“数学”:Rectangle Area
题目链接 题目要求: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle i ...
- thrift实现HDFS文件操作
thrift 文件如下 namespace java com.pera.file.transform struct File{ 1:string path , 2:string co ...
- XMPP系列(五)---文件传输
xmpp中发送文件和接收文件的处理有些不太一样,接收文件处理比较简单,发送稍微复杂一些. 首先需要在XMPPFramework.h中添加文件传输类 //文件传输 //接收文件 #import &quo ...
- 【作业2.0】HansBug的5-7次OO作业分析与小结,以及一些个人体会
不知不觉又做了三次作业,容我在本文胡言乱语几句2333. 第五次作业 第五次作业是前面的电梯作业的多线程版本,难度也有了一些提升.(点击就送指导书) 类图 程序的类图结构如下: UML时序图 程序的逻 ...
- 十六进制颜色转换为iOS可以用的UIColor
// // UIColor+Transformation.h // ContactApp // // Created by 袁冬冬 on 15/9/11. // Copyright (c) 2 ...
- MvcSiteMapProvider 自定义模板
MvcSiteMapProvider 介绍文字就省了,直接访问官方站点吧. 官方站点:https://github.com/maartenba/MvcSiteMapProvider 默认的模板文件 ...
- Delphi 项目总结
Delphi 项目总结 随着项目的失败,这些天一直在总结失败的原因,到底是为什么? 一.技术层面 1.少用指针类型,多用类. 虽然指针类型能有效的节约内 ...
- Python高阶函数之 - 装饰器
高阶函数: 1. 函数名可以作为参数传入 2. 函数名可以作为返回值. python装饰器是用于拓展原来函数功能的一种函数 , 这个函数的特殊之处在于它的返回值也是一个函数 , 使用pyth ...
- Golang之Context的使用
转载自:http://www.nljb.net/default/Golang%E4%B9%8BContext%E7%9A%84%E4%BD%BF%E7%94%A8/ 简介 在golang中的创建一个新 ...