#include<bits/stdc++.h> #define watch(x) std::cout << (#x) << " is " << (x) << std::endl using LL = longlong; using pii = std::pair<int, int>; using pll = std::pair<LL, LL>;
std::vector<double> Gauss(std::vector<std::vector<double>> A, std::vector<double> b){ int n = A.size(), m = A[0].size(); std::vector<double> x(m); std::vector<int> p(m); std::iota(p.begin(), p.end(), 0); constdouble eps = 1e-12; auto findNonZero = [&](int i) { // 实际上找最大的比较好 for (int row = i; row < n; ++row) if (fabs(A[row][i]) > eps) return row; return n; }; auto triangleGauss = [&](int sz) { // A[i][i] = 1 std::vector<double> x(sz); for (int i = sz - 1; i >=0; --i) { x[i] = b[i]; for (int row = 0; row < i; ++row) b[row] -= A[row][i] * x[i]; } x.resize(A[0].size()); return x; }; int sz = n; for (int i = 0, row; i < n; ++i) { while (i < m) { row = findNonZero(i); if (row != n) break; for (int j = 0; j < n; ++j) A[j][i] = A[j][m - 1]; std::swap(p[i], p[--m]); } if (i == m) { for (int row = m; row < n; ++row) if (fabs(b[row])) { // std::cout << "\nNo answer\n"; returnstd::vector<double>(); } sz = i; break; } if (row != i) { std::swap(A[row], A[i]); std::swap(b[row], b[i]); } b[i] /= A[i][i]; for (int j = m - 1; j >= i; --j) A[i][j] /= A[i][i]; for (int row = i + 1; row < n; ++row) { b[row] -= A[row][i] * b[i]; for (int j = m - 1; j >= i; --j) { A[row][j] -= A[row][i] * A[i][j]; } } } // if (sz != A[0].size()) std::cout << "\nInfinite answer\n"; auto xt = triangleGauss(sz); for (int t = 0; t < A[0].size(); ++t) x[p[t]] = xt[t]; return x; }
intmain(){ // freopen("in","r",stdin); std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int cas = 1; std::cin >> cas; while (cas--) { int n, m; std::cin >> n >> m; std::vector<std::vector<double>> a(n, std::vector<double>(m, 0)); for (auto &x : a) { for (auto &i : x) std::cin >> i; } std::vector<double> b(n); for (auto &x : b) std::cin >> x; auto x = Gauss(a, b); for (auto t : x) std::cout << t << "\n"; } return0; }
#include<bits/stdc++.h> #define watch(x) std::cout << (#x) << " is " << (x) << std::endl using LL = longlong; using pii = std::pair<int, int>; using pll = std::pair<LL, LL>;
std::vector<LL> Gauss(std::vector<std::vector<LL>> A, std::vector<LL> b){ int n = A.size(), m = A[0].size(); std::vector<LL> x(m); std::vector<int> p(m); std::iota(p.begin(), p.end(), 0); const LL M = 998244353; std::function<LL(LL)> inv = [&](LL a) -> LL { return a == 1 ? 1 : (M - M / a) * inv(M % a) % M; }; auto sub = [](LL &x, LL y) { (x -= y) < 0 && (x += M); }; auto triangleGauss = [&](int sz) { // A[i][i] = 1 std::vector<LL> x(sz); for (int i = sz - 1; i >=0; --i) { x[i] = (b[i] + M) % M; for (int row = 0; row < i; ++row) sub(b[row], A[row][i] * x[i] % M); } x.resize(A[0].size()); return x; }; auto findNonZero = [&](int i) { for (int row = i; row < n; ++row) if (A[row][i]) return row; return n; }; int sz = n; for (int i = 0, row; i < n; ++i) { while (i < m) { row = findNonZero(i); if (row != n) break; for (int j = 0; j < n; ++j) A[j][i] = A[j][m - 1]; std::swap(p[i], p[--m]); } if (i == m) { for (int row = m; row < n; ++row) if (b[row]) { // std::cout << "\nNo answer\n"; returnstd::vector<LL>(); } sz = i; break; } if (row != i) { std::swap(A[i], A[row]); std::swap(b[i], b[row]); } LL inva = inv(A[i][i]); (b[i] *= inva) %= M; for (int j = m - 1; j >= i; --j) (A[i][j] *= inva) %= M; for (int row = i + 1; row < n; ++row) { sub(b[row], A[row][i] * b[i] % M); for (int j = m - 1; j >= i; --j) { sub(A[row][j], A[row][i] * A[i][j] % M); } } } // if (sz != A[0].size()) std::cout << "\nInfinite answer\n"; auto xt = triangleGauss(sz); for (int t = 0; t < A[0].size(); ++t) x[p[t]] = xt[t]; return x; }
intmain(){ // freopen("in","r",stdin); std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int cas = 1; std::cin >> cas; while (cas--) { int n, m; std::cin >> n >> m; std::vector<std::vector<LL>> a(n, std::vector<LL>(m, 0)); for (auto &x : a) { for (auto &i : x) std::cin >> i; } std::vector<LL> b(n); for (auto &x : b) std::cin >> x; auto x = Gauss(a, b); for (auto t : x) std::cout << t << "\n"; } return0; }