Skip to content

Commit 7f1f55c

Browse files
Added 6 Xor Gauss problems from Codechef, Codeforces and Spoj
1 parent 3bed543 commit 7f1f55c

File tree

6 files changed

+285
-0
lines changed

6 files changed

+285
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
#define llong long long int
6+
7+
int main() {
8+
ios_base::sync_with_stdio(false);
9+
cin.tie(NULL);
10+
11+
int T; cin >> T;
12+
while (T--) {
13+
int K, M;
14+
cin >> K >> M;
15+
16+
int sz = 0;
17+
vector<int> basis(K, 0);
18+
19+
for (int i = 0; i < M; i++) {
20+
int x; cin >> x;
21+
22+
bool done = false;
23+
for (int j = 0; j < K && !done; j++) {
24+
if (!(x & (1 << j))) continue;
25+
26+
if (basis[j]) x ^= basis[j];
27+
else basis[j] = x, sz++, done = true;
28+
}
29+
}
30+
31+
int ans = 1 << (K - sz);
32+
cout << ans << "\n";
33+
}
34+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
#define llong long long int
6+
7+
const int MAX = 20;
8+
const int MOD = 1e9 + 7;
9+
10+
int exp(int a, int n) {
11+
if (!n) return 1;
12+
13+
int ans = exp(a, n >> 1);
14+
ans = (1LL * ans * ans) % MOD;
15+
16+
if (!(n & 1)) return ans;
17+
return (1LL * ans * a) % MOD;
18+
}
19+
20+
int main() {
21+
ios_base::sync_with_stdio(false);
22+
cin.tie(NULL);
23+
24+
int n, q;
25+
cin >> n >> q;
26+
27+
vector<int> a(n);
28+
for (int i = 0; i < n; i++)
29+
cin >> a[i];
30+
31+
vector<tuple<int, int, int>> queries(q);
32+
for (int i = 0; i < q; i++) {
33+
int l, x;
34+
cin >> l >> x;
35+
queries[i] = make_tuple(l, x, i);
36+
}
37+
38+
sort(queries.begin(), queries.end());
39+
40+
vector<int> ans(q, 0);
41+
int basis[MAX] = {0}, sz = 0, processed = 0;
42+
43+
for (auto [l, x, idx]: queries) {
44+
while (processed < l) {
45+
bool done = false;
46+
for (int i = 0; i < MAX && !done; i++) {
47+
if (!(a[processed] & (1 << i))) continue;
48+
49+
if (basis[i]) a[processed] ^= basis[i];
50+
else basis[i] = a[processed], sz++, done = true;
51+
}
52+
53+
processed++;
54+
}
55+
56+
bool independent = false;
57+
for (int i = 0; i < MAX && !independent; i++) {
58+
if (!(x & (1 << i))) continue;
59+
60+
if (basis[i]) x ^= basis[i];
61+
else independent = true;
62+
}
63+
64+
if (!independent)
65+
ans[idx] = exp(2, l - sz);
66+
}
67+
68+
for (int i = 0; i < q; i++)
69+
cout << ans[i] << "\n";
70+
}

Codeforces/zero_xor_subset_less.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
#define llong long long int
6+
7+
const int MAX = 30;
8+
9+
int main() {
10+
ios_base::sync_with_stdio(false);
11+
cin.tie(NULL);
12+
13+
int n; cin >> n;
14+
15+
vector<int> a(n);
16+
for (int i = 0; i < n; i++)
17+
cin >> a[i];
18+
19+
int x = 0, sz = 0, basis[MAX] = {0};
20+
for (int i = 0; i < n; i++) {
21+
x ^= a[i];
22+
23+
int v = x;
24+
bool done = false;
25+
26+
for (int j = 0; j < MAX && !done; j++) {
27+
if (!(v & (1 << j))) continue;
28+
29+
if (basis[j]) v ^= basis[j];
30+
else basis[j] = v, sz++, done = true;
31+
}
32+
}
33+
34+
if (!x) cout << "-1\n";
35+
else cout << sz << "\n";
36+
}

Other/godzilla_and_pretty_xor.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
#define llong long long int
6+
7+
const int MAX = 31;
8+
9+
int main() {
10+
ios_base::sync_with_stdio(false);
11+
cin.tie(NULL);
12+
13+
int n; cin >> n;
14+
15+
int basis[MAX] = {0}, sz = 0;
16+
while (n--) {
17+
int t, k;
18+
cin >> t >> k;
19+
20+
if (t == 1) {
21+
bool done = false;
22+
for (int i = MAX - 1; i >= 0 && !done; i--) {
23+
if (!(k & (1 << i))) continue;
24+
25+
if (basis[i]) k ^= basis[i];
26+
else basis[i] = k, sz++, done = true;
27+
}
28+
} else {
29+
int ans = 0, seen = 0;
30+
for (int i = MAX - 1; i >= 0; i--) {
31+
if (!basis[i]) continue;
32+
33+
int rem = 1 << (sz - ++seen);
34+
if (k > rem) {
35+
k -= rem;
36+
if (!(ans & (1 << i)))
37+
ans ^= basis[i];
38+
} else if (ans & (1 << i)) {
39+
ans ^= basis[i];
40+
}
41+
}
42+
43+
cout << ans << "\n";
44+
}
45+
}
46+
}

Other/two_faced_hobz.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
#define llong long long int
6+
7+
const int MAX = 30;
8+
9+
int main() {
10+
ios_base::sync_with_stdio(false);
11+
cin.tie(NULL);
12+
13+
freopen("salkan.in", "r", stdin);
14+
15+
int T; cin >> T;
16+
while (T--) {
17+
int N, K;
18+
cin >> N >> K;
19+
20+
int XA = 0;
21+
vector<int> A(N);
22+
23+
for (int i = 0; i < N; i++) {
24+
cin >> A[i];
25+
XA ^= A[i];
26+
}
27+
28+
int basis[MAX] = {0};
29+
for (int i = 0; i < N; i++) {
30+
int B; cin >> B;
31+
32+
int v = A[i] ^ B;
33+
bool done = false;
34+
35+
for (int j = MAX - 1; j >= 0 && !done; j--) {
36+
if (!(v & (1 << j))) continue;
37+
38+
if (basis[j]) v ^= basis[j];
39+
else basis[j] = v, done = true;
40+
}
41+
}
42+
43+
int ans = XA;
44+
for (int i = MAX - 1; i >= 0; i--) {
45+
if (!basis[i]) continue;
46+
47+
int aux = (ans & (1 << i)) ? ans : ans ^ basis[i];
48+
for (int j = i - 1; j >= 0; j--) {
49+
if (!basis[j]) continue;
50+
51+
if (aux & (1 << j))
52+
aux ^= basis[j];
53+
}
54+
55+
if (aux > K && (ans & (1 << i))) ans ^= basis[i];
56+
else if (aux <= K && !(ans & (1 << i))) ans ^= basis[i];
57+
}
58+
59+
if (ans > K) ans = 0;
60+
cout << ans << "\n";
61+
}
62+
}

Spoj/xor_maximization.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
#define llong long long int
6+
7+
const int MAX = 62;
8+
9+
int main() {
10+
ios_base::sync_with_stdio(false);
11+
cin.tie(NULL);
12+
13+
int N; cin >> N;
14+
15+
llong basis[MAX] = {0LL};
16+
for (int i = 0; i < N; i++) {
17+
llong a; cin >> a;
18+
19+
bool done = false;
20+
for (int j = MAX - 1; j >= 0 && !done; j--) {
21+
if (!(a & (1LL << j))) continue;
22+
23+
if (basis[j]) a ^= basis[j];
24+
else {
25+
done = true;
26+
basis[j] = a;
27+
}
28+
}
29+
}
30+
31+
llong ans = 0LL;
32+
for (int i = MAX - 1; i >= 0; i--)
33+
if (basis[i] && !(ans & (1LL << i)))
34+
ans ^= basis[i];
35+
36+
cout << ans << "\n";
37+
}

0 commit comments

Comments
 (0)