1879D. Sum of XOR Functions

D. Sum of XOR Functions

Description

输入 n(1≤n≤3e5) 和长为 n 的数组 a(0≤a[i]≤1e9)。

定义 f(L,R) 为连续子数组 a[L] 到 a[R] 的异或和。
计算所有 f(L,R)*(R-L+1) 的和,其中 L <= R。
输出答案模 998244353 的结果。

Hints/Notes

  • preXor

Solution

Language: C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <utility>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>

#include <fstream>

using namespace std;

istream &in = cin;
ostream &out = cout;

// use one number x to mark all xor
// for one xor value, assume it has 30 bits, on the ith bit
// if the bit is 1, we need to know all previous 0 occurrences << i
// the question is how to get the r - l
// so we need two array, one for the count of 0/1 at bit i
// one for the sum of index at bit i
int main() {
int n;
in >> n;
long long count[30][2] = {0};
long long sum[30][2] = {0};
long long x = 0;
for (int i = 0; i < 30; i++) {
count[i][0] = 1;
}
long long res = 0;
for (int i = 1; i <= n; i++) {
int num;
in >> num;
x ^= num;
for (int j = 0; j < 30; j++) {
int bit = (x >> j) & 1;
// take this as an example
// for one index, it's always zero, suddenly for the ith number,
// its bit at this index is 1. It means all the previous subArray
// can be counted against, and we need to calculate the number of
// 0s, along with the total of their index
res = (res + ((count[j][1 - bit] * i - sum[j][1 - bit]) % 998244353 << j) % 998244353) % 998244353;
sum[j][bit] += i;
count[j][bit]++;
}
}
out << res << endl;
return 0;
}