Kyopro Library
 
読み取り中…
検索中…
一致する文字列を見つけられません
linear_equation.hpp
[詳解]
1#include"../../kyopro_library/template.hpp"
2
3/// @brief F_2 上の連立線形方程式
4/// @ref https://mathlandscape.com/solution-sp/
5/// @ref https://yukicoder.me/submissions/1011997
6/// @ref verify:https://yukicoder.me/problems/no/2895
7
8/// @brief 掃き出し法
9/// @param a 連立方程式 Ax=b の拡大係数行列
10/// @param where ピボットとなる変数を記録するための配列
11/// @return A のランク
12int RowReduction(vector<vector<bool>>& a, vector<int>& where) {
13 int row=a.size(),col=a.front().size();
14 int rank=0;
15 for(int c=0; c<col-1; c++) {
16 int pivot=rank;
17 while(pivot<row&&!a[pivot][c]) pivot++;
18 if(pivot==row) continue;
19 swap(a[pivot],a[rank]);
20 where.push_back(c);
21 for(int r=0; r<row; r++) {
22 if(r!=rank&&a[r][c]) {
23 //A[r]^=A[c]
24 for(int i=0; i<col; i++) a[r][i]=a[r][i]^a[rank][i];
25 }
26 }
27 rank++;
28 }
29 return rank;
30}
31
32/// @brief 連立線形方程式 Ax=b を解く
33/// @param x0 特殊解(b=0 の場合は自明解になる)
34/// @param ker Ax=0 の解空間の基底
35/// @note 一般解は x0 と解空間の基底の任意の線形結合で表される
36/// @attention A のサイズによっては基底のサイズが巨大になるので注意すること
37bool LinearEquation(vector<vector<bool>> a, vector<bool> b, vector<bool>& x0, vector<vector<bool>>& ker) {
38 int row=a.size(),col=a.front().size();
39 assert(b.size()==row);
40 vector<vector<bool>> a2=a;
41 for(int i=0; i<row; i++) a2[i].push_back(b[i]);
42
43 vector<int> where;
44 int rank=RowReduction(a2,where);
45
46 for(int r=rank; r<row; r++) if(a2[r].back()) return false;
47
48 x0=vector<bool>(col,false);
49 for(int i=0; i<rank; i++) x0[where[i]]=a2[i].back();
50
51 int r=0;
52 for(int c=0; c<col; c++) {
53 if(r<rank&&c==where[r]) {
54 r++;
55 continue;
56 }
57 vector<bool> x(col);
58 x[c]=true;
59 for(int r2=0; r2<r; r2++) x[where[r2]]=a2[r2][c];
60 ker.push_back(x);
61 }
62
63 return true;
64}
int RowReduction(vector< vector< bool > > &a, vector< int > &where)
F_2 上の連立線形方程式 https://mathlandscape.com/solution-sp/ https://yukicoder.me/submissions/1011997 verify:...
bool LinearEquation(vector< vector< bool > > a, vector< bool > b, vector< bool > &x0, vector< vector< bool > > &ker)
連立線形方程式 Ax=b を解く