結果

問題 No.1175 Simultaneous Equations
ユーザー tkmst201tkmst201
提出日時 2021-01-12 18:22:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 7,070 bytes
コンパイル時間 2,188 ms
コンパイル使用メモリ 209,048 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-01 07:13:04
合計ジャッジ時間 2,837 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) begin(v),end(v)
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = false;
//---------------------------------//

struct Matrix {
public:
	using value_type = double;
	using size_type = std::size_t;
	
	Matrix() {}
	Matrix(size_type h, size_type w, const value_type & x = 0) : h(h), w(w), val(h, std::vector<value_type>(w, x)) {
		assert(h > 0 && w > 0);
	}
	Matrix(std::vector<std::vector<value_type>> val) : h(val.size()), w(val.size() ? val[0].size() : 0), val(val) {
		assert(h > 0 && w > 0);
		for (size_type i = 1; i < h; ++i) assert(val[i].size() == w);
	}
	Matrix(std::initializer_list<std::vector<value_type>> init) : val(init.begin(), init.end()) {
		h = val.size();
		w = val.size() ? val[0].size() : 0;
		assert(h > 0 && w > 0);
		for (size_type i = 1; i < h; ++i) assert(val[i].size() == w);
	}
	
	std::vector<value_type> & operator [](size_type i) noexcept { return val[i]; }
	const std::vector<value_type> & operator [](size_type i) const noexcept { return val[i]; };
	value_type & operator ()(size_type i, size_type j) noexcept { return val[i][j]; };
	const value_type & operator ()(size_type i, size_type j) const noexcept { return val[i][j]; }
	value_type & at(size_type i, size_type j) {
		assert(i < h && j < w);
		return val[i][j];
	}
	const value_type & at(size_type i, size_type j) const {
		assert(i < h & j < w);
		return val[i][j];
	}
	
	bool empty() const { return !(h || w); }
	std::pair<size_type, size_type> type() const { return std::make_pair(h, w); }
	bool match_type(const Matrix & rhs) const noexcept { return h == rhs.h && w == rhs.w; }
	bool is_square() const { return h == w; }
	const std::vector<std::vector<value_type>> & get() const noexcept { return val; }
	
	bool operator ==(const Matrix & rhs) const noexcept { return match_type(rhs) && val == rhs.val; }
	bool operator !=(const Matrix & rhs) const noexcept { return !(*this == rhs); }
	Matrix operator +() const { return Matrix(*this); }
	Matrix operator -() const { return Matrix(h, w) - Matrix(*this); }
	Matrix operator +(const Matrix & rhs) const { return Matrix(*this) += rhs; }
	Matrix operator -(const Matrix & rhs) const { return Matrix(*this) -= rhs; }
	Matrix operator *(const Matrix & rhs) const {
		assert(w == rhs.h);
		Matrix res(h, rhs.w);
		for (size_type i = 0; i < h; ++i) for (size_type j = 0; j < rhs.w; ++j) for (size_type k = 0; k < w; ++k)
			res.val[i][j] += val[i][k] * rhs.val[k][j];
		return res;
	}
	Matrix operator /(const Matrix & rhs) const { return Matrix(*this) /= rhs; }
	friend Matrix operator *(const value_type & lhs, const Matrix & rhs) {
		Matrix res(rhs.val);
		for (size_type i = 0; i < res.h; ++i) for (size_type j = 0; j < res.w; ++j)
			res.val[i][j] = lhs * res.val[i][j];
		return res;
	}
	Matrix operator *(const value_type & rhs) const {
		Matrix res(val);
		for (size_type i = 0; i < h; ++i) for (size_type j = 0; j < w; ++j)
			res.val[i][j] *= rhs;
		return res;
	}
	Matrix operator /(const value_type & rhs) const {
		Matrix res(val);
		for (size_type i = 0; i < h; ++i) for (size_type j = 0; j < w; ++j)
			res.val[i][j] /= rhs;
		return res;
	}
	Matrix & operator +=(const Matrix & rhs) {
		assert(match_type(rhs));
		for (size_type i = 0; i < h; ++i) for (size_type j = 0; j < w; ++j)
			val[i][j] += rhs.val[i][j];
		return *this;
	}
	Matrix & operator -=(const Matrix & rhs) {
		assert(match_type(rhs));
		for (size_type i = 0; i < h; ++i) for(size_type j = 0; j < w; ++j)
			val[i][j] -= rhs.val[i][j];
		return *this;
	}
	Matrix & operator *=(const Matrix & rhs) {
		*this = *this * rhs;
		return *this;
	}
	Matrix & operator /=(const Matrix & rhs) {
		*this *= rhs.inverse();
		return *this;
	}
	
