// Author: Kajetan Ramsza #include "bits/stdc++.h" using namespace std; template<typename F, typename S> ostream& operator<<(ostream& os, const pair<F, S> &p) { return os<<"("<<p.first<<", "<<p.second<<")"; } template<typename T> ostream &operator<<(ostream & os, const vector<T> &v) { os << "{"; typename vector< T > :: const_iterator it; for( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << *it; } return os << "}"; } void dbg_out() { cerr<<'\n'; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr<<' '<<H; dbg_out(T...); } #ifdef DEBUG #define dbg(...) cerr<<"(" << #__VA_ARGS__ <<"):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif typedef long long ll; typedef long double ld; struct Board { int n, m; vector<vector<bool>> vec; Board() {} Board(int n_, int m_) : n(n_), m(m_) { vec.assign(n, vector<bool>(m)); } int read() { int res = 0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { char c; cin>>c; vec[i][j] = c == 'O'; res += vec[i][j]; } } return res; } // void read(const vector<bool> &val) { // assert((int)val.size() == n*m); // for(int i=0;i<n;i++) { // for(int j=0;j<m;j++) { // vec[i][j] = val[n*i + j]; // } // } // } bool parity() { int res = 0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(vec[i][j]) res += i + j; } } return res % 2; } bool empty(int i, int j) { if(n <= i || i < 0) return false; if(m <= j || j < 0) return false; return !vec[i][j]; } int infl() { int res = 0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(vec[i][j]) { res += empty(i-1, j); res += empty(i, j-1); res += empty(i+1, j); res += empty(i, j+1); } } } return res; } }; int n, m; int pawns; Board board; Board goal; bool parity; ll sum; const int maxnm = 66; const int maxp = 9; ll nchoosek[maxnm][maxp]; void preprocess() { nchoosek[0][0] = 1; for(int i=1;i<maxnm;i++) { for(int j=0;j<=min(maxp-1, i);j++) { nchoosek[i][j] = 1; for(ll k=i-j+1;k<=i;k++) { nchoosek[i][j] *= k; } for(ll k=2;k<=j;k++) nchoosek[i][j] /= k; dbg(i, j, nchoosek[i][j]); } } } ll infl(int i, int j) { dbg("-------------", i, j); ll result = 0; int even = (n*m+1) / 2; int odd = (n*m) / 2; if((i+j) % 2) odd--; else even--; for(int l=0;l<2;l++) { for(int r=0;r<2;r++) { for(int u=0;u<2;u++) { for(int d=0;d<2;d++) { int pawn = pawns - 1; int e = even; int o = odd; bool p = parity ^ ((i+j) % 2); if(i == 0 && u == 1) continue; if(i == n-1 && d == 1) continue; if(j == 0 && l == 1) continue; if(j == m-1 && r == 1) continue; dbg("NOT SKIPPED"); if(i != 0) { if((i+j-1) % 2) o--; else e--; } if(i != n-1) { if((i+j+1) % 2) o--; else e--; } if(j != 0) { if((i+j-1) % 2) o--; else e--; } if(j != m-1) { if((i+j+1) % 2) o--; else e--; } if(l == 1) { p ^= ((i+j-1) % 2); pawn--; } if(r == 1) { p ^= ((i+j+1) % 2); pawn--; } if(u == 1) { p ^= ((i+j-1) % 2); pawn--; } if(d == 1) { p ^= ((i+j+1) % 2); pawn--; } dbg(p, l, r, u, d, e, o, pawn); if(pawn < 0) continue; ll res = 0; for(int k=p;k<=min(o,pawn);k+=2) { dbg(o, e, k); dbg(e , pawn - k); dbg(nchoosek[o][k], nchoosek[e][pawn - k]); if(pawn - k > e) continue; res += nchoosek[o][k] * nchoosek[e][pawn - k]; } ll num = 0; if(i != 0 && u == 0) num++; if(i != n-1 && d == 0) num++; if(j != 0 && l == 0) num++; if(j != m-1 && r == 0) num++; dbg(res, num); result += res * num; } } } } dbg(result); return result; } void solve() { preprocess(); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { sum += infl(i, j); } } // sum /= pawns; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>n>>m; board = Board(n, m); pawns = board.read(); parity = board.parity(); dbg(parity); goal = Board(n, m); goal.read(); if(parity != goal.parity()) { cout<<0<<'\n'; return 0; } solve(); ll infl = goal.infl(); dbg(infl, sum); cout<<fixed<<setprecision(15); cout<<(ld)infl / (ld)sum<<'\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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | // Author: Kajetan Ramsza #include "bits/stdc++.h" using namespace std; template<typename F, typename S> ostream& operator<<(ostream& os, const pair<F, S> &p) { return os<<"("<<p.first<<", "<<p.second<<")"; } template<typename T> ostream &operator<<(ostream & os, const vector<T> &v) { os << "{"; typename vector< T > :: const_iterator it; for( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << *it; } return os << "}"; } void dbg_out() { cerr<<'\n'; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr<<' '<<H; dbg_out(T...); } #ifdef DEBUG #define dbg(...) cerr<<"(" << #__VA_ARGS__ <<"):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif typedef long long ll; typedef long double ld; struct Board { int n, m; vector<vector<bool>> vec; Board() {} Board(int n_, int m_) : n(n_), m(m_) { vec.assign(n, vector<bool>(m)); } int read() { int res = 0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { char c; cin>>c; vec[i][j] = c == 'O'; res += vec[i][j]; } } return res; } // void read(const vector<bool> &val) { // assert((int)val.size() == n*m); // for(int i=0;i<n;i++) { // for(int j=0;j<m;j++) { // vec[i][j] = val[n*i + j]; // } // } // } bool parity() { int res = 0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(vec[i][j]) res += i + j; } } return res % 2; } bool empty(int i, int j) { if(n <= i || i < 0) return false; if(m <= j || j < 0) return false; return !vec[i][j]; } int infl() { int res = 0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(vec[i][j]) { res += empty(i-1, j); res += empty(i, j-1); res += empty(i+1, j); res += empty(i, j+1); } } } return res; } }; int n, m; int pawns; Board board; Board goal; bool parity; ll sum; const int maxnm = 66; const int maxp = 9; ll nchoosek[maxnm][maxp]; void preprocess() { nchoosek[0][0] = 1; for(int i=1;i<maxnm;i++) { for(int j=0;j<=min(maxp-1, i);j++) { nchoosek[i][j] = 1; for(ll k=i-j+1;k<=i;k++) { nchoosek[i][j] *= k; } for(ll k=2;k<=j;k++) nchoosek[i][j] /= k; dbg(i, j, nchoosek[i][j]); } } } ll infl(int i, int j) { dbg("-------------", i, j); ll result = 0; int even = (n*m+1) / 2; int odd = (n*m) / 2; if((i+j) % 2) odd--; else even--; for(int l=0;l<2;l++) { for(int r=0;r<2;r++) { for(int u=0;u<2;u++) { for(int d=0;d<2;d++) { int pawn = pawns - 1; int e = even; int o = odd; bool p = parity ^ ((i+j) % 2); if(i == 0 && u == 1) continue; if(i == n-1 && d == 1) continue; if(j == 0 && l == 1) continue; if(j == m-1 && r == 1) continue; dbg("NOT SKIPPED"); if(i != 0) { if((i+j-1) % 2) o--; else e--; } if(i != n-1) { if((i+j+1) % 2) o--; else e--; } if(j != 0) { if((i+j-1) % 2) o--; else e--; } if(j != m-1) { if((i+j+1) % 2) o--; else e--; } if(l == 1) { p ^= ((i+j-1) % 2); pawn--; } if(r == 1) { p ^= ((i+j+1) % 2); pawn--; } if(u == 1) { p ^= ((i+j-1) % 2); pawn--; } if(d == 1) { p ^= ((i+j+1) % 2); pawn--; } dbg(p, l, r, u, d, e, o, pawn); if(pawn < 0) continue; ll res = 0; for(int k=p;k<=min(o,pawn);k+=2) { dbg(o, e, k); dbg(e , pawn - k); dbg(nchoosek[o][k], nchoosek[e][pawn - k]); if(pawn - k > e) continue; res += nchoosek[o][k] * nchoosek[e][pawn - k]; } ll num = 0; if(i != 0 && u == 0) num++; if(i != n-1 && d == 0) num++; if(j != 0 && l == 0) num++; if(j != m-1 && r == 0) num++; dbg(res, num); result += res * num; } } } } dbg(result); return result; } void solve() { preprocess(); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { sum += infl(i, j); } } // sum /= pawns; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>n>>m; board = Board(n, m); pawns = board.read(); parity = board.parity(); dbg(parity); goal = Board(n, m); goal.read(); if(parity != goal.parity()) { cout<<0<<'\n'; return 0; } solve(); ll infl = goal.infl(); dbg(infl, sum); cout<<fixed<<setprecision(15); cout<<(ld)infl / (ld)sum<<'\n'; } |