-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
116 lines (97 loc) · 2.17 KB
/
test.cpp
File metadata and controls
116 lines (97 loc) · 2.17 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "copypaste/debug.h"
#else
#define debug(...) 42
#endif
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef array<int, 2> a2;
struct fast_ios {
fast_ios() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
};
} fast_ios_;
void solve() {
int n, k;
cin >> n >> k;
vector<ll> a1(n);
for (int i = 0; i < n; i++) {
cin >> a1[i];
}
if (n == 1) {
cout << 0 << '\n';
return;
}
if (n % k) {
cout << 0 << '\n';
return;
}
vector<ll> a(n);
auto work = [&](int idx) {
for (int i = 0; i < n; i++) {
a[i] = a1[(i + idx) % n];
}
vector<ll> c(n);
for (int i = 0; i < n; i++) {
c[i] = a[i] ^ a[(i + 1) % n];
}
debug(c);
vector<ll> d(n);
for (int i = k; i < n; i++) {
d[i] = d[i - k] ^ c[i - k];
}
debug(d);
ll now = 0;
vector<ll> V;
for (int i = 0; i < n; i++) {
int idx = n - i - 1;
now ^= d[idx];
if (i >= k) {
now ^= d[idx + k];
}
debug(idx, a[idx], now);
V.push_back(a[idx] ^ now);
}
debug(V);
ll ans = 0;
const int LIMIT = 29;
for (int i = LIMIT; i >= 0; i--) {
int one_num = 0, zero_num = 0;
for (ll j : V) {
int f = (j >> i) & 1;
if (f) {
one_num++;
} else {
zero_num++;
}
}
int mi = min(one_num, zero_num);
if (mi > 0) {
debug(mi, i);
}
ans += (1LL << i) * mi;
}
return ans;
};
ll ans = numeric_limits<ll>::max();
for (int i = 0; i < 1; i++) {
ans = min(ans, work(i));
}
cout << ans << '\n';
}
int main() {
#ifdef LOCAL
freopen("./data.in", "r", stdin);
#endif
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}