Cod sursa(job #3253609)

Utilizator Cezar2009Cezar Mihai Titihazan Cezar2009 Data 3 noiembrie 2024 20:05:38
Problema Algoritmul lui Gauss Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.76 kb
//https://infoarena.ro/problema/gauss
//#pragma GCC optimize ("Ofast")
//#pragma GCC optimize ("fast-math")
//#pragma GCC optimize ("unroll-loops")
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
using namespace std;


ifstream fin("gauss.in");
ofstream fout("gauss.out");

double mat[305][305];
int main()
{
	ios_base::sync_with_stdio(false);
	//cin.tie(NULL);
	int n, m, i, j;
	vector <int> rez;

	fin >> n >> m;

	for (i = 1; i <= n; ++i)
	{
		for (j = 1; j <= m + 1; ++j)
		{
			fin >> mat[i][j];
			//cout << mat[i][j] << " ";
		}
		//cout << "\n";
	}

	int c, r;
	double rp;
	for (r = 1, c = 1; ((r <= n) && (c <= m)); ++r, ++c)
	{
		//cout << r << " " << c << "\n";
		if ((((-1e-8) <= mat[r][c]) && (((1e-8) >= mat[r][c]))))
		{
			for (i = r + 1; i <= n; ++i)
			{
				if ((((-1e-8) <= mat[i][c]) && (((1e-8) >= mat[i][c]))))
				{
					break;
				}
			}

			if (i > n)
			{
				rez.push_back(0);
				continue;
			}
			else
			{
				//cout << "facut";
				swap(mat[r], mat[i]);
			}
		}

		for (i = 1; i <= n; ++i)
		{
			if (i != r)
			{
				rp = mat[i][c] / mat[r][c];
				//cout << i << " " << c << " " << r << "\n";
				//cout << mat[i][c] << " " << mat[r][c] << " " << "\n";
				for (j = 1; j <= m + 1; ++j)
				{
					mat[i][j] = mat[i][j] - rp * mat[r][j];
					//cout << mat[i][j] << " ";
				}

				//cout << "\n";
			}
		}

		//cout << "\n";
		
		rez.push_back(r);
		//cout << "++rez " << r+1 << "\n";
		//cout << r+1 << " " << c+1 << "\n\n";
	}
	
	

	fout << setprecision(10) << fixed;
	i = 1;
	for (int x : rez)
	{
		fout << (x ? (mat[x][m + 1] / mat[x][i]) : 0.0000000000) << " ";
		//cout << mat[x][m + 1] << " " << mat[x][i] << "\n";
		++i;
	}

	return 0;
}