#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; const double EPS = 1e-9; const int INF = 2; // it doesn't actually have to be infinity or a big number //~ https://cp-algorithms.com/linear_algebra/linear-system-gauss.html#implementation int gauss (vector < vector<double> > a, vector<double> & ans) { int n = (int) a.size(); int m = (int) a[0].size() - 1; vector<int> where (m, -1); for (int col=0, row=0; col<m && row<n; ++col) { int sel = row; for (int i=row; i<n; ++i) if (abs (a[i][col]) > abs (a[sel][col])) sel = i; if (abs (a[sel][col]) < EPS) continue; for (int i=col; i<=m; ++i) swap (a[sel][i], a[row][i]); where[col] = row; for (int i=0; i<n; ++i) if (i != row) { double c = a[i][col] / a[row][col]; for (int j=col; j<=m; ++j) a[i][j] -= a[row][j] * c; } ++row; } ans.assign (m, 0); for (int i=0; i<m; ++i) if (where[i] != -1) ans[i] = a[where[i]][m] / a[where[i]][i]; for (int i=0; i<n; ++i) { double sum = 0; for (int j=0; j<m; ++j) sum += ans[j] * a[i][j]; if (abs (sum - a[i][m]) > EPS) return 0; } for (int i=0; i<m; ++i) if (where[i] == -1) return INF; return 1; } unordered_map<ull, int>mapa; void gen(int i, int mx, int p, ull m) { if (!p) { //~ cout<<mapa.size()<<" "<<bitset<5>(m)<<"\n"; mapa[m]=mapa.size(); return; } gen(i+1, mx, p-1, m|(1ull<<i)); if (mx-i-1>=p) gen(i+1, mx, p, m); } int main() { //~ vector<vector<double>>v={{1, 1, 3}, {1, -1, -1}}; //~ vector<double>odp; //~ gauss(v, odp); //~ cout<<odp[0]<<" "<<odp[1]; //~ return 0; ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, p=0, npos=0; cin>>n>>m; ull lmsk=0; for (int qq=0; qq<2; qq++) { for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { char c; cin>>c; if (c=='O') { p+=!qq; lmsk|=(1ull<<(i*m+j))*qq; npos+=(qq? 1 : -1)*((i+j)&1); } } } } if (npos) { cout<<"0\n"; return 0; } gen(0, n*m, p, 0); vector<vector<double>>sys(mapa.size()+1, vector<double>(mapa.size()+1, 0)); for (int i=0; i<(int)mapa.size(); i++) sys[i][i]=-1, sys.back()[i]=1; sys.back().back()=2; for (auto i : mapa) { ull msk=i.first; //~ cout<<"XD "<<i.first<<" "<<i.second<<"\n"; vector<ull>pol; for (int j=0; j<n*m; j++) { if (msk&(1ull<<j)) { ull b=1ull<<j; msk^=b; if (j-m>=0 && !(msk&(b>>m))) pol.push_back(msk^(b>>m)); if (j+m<n*m && !(msk&(b<<m))) pol.push_back(msk^(b<<m)); if (j%m!=0 && !(msk&(b>>1))) pol.push_back(msk^(b>>1)); if (j%m!=m-1 && !(msk&(b<<1))) pol.push_back(msk^(b<<1)); msk^=b; } } //~ for (auto j : pol) //~ { //~ cout<<i.second<<" "<<bitset<5>(msk)<<" "<<bitset<5>(j)<<"\n"; //~ } //~ cout<<"\n"; double k=pol.size(); for (auto j : pol) { sys[mapa[j]][i.second]=1./k; } } vector<double>ans; //~ for (auto i : sys) //~ { //~ for (auto j : i) //~ { //~ cout<<fixed<<setprecision(1)<<j<<" "; //~ } //~ cout<<"\n"; //~ } gauss(sys, ans); //~ for (auto i : ans) cout<<fixed<<setprecision(3)<<i<<"\n"; cout<<fixed<<setprecision(13)<<ans[mapa[lmsk]]<<"\n"; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | #include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; const double EPS = 1e-9; const int INF = 2; // it doesn't actually have to be infinity or a big number //~ https://cp-algorithms.com/linear_algebra/linear-system-gauss.html#implementation int gauss (vector < vector<double> > a, vector<double> & ans) { int n = (int) a.size(); int m = (int) a[0].size() - 1; vector<int> where (m, -1); for (int col=0, row=0; col<m && row<n; ++col) { int sel = row; for (int i=row; i<n; ++i) if (abs (a[i][col]) > abs (a[sel][col])) sel = i; if (abs (a[sel][col]) < EPS) continue; for (int i=col; i<=m; ++i) swap (a[sel][i], a[row][i]); where[col] = row; for (int i=0; i<n; ++i) if (i != row) { double c = a[i][col] / a[row][col]; for (int j=col; j<=m; ++j) a[i][j] -= a[row][j] * c; } ++row; } ans.assign (m, 0); for (int i=0; i<m; ++i) if (where[i] != -1) ans[i] = a[where[i]][m] / a[where[i]][i]; for (int i=0; i<n; ++i) { double sum = 0; for (int j=0; j<m; ++j) sum += ans[j] * a[i][j]; if (abs (sum - a[i][m]) > EPS) return 0; } for (int i=0; i<m; ++i) if (where[i] == -1) return INF; return 1; } unordered_map<ull, int>mapa; void gen(int i, int mx, int p, ull m) { if (!p) { //~ cout<<mapa.size()<<" "<<bitset<5>(m)<<"\n"; mapa[m]=mapa.size(); return; } gen(i+1, mx, p-1, m|(1ull<<i)); if (mx-i-1>=p) gen(i+1, mx, p, m); } int main() { //~ vector<vector<double>>v={{1, 1, 3}, {1, -1, -1}}; //~ vector<double>odp; //~ gauss(v, odp); //~ cout<<odp[0]<<" "<<odp[1]; //~ return 0; ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, p=0, npos=0; cin>>n>>m; ull lmsk=0; for (int qq=0; qq<2; qq++) { for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { char c; cin>>c; if (c=='O') { p+=!qq; lmsk|=(1ull<<(i*m+j))*qq; npos+=(qq? 1 : -1)*((i+j)&1); } } } } if (npos) { cout<<"0\n"; return 0; } gen(0, n*m, p, 0); vector<vector<double>>sys(mapa.size()+1, vector<double>(mapa.size()+1, 0)); for (int i=0; i<(int)mapa.size(); i++) sys[i][i]=-1, sys.back()[i]=1; sys.back().back()=2; for (auto i : mapa) { ull msk=i.first; //~ cout<<"XD "<<i.first<<" "<<i.second<<"\n"; vector<ull>pol; for (int j=0; j<n*m; j++) { if (msk&(1ull<<j)) { ull b=1ull<<j; msk^=b; if (j-m>=0 && !(msk&(b>>m))) pol.push_back(msk^(b>>m)); if (j+m<n*m && !(msk&(b<<m))) pol.push_back(msk^(b<<m)); if (j%m!=0 && !(msk&(b>>1))) pol.push_back(msk^(b>>1)); if (j%m!=m-1 && !(msk&(b<<1))) pol.push_back(msk^(b<<1)); msk^=b; } } //~ for (auto j : pol) //~ { //~ cout<<i.second<<" "<<bitset<5>(msk)<<" "<<bitset<5>(j)<<"\n"; //~ } //~ cout<<"\n"; double k=pol.size(); for (auto j : pol) { sys[mapa[j]][i.second]=1./k; } } vector<double>ans; //~ for (auto i : sys) //~ { //~ for (auto j : i) //~ { //~ cout<<fixed<<setprecision(1)<<j<<" "; //~ } //~ cout<<"\n"; //~ } gauss(sys, ans); //~ for (auto i : ans) cout<<fixed<<setprecision(3)<<i<<"\n"; cout<<fixed<<setprecision(13)<<ans[mapa[lmsk]]<<"\n"; } |