Pagini recente » Cod sursa (job #434994) | Cod sursa (job #1854660) | Cod sursa (job #2570496) | Istoria paginii runda/dedicatie_speciala9/clasament | Cod sursa (job #2409227)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300;
const double eps = 1e-10;
vector<vector<double>> a;
int main() {
freopen("gauss.in", "r", stdin);
freopen("gauss.out", "w", stdout);
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; ++i) {
vector<double>v(m + 1);
for (int i = 0; i <= m; ++i) {
int x;
scanf("%d", &x);
v[i] = x;
}
a.push_back(v);
}
vector<double>ans(m);
vector<int>p(m, -1);
int k = 0;
for (int i = 0; i < m && k < n; ++i) {
int pos = k;
for (int j = k + 1; j < n; ++j)
if (fabs(a[j][i]) > fabs(a[pos][i]))
pos = j;
if (fabs(a[pos][i]) < eps)
continue;
swap(a[i], a[pos]);
double coef = 1. / a[i][i];
for (int j = i; j <= m; ++j)
a[i][j] *= coef;
for (int j = 0; j < n; ++j)
if (j != i) {
coef = a[j][i];
for (int t = i; t <= m; ++t)
a[j][t] -= a[i][t] * coef;
}
/*cerr << "------>" << i << '\n';
for (auto it:a) {
for (auto it1:it)
cerr << it1 << ' ';
cerr << '\n';
}
cerr << '\n';*/
p[i] = k;
k++;
}
for (int i = k; i < n; ++i)
if (fabs(a[i][m]) <= eps) {
printf("Imposibil!");
return 0;
}
for (int i = 0; i < m; ++i)
if (p[i] != -1)
ans[i] = a[p[i]][m];
for (auto it:ans)
cout << it << ' ';
return 0;
}