题意:

给出两种操作:

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的更多相关文章

  1. 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, ...

  2. 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 ...

  3. 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( ...

  4. D. Kuro and GCD and XOR and SUM

    Kuro is currently playing an educational game about numbers. The game focuses on the greatest common ...

  5. 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 ...

  6. 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 ...

  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. (十)弹出框Alert与ActionSheet

    第一种方式:中间弹窗 从中间弹出的窗口称为AlertView. 可以设置多个按钮,取消按钮会放在对右端或者最下端,按钮超过两个,会竖着排列. UIAlertView *alert = [[[UIAle ...

  2. How To Get Log, Trace Files In OA Framework Pages And Concurrent Request Programs

    Goal   Solution   References APPLIES TO: Oracle Supplier Lifecycle Management - Version 12.1.2 and l ...

  3. 【一天一道LeetCode】#40. Combination Sum II

    一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...

  4. Linux 删除目录与文件

    Linux 删除目录与文件 在当前目录下查找所有.svn的目录 $ find . -type d -name ".svn" 删除当前目录下所有.svn的目录 $ find . -t ...

  5. STL - priority_queue(优先队列)

    优先级队列priority_queue 最大值优先级队列.最小值优先级队列 优先级队列适配器 STL priority_queue 用来开发一些特殊的应用. priority_queue<int ...

  6. AP INVOICES IMPORT API(NOT request)

    PROCEDURE process_cux_to_ap(x_return_status OUT NOCOPY VARCHAR2, x_msg_count OUT NOCOPY NUMBER, x_ms ...

  7. 【Android 应用开发】Android - 按钮组件详解

    总结了Android中常用的按钮用法 示例源码下载地址 : -- CSDN :  http://download.csdn.net/detail/han1202012/6852091 -- GitHu ...

  8. 算法精解:最小二乘法C实现

    计量经济学研究的直接目的是确定总体回归函数Yi=B1+B2Xi+ui,然而能够得到的只是来自总体的若干样本的观测值,要用样本信息建立的样本回归函数尽可能"接近"地去估计总体回归函数 ...

  9. C/C++内存布局及对齐

    1.源文件转换为可执行文件 源文件经过以下几步生成可执行文件: 1.预处理(preprocessor):对#include.#define.#ifdef/#endif.#ifndef/#endif等进 ...

  10. 【44】java大数值剖析

    基本的整数和浮点型精度不能满足需求,那么可以使用java.math中的两个类:BigInteger和BigDecimal. BigInteger和BigDecimal介绍: 这两个类可以处理包含任意长 ...