	Matrix pow(long long n) const {
		Matrix res = identity(h), x = *this;
		if (n < 0) { x = x.inverse(); n = -n; }
		while (n) { if (n & 1) res *= x; x *= x; n >>= 1; }
		return res;
	}
	
	Matrix trans() const {
		Matrix res(w, h);
		for (size_type i = 0; i < h; ++i) for (size_type j = 0; j < w; ++j)
			res.val[j][i] = val[i][j];
		return res;
	}
	
	Matrix inverse() const {
		assert(is_square());
		Matrix aug_mat = this->hstack(identity(h));
		if (aug_mat.gauss_jordan().first != h) return Matrix();
		return aug_mat.submat(0, w, h, 2 * w);
	}
	
	Matrix vstack(const Matrix & A) const {
		assert(w == A.w);
		Matrix res(h + A.h, w);
		std::copy(val.begin(), val.end(), res.val.begin());
		std::copy(A.val.begin(), A.val.end(), res.val.begin() + h);
		return res;
	}
	
	Matrix hstack(const Matrix & A) const {
		assert(h == A.h);
		Matrix res(h, w + A.w);
		for (int i = 0; i < h; ++i) {
			std::copy(val[i].begin(), val[i].end(), res.val[i].begin());
			std::copy(A.val[i].begin(), A.val[i].end(), res.val[i].begin() + w);
		}
		return res;
	}
	
	Matrix submat(size_type i1, size_type j1, size_type i2, size_type j2) const {
		assert(i1 < i2 && j1 < j2 && i2 <= h && j2 <= w);
		Matrix res(i2 - i1, j2 - j1);
		for (size_type i = 0; i < i2 - i1; ++i)
			std::copy(val[i + i1].begin() + j1, val[i + i1].begin() + j2, res.val[i].begin());
		return res;
	}
	
	static Matrix identity(size_type n) {
		Matrix res(n, n);
		for (size_type i = 0; i < n; ++i) res(i, i) = 1;
		return res;
	}
	
	std::pair<size_type, value_type> gauss_jordan(size_type colnum = -1) {
		if (colnum == -1) colnum = w;
		size_type rank = 0;
		value_type det = 1;
		
		for (size_type k = 0; k < colnum; ++k) {
			size_type pivot = -1;
			value_type mx = EPS;
			for (size_type i = rank; i < h; ++i) {
				value_type cur = std::abs(val[i][k]);
				if (cur > mx) {
					mx = cur;
					pivot = i;
				}
			}
			if (pivot == -1) continue;
			if (pivot != rank) {
				det *= -1;
				std::swap(val[rank], val[pivot]);
			}
			
			value_type div = val[rank][k];
			det *= div;
			for (size_type j = k; j < w; ++j) val[rank][j] /= div;
			
			for (size_type i = 0; i < h; ++i) if (i != rank) {
				for (size_type j = k + 1; j < w; ++j) val[i][j] -= val[rank][j] * val[i][k];
				val[i][k] = 0;
			}
			++rank;
		}
		
		if (!is_square() || rank != h) det = 0;
		
		return {rank, det}
		;
	}
	
	friend std::ostream & operator <<(std::ostream & os, const Matrix & rhs) {
		os << "type = (" << rhs.h << "," << rhs.w << ") [\n";
		for (size_type i = 0; i < rhs.h; ++i) for (size_type j = 0; j < rhs.w; ++j)
			os << (j == 0 ? " " : "") << rhs.val[i][j] << (j + 1 == rhs.w ? '\n' : ' ');
		return os << "]";
	}
	
private:
	static constexpr value_type EPS = 1e-6;
	size_type h, w;
	std::vector<std::vector<value_type>> val;
};

int main() {
	Matrix mat(2, 3);
	REP(i, 2) REP(j, 3) cin >> mat[i][j];
	mat.gauss_jordan();
	printf("%.15f %.15f\n", mat[0][2], mat[1][2]);
}
0