-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgausselim.sublime-snippet
139 lines (91 loc) · 1.87 KB
/
gausselim.sublime-snippet
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<snippet>
<content><![CDATA[
struct GaussElim
{
int n;
vector<vector<ll>> a;
ll M;
vector<ll> inv;
ll binexp(ll x, ll y) {
if (!y) return 1;
if (y & 1) return (x * binexp(x, y - 1)) % M;
return binexp((x * x) % M, y / 2);
}
GaussElim(int _N, ll MD, int pc = 0) {
n = _N;
M = MD;
if (pc) {
inv.resize(M, 0);
for (int i = 1; i < M; i++)
inv[i] = binexp(i, M - 2);
}
}
void add(vector<ll> v, int eq) {
v.push_back(eq);
a.push_back(v);
}
vector<ll> solve() {
vector<int> fr;
int ci = 0;
for (int i = 0; i < n; i++) {
int f = -1;
for (int j = ci; j < a.size(); j++) {
if (a[j][i]) {
f = j;
break;
}
}
if (f == -1) {
fr.push_back(i);
continue;
}
swap(a[f], a[ci]);
ll tt = (inv.size() ? inv[a[ci][i]] : binexp(a[ci][i], M - 2));
for (ll &j : a[ci]) { j *= tt; j %= M; }
for (int j = ci + 1; j < a.size(); j++) {
if (!a[j][i]) continue;
ll rt = M - a[j][i];
for (int k = i; k <= n; k++) {
a[j][k] += (a[ci][k] * rt);
a[j][k] %= M;
}
}
ci++;
}
vector<ll> ans(n, -1);
for (int i : fr) {
ans[i] = 0;
for (int j = 0; j < a.size(); j++) {
a[j][i] = 0;
}
}
for (int i = ci; i < a.size(); i++) {
if (a[i][n]) return {-1};
}
for (int i = ci - 1; i >= 0; i--) {
int tt = -1;
for (int j = 0; j < n; j++) {
if (a[i][j]) {
tt = j;
break;
}
}
ans[tt] = a[i][n];
for (int j = 0; j < i; j++) {
ll tm = M - a[j][tt];
a[j][tt] = 0;
a[j][n] += (tm * a[i][n]);
a[j][n] %= M;
}
}
for (int i : ans)
assert(i != -1);
return ans;
}
};
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>gauss</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>