-
Notifications
You must be signed in to change notification settings - Fork 13
/
Gaussian Elimination.cpp
64 lines (57 loc) · 1.07 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
//Logic: https://math.stackexchange.com/questions/48682/maximization-with-xor-operator
struct Gaussian
{
int no_of_bits = 20;
vector<int> v;
int set, origsize=0, redsize=0;
void push(int val)
{
origsize++;
if(val)
v.push_back(val);
}
void clear()
{
v.clear();
set=0, redsize=0;
}
void eliminate()
{
set = redsize = 0;
for(int bit=0;bit<=no_of_bits;bit++)
{
bool check=false;
for(int i=redsize;i<v.size();i++)
{
if((v[i]>>bit)&1)
{
swap(v[i], v[redsize]);
check=true;
break;
}
}
if(check)
{
for(int i=redsize+1;i<v.size();i++)
{
if((v[i]>>bit)&1)
v[i]^=v[redsize];
}
redsize++;
}
}
v.resize(redsize);
for(auto it:v)
set|=it;
}
Gaussian& operator =(Gaussian &orig)
{
v = orig.v;
set = orig.set;
redsize = orig.redsize;
origsize = orig.origsize;
return *this;
}
};
//Sample Problem 1: http://codeforces.com/contest/959/problem/F
//Sample Solution 1: http://codeforces.com/contest/959/submission/39772298