有关异或问题的一点东西
先看一道经典例题
C++代码如下(第一种)
#include <bits/stdc++.h>
using i64 = long long;
using PII = std::pair<i64,i64>;
#define int i64
#define yes std::cout << "YES\n";
#define no std::cout << "NO\n";
void solve() {
int n;
std::cin >> n;
std::vector<int> a(n + 1);
for (int i = 1; i <= n; i ++) {
std::cin >> a[i];
}
int ans = 0;
for (int u = 0; u < 31; u ++) {
int s = 0,m = 0;
for (int i = 1; i <= n; i ++) {
if (a[i] >> u & 1) {
s = i - s;
}
m += s;
}
ans += (1 << u) * m;
}
std::cout << ans << "\n";
}
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int T = 1;
// std::cin >> T;
while (T -- ) {
solve();
}
return 0;
}
C++代码如下(第二种)
#include <bits/stdc++.h>
using i64 = long long;
using PII = std::pair<i64,i64>;
#define int i64
#define yes std::cout << "YES\n";
#define no std::cout << "NO\n";
void solve() {
int n;
std::cin >> n;
std::vector<int> s(n + 1);
for (int i = 1; i <= n; i ++) {
int x;
std::cin >> x;
s[i] = s[i - 1] ^ x;
}
std::vector<int> cnt0(31),cnt1(31);
int ans = 0;
for (int i = 0; i <= n; i ++) {
for (int u = 0; u < 31; u ++) {
if (s[i] >> u & 1) {
ans += (1 << u) * cnt0[u];
cnt1[u] ++;
} else {
ans += (1 << u) * cnt1[u];
cnt0[u] ++;
}
}
}
std::cout << ans << "\n";
}
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int T = 1;
// std::cin >> T;
while (T -- ) {
solve();
}
return 0;
}
C++代码3
#include <bits/stdc++.h>
using i64 = long long;
using PII = std::pair<i64,i64>;
#define int i64
#define yes std::cout << "YES\n";
#define no std::cout << "NO\n";
void solve() {
int n;
std::cin >> n;
std::vector<int> s(n + 1);
for (int i = 1; i <= n; i ++) {
int x;
std::cin >> x;
s[i] = s[i - 1] ^ x;
}
std::vector<int> cnt0(31,1),cnt1(31);
int ans = 0;
for (int u = 0; u < 31; u ++) {
cnt0[u] = 1;
for (int i = 1; i <= n; i ++) {
if (s[i] >> u & 1) {
cnt1[u] ++;
} else {
cnt0[u] ++;
}
}
ans += (1 << u) * (cnt0[u] * cnt1[u]);
}
std::cout << ans << "\n";
}
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int T = 1;
// std::cin >> T;
while (T -- ) {
solve();
}
return 0;
}
练习(典题)
正片开始
这道题与上面那道相比多了一个(r - l + 1);拆开来看就是r * f(l,r) - (l - 1) * f(l,r);f(l,r)的每一位对应的就是第i行是j的区间个数,那么每次计算的时候 * i 即可,左边就可以表示为i * res[i][j],右边则记录一下每个位置出现的l - 1即可
注意,这里以上述代码2作为实例写的,代码1不容易计算l - 1,代码3是代码2的空间优化版
代码如下
#include <bits/stdc++.h>
using i64 = long long;
using PII = std::pair<i64,i64>;
#define int i64
#define yes std::cout << "YES\n";
#define no std::cout << "NO\n";
template<class T>
T power(T a, i64 b) {
T res = 1;
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
template<int P>
struct MInt {
int x;
MInt() : x{} {}
MInt(i64 x) : x{norm(x % P)} {}
int norm(int x) {
if (x < 0) {
x += P;
}
if (x >= P) {
x -= P;
}
return x;
}
int val() const {
return x;
}
MInt operator-() const {
MInt res;
res.x = norm(P - x);
return res;
}
MInt inv() const {
assert(x != 0);
return power(*this, P - 2);
}
MInt &operator*=(const MInt &rhs) {
x = 1LL * x * rhs.x % P;
return *this;
}
MInt &operator+=(const MInt &rhs) {
x = norm(x + rhs.x);
return *this;
}
MInt &operator-=(const MInt &rhs) {
x = norm(x - rhs.x);
return *this;
}
MInt &operator/=(const MInt &rhs) {
return *this *= rhs.inv();
}
friend MInt operator*(const MInt &lhs, const MInt &rhs) {
MInt res = lhs;
res *= rhs;
return res;
}
friend MInt operator+(const MInt &lhs, const MInt &rhs) {
MInt res = lhs;
res += rhs;
return res;
}
friend MInt operator-(const MInt &lhs, const MInt &rhs) {
MInt res = lhs;
res -= rhs;
return res;
}
friend MInt operator/(const MInt &lhs, const MInt &rhs) {
MInt res = lhs;
res /= rhs;
return res;
}
friend std::istream &operator>>(std::istream &is, MInt &a) {
i64 v;
is >> v;
a = MInt(v);
return is;
}
friend std::ostream &operator<<(std::ostream &os, const MInt &a) {
return os << a.val();
}
};
constexpr int P = 998244353;
using Z = MInt<P>;
void solve() {
int n;
std::cin >> n;
std::vector<int> s(n + 1);
for (int i = 1; i <= n; i ++) {
int x;
std::cin >> x;
s[i] = s[i - 1] ^ x;
}
std::vector cnt(30,std::vector<int>(2));
std::vector res(30,std::vector<int>(2));
Z ans = 0;
for (int i = 0; i <= n; i ++) {
for (int u = 0; u < 30; u ++) {
if (s[i] >> u & 1) {
ans += Z(1 << u) * (res[u][0] * i - cnt[u][0]);
res[u][1] ++;
cnt[u][1] += i;
} else {
ans += Z(1 << u) * (res[u][1] * i - cnt[u][1]);
res[u][0] ++;
cnt[u][0] += i;
}
}
}
std::cout << ans << "\n";
}
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int T = 1;
// std::cin >> T;
while (T -- ) {
solve();
}
return 0;
}
前缀异或为1,表示这段区间有奇数个1,那么以当前数结尾的前缀区间有cnt / 2 + 1个满足区间异或为1,cnt为1的个数(例如111,以最有1个1结尾的区间有2个)
前缀异或为0,表示这段区间有偶数个1,那么以当前数结尾的前缀区间有cnt / 2个满足区间异或为1,cnt为1的个数(例如101,以最有1个1结尾的区间有1个)
res数组的意义在于记录奇偶产生的区间个数
最后一点