Codeforces 1045B Space Isaac - 数论 - Hash
题目传送门
题目大意
给定将$\left \{ 0, 1, \dots, m - 1\right \}$分成了不相交的两个非空集合$A$和$B$,给定$A$,问存在多少个整数$x$满足$0\leqslant x < m$且对于任意$a\in A, b \in B$满足$a + b\not \equiv x \pmod{m}$
容易发现满足后一个条件的充分必要条件是对于任意$a \in A$,存在$a' \in A$使得$a + a' \equiv x \pmod{m}$。
如果$a \leqslant x$,则易证$a' \leqslant x$。
同样,如果$a > x$,则易证$a' > x$。
所以这个数$x$将序列分成两部分,每一部分首尾配对后,每一对的和都等于$x$。
那么怎么判断呢?我们发现$a + d = c + b$等价于$a - b = c - d$。
所以我们只用差分一下判断是否是回文,再判断首尾的和是否等于$x$就行了。
Code
/**
* Codeforces
* Problem#1045B
* Accepted
* Time: 77ms
* Memory: 7300k
*/
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <vector>
using namespace std;
typedef bool boolean; #define ull unsigned long long const int N = 2e5 + ;
const ull base = 1e9 + ; int n, M;
int ar[N];
int cr[N];
ull ph[N], sh[N], pb[N]; int add(int a, int b) {
return ((a += b) >= M) ? (a - M) : (a);
} inline void init() {
scanf("%d%d", &n, &M);
for (int i = ; i <= n; i++)
scanf("%d", ar + i);
} ull fquery(int l, int r) { // forward
if (l > r)
return ;
return ph[r] - ph[l - ] * pb[r - l + ];
} ull rquery(int l, int r) { // backward
if (l > r)
return ;
return sh[l] - sh[r + ] * pb[r - l + ];
} boolean ispar(int l, int r) {
return fquery(l, r) == rquery(l, r);
} vector<int> vec; inline void solve() {
ph[] = , sh[n] = , pb[] = ;
for (int i = ; i < n; i++)
ph[i] = ph[i - ] * base + (cr[i] = ar[i + ] - ar[i]);
for (int i = n - ; i; i--)
sh[i] = sh[i + ] * base + cr[i];
for (int i = ; i < n; i++)
pb[i] = pb[i - ] * base; for (int i = , x; i < n; i++) {
x = ar[] + ar[i];
// if (x >= M)
// break;
if (add(ar[i + ], ar[n]) == x && ispar(, i - ) && ispar(i + , n - ))
vec.push_back(x);
}
int x = add(ar[], ar[n]);
if ((x <= ar[] || x >= ar[n]) && ispar(, n - ))
vec.push_back(x);
printf("%d\n", (signed) vec.size());
sort(vec.begin(), vec.end());
for (int i = ; i < (signed) vec.size(); i++)
printf("%d ", vec[i]);
putchar('\n');
} int main() {
init();
solve();
return ;
}
Codeforces 1045B Space Isaac - 数论 - Hash的更多相关文章
- Codeforces 1045B Space Isaac
Space Isaac 我们定义第一个集合为a, 第二个集合为b 先把a数组排序, 然后我们会以线段的形式得到b集合. 我们先用a[ 1 ]去和 b 中的元素结合, 只有size(a) 个数字未被覆盖 ...
- luogu2312 解方程 (数论,hash)
luogu2312 解方程 (数论,hash) 第一次外出学习讲过的题目,然后被讲课人的一番话惊呆了. 这个题,我想着当年全国只有十几个满分.....然后他又说了句我考场A这道题时,用了5个模数 确实 ...
- [CodeForces - 1225C]p-binary 【数论】【二进制】
[CodeForces - 1225C]p-binary [数论][二进制] 标签: 题解 codeforces题解 数论 题目描述 Time limit 2000 ms Memory limit 5 ...
- Codeforces Gym 100463B Music Mess Hash 逻辑题
Music Mess Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments ...
- CodeForces 1056E - Check Transcription - [字符串hash]
题目链接:https://codeforces.com/problemset/problem/1056/E One of Arkady's friends works at a huge radio ...
- codeforces 633C. Spy Syndrome 2 hash
题目链接 C. Spy Syndrome 2 time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- CodeForces - 615D Multipliers(数论)
http://codeforces.com/problemset/problem/615/D 题意 给出m个质因子,组成一个数n.问n的约数的乘积是多少,输出mod 1e+7的结果. 分析 从输入我们 ...
- Codeforces Round #425 (Div. 2) Problem C Strange Radiation (Codeforces 832C) - 二分答案 - 数论
n people are standing on a coordinate axis in points with positive integer coordinates strictly less ...
- [CodeForces - 447A] A - DZY Loves Hash
A - DZY Loves Hash DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert ...
随机推荐
- transform的兼容性写法浏览器 和 transition
transform:rotate(7deg); -ms-transform:rotate(7deg); /* IE 9 */ -moz-transform:rotate(7deg); /* Firef ...
- Unity 为队伍设置不同颜色的shader
在魔兽争霸等一些游戏中,我们通过模型的颜色就能很轻松的区分队伍,如下: 实现的方法有很多,比如: 1,为不同队伍各出一张不同颜色的贴图(Hmmm,war3有的地图可以容纳12只队伍,美术大大们会很 ...
- c++试题2
一.写出下列程序的运行结果(40 分) 1.for(i=1;i<5;i++); cout << “OK” << endl; 程序执行后的输出结果是: OK ___ ...
- emoji表情softbank和unicode不兼容解决方案
https://blog.ernest.me/post/emoji-remapping-solution
- InnoDB中锁的算法(3)
Ⅰ.隐式锁vs显示锁 session1: (root@localhost) [test]> show variables like 'tx_isolation'; +-------------- ...
- c->log技巧
介绍: 在C代码里,有时会加入一些打印信息方便分析问题,可用如下代码替代打印函数,更加方便. // // Created by lady on 18-12-10. // #include <st ...
- (备忘)打开office2010总是在配置进度
1.同时按上键盘上面的windows键和R键,出现“运行” 2.输入“regedit”,回车进入注册表 3.点击“HKEY_CURRENT_USER”展开,依次“Software”--“Microso ...
- EFM32G232F64时钟树
1.为了熟悉MCU的时钟树,先看看EFM32G232F64的CMU(ClockManagementUnit) 时钟管理单元(CMU)用于管控晶振(时钟源)和各个时钟节点.出于降低功耗和启动时间的目的, ...
- java框架之Spring(4)-Spring整合Hibernate和Struts2
准备 导包 Struts2 导入 Struts2 zip 包解压目录下 'apps/struts-blank.war' 中所有 jar 包,如下: asm-3.3.jar asm-commons-3. ...
- [vue]mvc模式和mvvm模式及vue学习思路(废弃)
好久不写东西了,感觉收生疏了, 学习使用以思路为主, 记录笔记为辅作用. v-if: http://www.cnblogs.com/iiiiiher/p/9025532.html v-show tem ...