-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathE_OneIsEnough.cpp
More file actions
80 lines (64 loc) · 1.46 KB
/
E_OneIsEnough.cpp
File metadata and controls
80 lines (64 loc) · 1.46 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "bits/stdc++.h"
using i64 = long long;
void solve()
{
int n, m;
std::cin >> n >> m;
std::vector<int> a(n);
for (int i = 0; i < n; i++)
{
std::cin >> a[i];
a[i] = (a[i] > 0 ? 1 : 0);
}
std::map<int, std::vector<int>> c;
c[0] = a;
auto add = [&](int x, int y)
{
int ans = (x + y) % n;
ans = (ans + n) % n;
assert(ans >= 0);
return ans;
};
auto subtract = [&](int x, int y)
{
int ans = (x - y) % n;
ans = (ans + n) % n;
assert(ans >= 0);
return ans;
};
for (int i = 1; i < 31; i++)
{
c[1 << i].resize(n);
for (int j = 0; j < n; j++)
c[1 << i][j] = c[0][subtract(j, 1 << i)] ^ c[0][add(j, 1 << i)];
}
auto make = [&](int from, int to)
{
if (c.contains(to))
return;
c[to].resize(n);
for (int i = 0; i < n; i++)
c[to][i] = c[from][subtract(i, from - to)] ^ c[from][add(i, from - to)];
return;
};
int start = 0;
for (int i = 0; i < 31; i++)
{
if ((m >> i) & 1)
{
make(start, start + (1 << i));
start += (1 << i);
}
}
std::cout << std::accumulate(std::begin(c[m]), std::end(c[m]), 0) << "\n";
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t = 1;
std::cin >> t;
while (t--)
solve();
return 0;
}