#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
const ll MAXN = 1e3 + 10;
const ll MOD = 1e9 + 7;
const ll MAXLOG = 7;
const ll MAXA = 128;
struct upd
{
ll left, right;
ll val;
upd()
{
left = right = 1;
val = 0;
}
upd(ll _left, ll _right, ll _val)
{
left = _left;
right = _right;
val = _val;
}
};
ll n, q;
ll a[MAXN];
ll cur[MAXN];
ll pow2[MAXN];
ll tmp[32];
ll cnt[MAXN][32];
ll curcnt[MAXN];
ll pow2q;
vector < upd > updates;
bool lampa = true;
void read()
{
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
}
cin >> q;
for(int i = 1; i <= q; i++)
{
ll left, right;
ll x;
if(left < right)
lampa = false;
cin >> left >> right >> x;
upd u = upd(left, right, x);
updates.push_back(u);
}
}
void solve()
{
ll ans = 0;
pow2[0] = 1;
for(int i = 1; i < MAXN; i++)
{
pow2[i] = (pow2[i - 1] * 2) % MOD;
}
for(int i = 1; i <= n; i++)
{
for(int j = i; j <= n; j++)
{
ll cnt = (i * (n - j + 1)) % MOD;
ll type;
if(i != j)
type = (cnt * 2) % MOD;
else
type = cnt;
ll cursum = 0;
for(int bit1 = 0; bit1 < MAXLOG; bit1++)
{
for(int bit2 = 0; bit2 < MAXLOG; bit2++)
{
vector < vector < ll > > dp;
dp.resize(2);
dp[0].resize(4);
dp[1].resize(4);
dp[0][(bool(a[i] & (1 << bit1)) << 1) | (bool(a[j] & (1 << bit2)))] = 1;
for(int k = 0; k < q; k++)
{
dp[1] = dp[0];
int in1 = 0;
int in2 = 0;
if(updates[k].left <= i && i <= updates[k].right)
in1 = (bool(updates[k].val & (1 << bit1)));
if(updates[k].left <= j && j <= updates[k].right)
in2 = (bool(updates[k].val & (1 << bit2)));
int h = (in1 << 1) | in2; /// 00; 01; 10; 11
for(int x = 0; x < 4; x++)
{
dp[1][x ^ h] = (dp[1][x ^ h] + dp[0][x]) % MOD;
}
swap(dp[1], dp[0]);
}
cursum = (cursum + (pow2[bit1 + bit2] * dp[0][3]) % MOD) % MOD;
}
}
cursum = (cursum * type) % MOD;
ans = (ans + cursum) % MOD;
}
}
cout << ans << endl;
}
void update(bitset < MAXA >&bs, ll val)
{
if(bs[bs._Find_first() ^ val])
return;
vector < int > to_add;
for(int i = bs._Find_first(); i < MAXA; i = bs._Find_next(i))
to_add.push_back(i ^ val);
for(int i : to_add)
bs[i] = 1;
}
ll binpow(ll a, ll b)
{
ll result = 1;
while(b)
{
if(b & 1)
result = (result * a) % MOD;
a = (a * a) % MOD;
b /= 2;
}
return result;
}
void solve_2()
{
pow2q = 1;
for(int i = 1; i <= q; i++)
{
pow2q = (pow2q * 2) % MOD;
}
ll ans = 0;
for(int i = 1; i <= n; i++)
{
for(int j = i; j <= n; j++)
{
bitset < MAXA > bs1;
bitset < MAXA > bs2;
bitset < MAXA > bs3;
bs1[a[i]] = bs2[a[j]] = bs3[0] = 1;
for(int k = 0; k < q; k++)
{
bool in1 = false;
bool in2 = false;
if(updates[k].left <= i && i <= updates[k].right)
in1 = true;
if(updates[k].left <= j && j <= updates[k].right)
in2 = true;
if(in1 && in2)
update(bs3, updates[k].val);
else if(in1)
update(bs1, updates[k].val);
else if(in2)
update(bs2, updates[k].val);
}
ll sum = 0;
for(int idx1 = bs1._Find_first(); idx1 < MAXA; idx1 = bs1._Find_next(idx1))
{
for(int idx2 = bs2._Find_first(); idx2 < MAXA; idx2 = bs2._Find_next(idx2))
{
for(int idx3 = bs3._Find_first(); idx3 < MAXA; idx3 = bs3._Find_next(idx3))
{
ll cur = ((idx1 ^ idx3) * (idx2 ^ idx3)) % MOD;
if(i != j)
cur = (cur * 2) % MOD;
sum = (sum + cur) % MOD;
}
}
}
ll cnt = (i * (n - j + 1)) % MOD;
sum = (sum * cnt) % MOD;
sum = (sum * pow2q) % MOD;
sum = (sum * (binpow(bs1.count() * bs2.count() * bs3.count(), MOD - 2))) % MOD;
ans = (ans + sum) % MOD;
}
}
cout << ans << endl;
}
void solve_3()
{
vector < int > chep(n + 1);
for(int i = 1; i <= n; i++)
{
chep[i - 1] = a[i];
}
for(int i = 0; i < n; i++)
{
cnt[i][chep[i]]++;
}
for(int i = 0; i < q; i++)
{
ll left = updates[i].left;
ll right = updates[i].right;
ll val = updates[i].val;
left--;
curcnt[left]++;
for(int j = 0; j < 32; j++)
tmp[j] = cnt[left][j];
for(int j = 0; j < 32; j++)
{
cnt[left][j ^ val] = (cnt[left][j ^ val] + tmp[j]) % MOD;
}
}
for(int i = n; i > 0; i--)
{
curcnt[i - 1] += curcnt[i];
}
for(int i = 0; i <= n; i++)
{
ll x = curcnt[i];
ll p = 1;
while(x--)
p = (p * 2) % MOD;
curcnt[i] = p;
}
vector < ll > last;
vector < ll > d;
last.assign(4000, 0);
last[0] = 1;
ll ans = 0;
for(int i = 0; i < n; i++)
{
d.assign(4000, 0);
for(int j = 0; j < 32; j++)
{
for(int k = 0; j + k < 4000; k++)
{
d[j + k] = (d[j + k] + (last[k] * cnt[i][j]) % MOD) % MOD;
}
}
for(int j = 0; j < 4000; j++)
{
last[j] = d[j];
ans = (ans + d[j] * (j * j) * (n + 1) % MOD * curcnt[i + 1]) % MOD;
}
}
last.assign(200000, 0);
last[0] = 1;
for(int i = 0; i < n; i++)
{
d.assign(200000, 0);
for(int j = 0; j < 32; j++)
{
for(int k = 0; k + j * (n - i) < 200000; k++)
{
d[k + j * (n - i)] = (d[k + j * (n - i)] + (last[k] * cnt[i][j]) % MOD) % MOD;
}
}
last = d;
}
for(int i = 0; i < 200000; i++)
{
ans = (ans + MOD - d[i] * i % MOD * i % MOD) % MOD;
}
cout << ans << endl;
}
int main()
{
ios_base :: sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
read();
if(q > 5000)
solve_3();
else if(n > 500)
solve_2();
else
solve();
return 0;
}
Compilation message
xorsum.cpp: In function 'void solve_3()':
xorsum.cpp:253:12: warning: unused variable 'right' [-Wunused-variable]
253 | ll right = updates[i].right;
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
57 ms |
464 KB |
Output is correct |
7 |
Correct |
52 ms |
348 KB |
Output is correct |
8 |
Correct |
53 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2041 ms |
6076 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
385 ms |
348 KB |
Output is correct |
2 |
Correct |
330 ms |
348 KB |
Output is correct |
3 |
Correct |
154 ms |
344 KB |
Output is correct |
4 |
Correct |
124 ms |
452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
348 KB |
Output is correct |
2 |
Correct |
9 ms |
348 KB |
Output is correct |
3 |
Correct |
9 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
348 KB |
Output is correct |
2 |
Correct |
9 ms |
348 KB |
Output is correct |
3 |
Correct |
9 ms |
348 KB |
Output is correct |
4 |
Correct |
1942 ms |
604 KB |
Output is correct |
5 |
Correct |
1935 ms |
600 KB |
Output is correct |
6 |
Correct |
1901 ms |
604 KB |
Output is correct |
7 |
Execution timed out |
2029 ms |
600 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
57 ms |
464 KB |
Output is correct |
7 |
Correct |
52 ms |
348 KB |
Output is correct |
8 |
Correct |
53 ms |
348 KB |
Output is correct |
9 |
Correct |
9 ms |
348 KB |
Output is correct |
10 |
Correct |
9 ms |
348 KB |
Output is correct |
11 |
Correct |
9 ms |
348 KB |
Output is correct |
12 |
Correct |
1222 ms |
480 KB |
Output is correct |
13 |
Correct |
1209 ms |
600 KB |
Output is correct |
14 |
Correct |
1246 ms |
344 KB |
Output is correct |
15 |
Correct |
1219 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
57 ms |
464 KB |
Output is correct |
7 |
Correct |
52 ms |
348 KB |
Output is correct |
8 |
Correct |
53 ms |
348 KB |
Output is correct |
9 |
Correct |
9 ms |
348 KB |
Output is correct |
10 |
Correct |
9 ms |
348 KB |
Output is correct |
11 |
Correct |
9 ms |
348 KB |
Output is correct |
12 |
Correct |
1942 ms |
604 KB |
Output is correct |
13 |
Correct |
1935 ms |
600 KB |
Output is correct |
14 |
Correct |
1901 ms |
604 KB |
Output is correct |
15 |
Execution timed out |
2029 ms |
600 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
57 ms |
464 KB |
Output is correct |
7 |
Correct |
52 ms |
348 KB |
Output is correct |
8 |
Correct |
53 ms |
348 KB |
Output is correct |
9 |
Execution timed out |
2041 ms |
6076 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |