[ABC150F] Xor Shift
题意
给定两个序列 \(a, b\),求将 \(b\) 循环移位 \(k\) 位,再给所有 \(b_i \oplus x\),求所有满足条件的 \((k, x)\)。
\(n \le 2 \times 10 ^ 5\)。
Sol
对于区间异或,容易想到差分。
考虑对 \(a\) 和 \(b\) 分别差分,注意到差分后 \(x\) 直接消失了!
也就是:
- \(a_0 \oplus a_1 = b_{(0 + k) \mod n} \oplus b_{(1 + k) \mod n}\)
- \(a_1 \oplus a_2 = b_{(1 + k) \mod n} \oplus b_{(2 + k) \mod n}\)
- \(a_2 \oplus a_3 = b_{(2 + k) \mod n} \oplus b_{(3 + k) \mod n}\)
- \(...\)
- \(a_{n - 1} \oplus a_0 = b_{(n - 1 + k) \mod n} \oplus b_{0} \mod n\)
容易发现映射后的条件的必要性显然,而充分性的条件刚好是 \(x\) 的取值范围。
于是原问题变为字符串匹配模板,直接 \(\texttt{kmp}\) 即可。
Code
#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;
}