CF 1042 Div3 E
https://codeforces.com/contest/2131/problem/e
题意
给你两个长为 \(n\) 的数组 \(a_i, b_i\)。
你可以对于每个 \(i\) 至多执行一次操作。
操作为:\(a_i = a_i \oplus a_{i + 1}\)。
问最终是否可以使得所有 \(a_i = b_i\)。
\(n\leq 10^6\)。
Solution
类似结论题吧。
考虑每个 \(a_i\) 肯定最终都需要变成 \(b_i\),那么只有以下几种情况。
- \(a_i = b_i\)
- \(a_i \oplus a_{i + 1} = b_i\)
- \(a_i \oplus b_{i + 1} = b_i\)
那么只要对于所有的 \(i\) 看是否满足其中之一即可。
Code
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10, inf = 0x3f3f3f3f;
int n, a[N], b[N];
int main()
{
cin.tie(0)->ios::sync_with_stdio(false);
int _; cin >> _;
while (_--) {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i];
bool ok = (a[n] == b[n]);
for (int i = 1; i <= n; i++) {
if (a[i] == b[i] || (a[i] ^ a[i + 1]) == b[i] || (a[i] ^ b[i + 1]) == b[i]) continue;
ok = 0;
}
cout << (ok ? "YES\n" : "NO\n");
}
return 0;
}

浙公网安备 33010602011771号