forked from Ashishgup1/Competitive-Coding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gaussian Elimination.cpp
68 lines (57 loc) · 1.17 KB
/
Gaussian Elimination.cpp
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
struct Gauss
{
static const int bits = 20;
int table[bits];
Gauss()
{
for(int i = 0; i < bits; i++)
table[i] = 0;
}
int size()
{
int ans = 0;
for(int i = 0; i < bits; i++)
{
if(table[i])
ans++;
}
return ans;
}
bool can(int x)
{
for(int i = bits-1; i >= 0; i--)
x = min(x, x ^ table[i]);
return x == 0;
}
void add(int x)
{
for(int i = bits-1; i >= 0 && x; i--)
{
if(table[i] == 0)
{
table[i] = x;
x = 0;
}
else
x = min(x, x ^ table[i]);
}
}
int getBest()
{
int x = 0;
for(int i = bits-1; i >= 0; i--)
x = max(x, x ^ table[i]);
return x;
}
void merge(Gauss &other)
{
for(int i = bits-1; i >= 0; i--)
add(other.table[i]);
}
};
//Logic: https://math.stackexchange.com/questions/48682/maximization-with-xor-operator
//Source: https://codeforces.com/profile/tfg
//Problem 1: http://codeforces.com/contest/959/problem/F
//Solution 1: https://codeforces.com/contest/959/submission/50314871
//Problem 2: https://codeforces.com/contest/1101/problem/G
//Solution 2: https://codeforces.com/contest/1101/submission/50315103