Cod sursa(job #1365366)

Utilizator DuxarFII-Stefan-Negrus Duxar Data 28 februarie 2015 11:36:19
Problema Algoritmul lui Gauss Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 3.12 kb
#ifdef ONLINE_JUDGE
#include <bits/stdc++.h>
#else
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <cstdlib>
#endif

using namespace std;

	// lambda : [] (int a, int b) -> bool { body return }
	// string r_str = R"(raw string)"

#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define pb push_back
#define LL long long
#define ULL unsigned long long
#define BASE 73
#define NMAX 10000
#define NMAX2 20001
#define MOD1 1000000007
#define EPS 0.00000001
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
#define CRLINE Duxar(__LINE__);
#define SHOWME(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl;
#define ENTER putchar('\n');

int dx4[] = {-1, 0, 1, 0};
int dy4[] = {0, 1, 0, -1};

int dx6[] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dy6[] = {0, 1, 1, 1, 0, -1, -1, -1};

void Duxar(int _this_line) {
#ifndef ONLINE_JUDGE
	printf("\n . . . . . . . . . . . . . Passed line - %d\n", _this_line);
#endif
}

template <class T>
void ReadNo(T &_value) {
	T _sign = 1;
	char ch;
	_value = 0;
	while(!isdigit(ch = getchar())) {
		(ch == '-') && (_sign = -1);
	}
	do {
		_value = _value * 10 + (ch - '0');
	} while(isdigit(ch = getchar()));
	_value *= _sign;
}

template <class T>
void AddNr(T &a, T b) {
	a = a + b;
	while (a >= MOD1) {
		a -= MOD1;
	}
	while (a < 0) {
		a += MOD1;
	}
}

int N, M;
vector <int> first_not_z;
vector <vector <double> > mat;
vector <double> result, ans;

void SwapLines(int i, int j) {
	int k;
	swap(result[i], result[j]);
	for (k = 0; k < M; ++k) {
		swap(mat[i][k], mat[j][k]);
	}
}

void DivideLine(int i, double val) {
	int j;
	result[i] /= val;
	for (j = 0; j < M; ++j) {
		mat[i][j] /= val;
	}
}

void SubstractLine(int i, int j) {
	int k;
	for (k = i + 1; k < N; ++k) {
		if (mat[k][j] != 0) {
			double z = mat[k][j];
			result[k] -= result[i] * z;
			for (int q = j; q < M; ++q) {
				mat[k][q] -= z * mat[i][q];
			}
		}
	}
}

int main(){
#ifdef INFOARENA
	freopen("gauss.in", "r", stdin);
	freopen("gauss.out", "w", stdout);
#else
#ifndef ONLINE_JUDGE

#endif
#endif
	
	int i, j, k;
	
	ReadNo(N); ReadNo(M);
	mat.resize(N, vector<double> (M));
	result.resize(N);
	ans.resize(M, 0);
	first_not_z.resize(N, -1);
	
	for (i = 0; i < N; ++i) {
		for (j = 0; j < M; ++j) {
			ReadNo(mat[i][j]);
		}
		ReadNo(result[i]);
	}
	
	i = 0; j = 0;
	while (i < N && j < M) {
		if (mat[i][j] == 0) {
			for (k = i + 1; k < N; ++k) {
				if (mat[k][j] != 0) {
					SwapLines(i, k);
					break;
				}
			}
			
			if (k == N) {
				ans[j] = 0;
				++j;
				continue;
			}
		}
		
		DivideLine(i, mat[i][j]);
		SubstractLine(i, j);
		first_not_z[i] = j;
		++i; ++j;
	}
	
	for (i = N - 1; i >= 0; --i) {
		if (first_not_z[i] == -1 && !(fabs(result[i] - 0) < EPS)) {
			printf("Imposibil\n");
			return 0;
		}
		for (j = first_not_z[i] + 1; j < M; ++j) {
			result[i] -= ans[j] * mat[i][j];
		}
		ans[first_not_z[i]] = result[i] / mat[i][first_not_z[i]];
	}
	
	for (i = 0; i < M; ++i) {
		printf("%.9lf ", ans[i]);
	}
	
	
	
	return 0;
}