高斯消元方程组版。
int gauss () {
int r, c;
for (r = 1, c = 1; c <= n; c++) {
int _max = r;
for (int i = r; i <= n; i++)
if (Abs (a[i][c]) > Abs (a[_max][c]))
_max = i;
if (Abs (a[_max][c]) < eps) continue;
for (int i = c; i <= n + 1; i++) swap (a[r][i], a[_max][i]);
for (int i = n + 1; i >= c; i--) a[r][i] /= a[r][c];
for (int i = r + 1; i <= n; i++)
if (Abs (a[i][c]) > eps)
for (int j = n + 1; j >= c; j--)
a[i][j] -= a[r][j] * a[i][c];
r++;
}
if (r <= n) {
for (int i = r; i <= n; i++)
if (Abs (a[i][n + 1]) > eps)
return 2;
return 1;
}
else {
for (int i = n; i >= 1; i--)
for (int j = i + 1; j <= n; j++)
a[i][n + 1] -= a[j][n + 1] * a[i][j];
return 0;
}
}
高斯消元异或版。
int gauss () {
int r, c;
for (r = 1, c = 1; c <= n; c++) {
int _max = r;
for (int i = r; i <= n; i++)
if (a[i][c])
_max = i;
if (a[_max][c] == 0) continue;
for (int i = c; i <= n + 1; i++) swap (a[r][i], a[_max][i]);
for (int i = r + 1; i <= n; i++)
if (a[i][c])
for (int j = n + 1; j >= c; j--)
a[i][j] ^= a[r][j];
r++;
}
if (r <= n) {
for (int i = r; i <= n; i++)
if (a[i][n + 1])
return 2;
return 1;
}
else {
for (int i = n; i >= 1; i--)
for (int j = i + 1; j <= n; j++)
a[i][n + 1] ^= a[j][n + 1] & a[i][j];
return 0;
}
}