[ABC150F] Xor Shift

cxqghzj·2024-10-02 10:56·10 次阅读

[ABC150F] Xor Shift

题意#

给定两个序列 a,b,求将 b 循环移位 k 位,再给所有 bix,求所有满足条件的 (k,x)

n2×105

Sol#

对于区间异或,容易想到差分。

考虑对 ab 分别差分,注意到差分后 x 直接消失了!

也就是:

  • a0a1=b(0+k)modnb(1+k)modn
  • a1a2=b(1+k)modnb(2+k)modn
  • a2a3=b(2+k)modnb(3+k)modn
  • ...
  • an1a0=b(n1+k)modnb0modn

容易发现映射后的条件的必要性显然,而充分性的条件刚好是 x 的取值范围。

于是原问题变为字符串匹配模板,直接 kmp 即可。

Code#

Copy
#include <iostream> #include <algorithm> #include <cstdio> #include <array> #include <vector> #define pii pair <int, int> using namespace std; #ifdef ONLINE_JUDGE #define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++) char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf; #endif int read() { int p = 0, flg = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') flg = -1; c = getchar(); } while (c >= '0' && c <= '9') { p = p * 10 + c - '0'; c = getchar(); } return p * flg; } void write(int x) { if (x < 0) { x = -x; putchar('-'); } if (x > 9) { write(x / 10); } putchar(x % 10 + '0'); } bool _stmer; #define fi first #define se second const int N = 2e5 + 5, inf = 2e9; namespace Kmp { array <int, N * 3> isl; void prefix(vector <int> s, int n) { for (int i = 2; i <= n; i++) { isl[i] = isl[i - 1]; while (isl[i] && s[isl[i] + 1] != s[i]) isl[i] = isl[isl[i]]; if (s[isl[i] + 1] == s[i]) isl[i]++; } } } //namespace Kmp array <int, N> s, h; bool _edmer; int main() { cerr << (&_stmer - &_edmer) / 1024.0 / 1024.0 << "MB\n"; int n = read(); for (int i = 0; i < n; i++) s[i] = read(); for (int i = 0; i < n; i++) h[i] = read(); vector <int> str; str.push_back(inf); for (int i = 0; i < n; i++) str.push_back(s[i] ^ s[(i + 1) % n]); str.push_back(inf); for (int i = 0; i < 2 * n; i++) str.push_back(h[i % n] ^ h[(i + 1) % n]); Kmp::prefix(str, str.size() - 1); vector <pii> ans; for (int i = 2 * n + 1; i < (int)str.size() - 1; i++) if (Kmp::isl[i] == n) ans.push_back(make_pair((n - (i - n - 1) % n) % n, h[(i - n - 1) % n] ^ s[0])); sort(ans.begin(), ans.end()); for (auto [x, y] : ans) { write(x), putchar(32); write(y), puts(""); } return 0; }
posted @   cxqghzj  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
点击右上角即可分享
微信分享提示
目录