//~ while (clock()<=69*CLOCKS_PER_SEC) //~ #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("O3") //~ #pragma GCC optimize("Ofast") //~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //~ #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; using ll=long long; using ld=long double; using pii=pair<int, int>; using pll=pair<ll, ll>; using vi=vector<int>; using vll=vector<ll>; template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; } #ifdef LOCAL #define _upgrade ios_base::sync_with_stdio(0); #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__); #else #define _upgrade ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define debug(...) 98; #define cerr if(0) cout #endif #define pb push_back #define mp make_pair #define all(x) x.begin(),x.end() #define sz(x) ((int)(x).size()) #define fi first #define se second #define erase_duplicates(x) sort(all(x)), (x).resize(distance((x).begin(), unique(all(x)))); #define watch(x) cout << (#x) << " is " << (x) << endl; #define pow2(x) ((x)*(x)) #define mod(x, m) ((((x) % (m)) + (m)) % (m)) #define max3(a, b, c) max(a, max(b, c)) #define min3(a, b, c) min(a, min(b, c)) #define REP(i, a, b) for(int i = a; i <= b; ++i) #define REPV(i, b, a) for(int i = b; i >= a; --i) const double m_pi = acos(-1.0); const int int_max = 0x3f3f3f3f; const int M7 = 1000*1000*1000+7; const int M9 = 1000*1000*1000+9; vector<int> emptyvi; vector<vector<int> > tab; vector<vector<int> > tab2; int main() { _upgrade mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); cout<<fixed<<setprecision(15); int n, m;cin>>n>>m; int parz = 0; int numPion = 0; int planszSize = n*m; REP(i,0,n-1){ string s; cin>>s; tab.pb(emptyvi); REP(j, 0, m-1) { if(s[j] == '.') tab[i].pb(0); if(s[j] == 'O') { tab[i].pb(1); parz += i + j; ++numPion; } } } int parz2 = 0; REP(i,0,n-1){ string s; cin>>s; tab2.pb(emptyvi); REP(j, 0, m-1) { if(s[j] == '.') tab2[i].pb(0); if(s[j] == 'O') { tab2[i].pb(1); parz2 += i + j; } } } if(parz%2 != parz2%2) { cout <<0<<"\n"; return 0; } if(planszSize == 1) { cout <<1<<"\n"; return 0; } long double myKraw=0; REP(i,0,n-1){ REP(j, 0, m-1) { if(tab2[i][j]){ if(i > 0 && !tab2[i-1][j])++myKraw; if(j > 0 && !tab2[i][j-1])++myKraw; if(i < n-1 && !tab2[i+1][j])++myKraw; if(j < m-1 && !tab2[i][j+1])++myKraw; } } } long double allPos = 1; int one =min(planszSize-numPion, numPion); int two =max(planszSize-numPion, numPion); REP(i, two+1, planszSize) { allPos*=i; } REP(i, 1, one) { allPos/=i; } long double allKraw = allPos * 4 * numPion; long double dimenKraw = allPos * 2 * numPion; long double krawPosn = dimenKraw / n; long double krawPosm = dimenKraw / m; long double otherPosn = dimenKraw * (n-1)*(numPion-1) / n / (planszSize-1); long double otherPosm = dimenKraw * (m-1)*(numPion-1) / m / (planszSize-1); long double existing = allKraw - krawPosn - krawPosm - otherPosn - otherPosm; long double ret = myKraw*2/existing; cout<<ret<<"\n"; // debug(allPos); // debug(allKraw); // debug(krawPosn); // debug(krawPosm); // debug(otherPosn); // debug(otherPosm); // debug(existing); // debug(myKraw); return 0; } /* __builtin_popcount count total set bits on a given integer (use __builtin_popcountll for long long). 4 6 ...C.. .B.C.B C.CB.C .B.C.. 103 DDDDDDGDGPPLPDLDPGLDPGLDPGDLPGLGPDLGDPLGDPLGDPLGPDLGPDLGPDLGDGPLDPLGLDLGPDLGPDLPDLGPDLGPDLGPDLGPDLGPDLG 4 2 .C C. BC C. 3 GDL 4 4 .BBB .BB. .BC. .CCB 6 LLDLDP 43 50 CBC...C.B.....C..BC.CBC...BCC.B.BB.B...B..CB.B...B .....BB.C.B.C.CCB.....CCC..C.CC..B..BC...BBCCBB..C .CB.......CB.C.B.BC.C..C.B..B.....B.C...CC.B.CB.B. .BBB.BCCC..CBBCBC.....C..B.B.CCB......CB.BC.BCC..C .....B.C.C..B.BBBC.C.....BCBC...CB....C.CC.C..B.B. B.BBCCB....BC.B...B.C.BC...BBB..CB.B...C..BCCBB..C .CBB...BCC....CC.CB..C.CC.C.CCB.B...BC.BBB..BB.BBC CBC..B.....BC.C..C...B.CBC..BCCC..BB.BBBB..BB...CC C.B...BB.B..C...C.CBBBCCBB..C.BCB..C.CC...B...BC.B .C..C..BB..C.C...C.C..BC.CCB...........CC....CCCBB BC......B..B.B.C.C.B..B..B..BB.BC.BC.CC....CB.BC.B ....BC......C.BBBB.CB..CC..B....B..C...CB..B...C.C BB.B.C.C.C.C..B...C......BC....BBBCBB...BB...B.... B..CBB...B.CB..B.B.......BCCB.C......BCC.B...C..BB CBB.C.B......C...BB.CBCB.B.CBCBCCCB.B.CC.C.CBC.CB. ..C..C.B.BBC...CC....C...B..BB..BCBCCC.BBCC..CB... .C...CB.B..BC..B.B.CC..CBCB..C........C..CB...B..C ..C.B.BCC.CCCC.CC......C.C.CC.CBCBC.....C.BC.CBC.. C...CB......CBCB...CBCB......C..C..C..CCB.BCC.BB.. CB.BBC..C..B...B.B...C.C..BBC..BB.BB..C..B.B..B..C .B.C.....BBB.C.B.BC.BBCC...CC..B.C..C.C.BB.B...BBC CC..CBCBBC.B...C..BC..BBC...CB.CBB.B........C...C. C..C..BCCBBCC...BB......C.CC..BB.B...B.B..CBBC.BCC .C.BCBCCC.B.C..B.B.B...BBCCCCBBCB...B.B.C..CB....C ..C..B.C..CC.C..BC.BB.CCCB.C...C..C...B..B.....B.B .C.B..B.BCCB.B.CCC...CB.C.C.CCC..CBCBBCBBC.BB...B. BC..BCC.CBBBCB.C....BBB.BC.CC.C...CC..CCCCBC..CC.. .CB.BB.C.B..C.C.B...B.B...C.C...CBBC.CBB..BB...CBC B..B.BCB....BBBCB....BB.C....CB..CC..B.C......B.C. BCB.B.B..C..C.C.B..CCB..CC..B.BC.CBC..BB.CCBBC.... .B......C..C......CB.BB..BBC..C.BBC.CB.B.B.C.BC.B. CCC..CBB.CBCBB.BB.....CC.CB.BCBCB.CC.C.CB.B.BB.BB. BC..BC.BCB..C..B..CB.BCC.CC......BCC.B..C.C..B.CBB ........BB..B......CBB..B.CBBCC.CC.BCCBCCBC.BC.BB. ...C...CC..BCCCCBB.C......CCCBB.CBCC.BB.C..BC.C..C ..BBCB...B.BC....C..C....B....CC..BBB..C..BC.B..B. .B.....BB.B...BBB.....B..BCC..B.BCB...CBC..B...B.B .CCC..B.C.BBB..C.BB...CBB.BCBBBBB...BB.CC..CB..C.C ..B.C..BCB.C..BBC.CB..CBC..B.B.B....B.C..C.BCC.CCC CC...C..CBCBBBCC.C.CB...C...BCB.BC..C.CBC.BBBC.CB. B....CB.B..B..B.C..B.B...CC..C.B.C.BC...BC.C..BCCC ...C.CCBC.B..C.B...BB..B.C.CBC.CB...C.B.....BB..C. CB.CBCC.CB.B....CBB.......CC....B....C.........BCC 3 DPG 180 DLPPPGDGGL LGGDD PDGDGLLDLPGDDDD PLLLPGDDLGGDGGP DPPPLLPPPLDDDGDGPLDGGLLPLLPGGGDPGLLGDDLPDDLGL PLGLGPGLLDDLDPGGPPDDPDGLPPDGPDGPLGLGPDGLDLPGL PPDLDPDGPLDGDPLPPPGDGPDLGPLLPDLGDDGGLLLLLLDLD */
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 223 224 225 226 227 228 229 230 231 232 233 234 235 | //~ while (clock()<=69*CLOCKS_PER_SEC) //~ #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("O3") //~ #pragma GCC optimize("Ofast") //~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //~ #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; using ll=long long; using ld=long double; using pii=pair<int, int>; using pll=pair<ll, ll>; using vi=vector<int>; using vll=vector<ll>; template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; } #ifdef LOCAL #define _upgrade ios_base::sync_with_stdio(0); #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__); #else #define _upgrade ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define debug(...) 98; #define cerr if(0) cout #endif #define pb push_back #define mp make_pair #define all(x) x.begin(),x.end() #define sz(x) ((int)(x).size()) #define fi first #define se second #define erase_duplicates(x) sort(all(x)), (x).resize(distance((x).begin(), unique(all(x)))); #define watch(x) cout << (#x) << " is " << (x) << endl; #define pow2(x) ((x)*(x)) #define mod(x, m) ((((x) % (m)) + (m)) % (m)) #define max3(a, b, c) max(a, max(b, c)) #define min3(a, b, c) min(a, min(b, c)) #define REP(i, a, b) for(int i = a; i <= b; ++i) #define REPV(i, b, a) for(int i = b; i >= a; --i) const double m_pi = acos(-1.0); const int int_max = 0x3f3f3f3f; const int M7 = 1000*1000*1000+7; const int M9 = 1000*1000*1000+9; vector<int> emptyvi; vector<vector<int> > tab; vector<vector<int> > tab2; int main() { _upgrade mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); cout<<fixed<<setprecision(15); int n, m;cin>>n>>m; int parz = 0; int numPion = 0; int planszSize = n*m; REP(i,0,n-1){ string s; cin>>s; tab.pb(emptyvi); REP(j, 0, m-1) { if(s[j] == '.') tab[i].pb(0); if(s[j] == 'O') { tab[i].pb(1); parz += i + j; ++numPion; } } } int parz2 = 0; REP(i,0,n-1){ string s; cin>>s; tab2.pb(emptyvi); REP(j, 0, m-1) { if(s[j] == '.') tab2[i].pb(0); if(s[j] == 'O') { tab2[i].pb(1); parz2 += i + j; } } } if(parz%2 != parz2%2) { cout <<0<<"\n"; return 0; } if(planszSize == 1) { cout <<1<<"\n"; return 0; } long double myKraw=0; REP(i,0,n-1){ REP(j, 0, m-1) { if(tab2[i][j]){ if(i > 0 && !tab2[i-1][j])++myKraw; if(j > 0 && !tab2[i][j-1])++myKraw; if(i < n-1 && !tab2[i+1][j])++myKraw; if(j < m-1 && !tab2[i][j+1])++myKraw; } } } long double allPos = 1; int one =min(planszSize-numPion, numPion); int two =max(planszSize-numPion, numPion); REP(i, two+1, planszSize) { allPos*=i; } REP(i, 1, one) { allPos/=i; } long double allKraw = allPos * 4 * numPion; long double dimenKraw = allPos * 2 * numPion; long double krawPosn = dimenKraw / n; long double krawPosm = dimenKraw / m; long double otherPosn = dimenKraw * (n-1)*(numPion-1) / n / (planszSize-1); long double otherPosm = dimenKraw * (m-1)*(numPion-1) / m / (planszSize-1); long double existing = allKraw - krawPosn - krawPosm - otherPosn - otherPosm; long double ret = myKraw*2/existing; cout<<ret<<"\n"; // debug(allPos); // debug(allKraw); // debug(krawPosn); // debug(krawPosm); // debug(otherPosn); // debug(otherPosm); // debug(existing); // debug(myKraw); return 0; } /* __builtin_popcount count total set bits on a given integer (use __builtin_popcountll for long long). 4 6 ...C.. .B.C.B C.CB.C .B.C.. 103 DDDDDDGDGPPLPDLDPGLDPGLDPGDLPGLGPDLGDPLGDPLGDPLGPDLGPDLGPDLGDGPLDPLGLDLGPDLGPDLPDLGPDLGPDLGPDLGPDLGPDLG 4 2 .C C. BC C. 3 GDL 4 4 .BBB .BB. .BC. .CCB 6 LLDLDP 43 50 CBC...C.B.....C..BC.CBC...BCC.B.BB.B...B..CB.B...B .....BB.C.B.C.CCB.....CCC..C.CC..B..BC...BBCCBB..C .CB.......CB.C.B.BC.C..C.B..B.....B.C...CC.B.CB.B. .BBB.BCCC..CBBCBC.....C..B.B.CCB......CB.BC.BCC..C .....B.C.C..B.BBBC.C.....BCBC...CB....C.CC.C..B.B. B.BBCCB....BC.B...B.C.BC...BBB..CB.B...C..BCCBB..C .CBB...BCC....CC.CB..C.CC.C.CCB.B...BC.BBB..BB.BBC CBC..B.....BC.C..C...B.CBC..BCCC..BB.BBBB..BB...CC C.B...BB.B..C...C.CBBBCCBB..C.BCB..C.CC...B...BC.B .C..C..BB..C.C...C.C..BC.CCB...........CC....CCCBB BC......B..B.B.C.C.B..B..B..BB.BC.BC.CC....CB.BC.B ....BC......C.BBBB.CB..CC..B....B..C...CB..B...C.C BB.B.C.C.C.C..B...C......BC....BBBCBB...BB...B.... B..CBB...B.CB..B.B.......BCCB.C......BCC.B...C..BB CBB.C.B......C...BB.CBCB.B.CBCBCCCB.B.CC.C.CBC.CB. ..C..C.B.BBC...CC....C...B..BB..BCBCCC.BBCC..CB... .C...CB.B..BC..B.B.CC..CBCB..C........C..CB...B..C ..C.B.BCC.CCCC.CC......C.C.CC.CBCBC.....C.BC.CBC.. C...CB......CBCB...CBCB......C..C..C..CCB.BCC.BB.. CB.BBC..C..B...B.B...C.C..BBC..BB.BB..C..B.B..B..C .B.C.....BBB.C.B.BC.BBCC...CC..B.C..C.C.BB.B...BBC CC..CBCBBC.B...C..BC..BBC...CB.CBB.B........C...C. C..C..BCCBBCC...BB......C.CC..BB.B...B.B..CBBC.BCC .C.BCBCCC.B.C..B.B.B...BBCCCCBBCB...B.B.C..CB....C ..C..B.C..CC.C..BC.BB.CCCB.C...C..C...B..B.....B.B .C.B..B.BCCB.B.CCC...CB.C.C.CCC..CBCBBCBBC.BB...B. BC..BCC.CBBBCB.C....BBB.BC.CC.C...CC..CCCCBC..CC.. .CB.BB.C.B..C.C.B...B.B...C.C...CBBC.CBB..BB...CBC B..B.BCB....BBBCB....BB.C....CB..CC..B.C......B.C. BCB.B.B..C..C.C.B..CCB..CC..B.BC.CBC..BB.CCBBC.... .B......C..C......CB.BB..BBC..C.BBC.CB.B.B.C.BC.B. CCC..CBB.CBCBB.BB.....CC.CB.BCBCB.CC.C.CB.B.BB.BB. BC..BC.BCB..C..B..CB.BCC.CC......BCC.B..C.C..B.CBB ........BB..B......CBB..B.CBBCC.CC.BCCBCCBC.BC.BB. ...C...CC..BCCCCBB.C......CCCBB.CBCC.BB.C..BC.C..C ..BBCB...B.BC....C..C....B....CC..BBB..C..BC.B..B. .B.....BB.B...BBB.....B..BCC..B.BCB...CBC..B...B.B .CCC..B.C.BBB..C.BB...CBB.BCBBBBB...BB.CC..CB..C.C ..B.C..BCB.C..BBC.CB..CBC..B.B.B....B.C..C.BCC.CCC CC...C..CBCBBBCC.C.CB...C...BCB.BC..C.CBC.BBBC.CB. B....CB.B..B..B.C..B.B...CC..C.B.C.BC...BC.C..BCCC ...C.CCBC.B..C.B...BB..B.C.CBC.CB...C.B.....BB..C. CB.CBCC.CB.B....CBB.......CC....B....C.........BCC 3 DPG 180 DLPPPGDGGL LGGDD PDGDGLLDLPGDDDD PLLLPGDDLGGDGGP DPPPLLPPPLDDDGDGPLDGGLLPLLPGGGDPGLLGDDLPDDLGL PLGLGPGLLDDLDPGGPPDDPDGLPPDGPDGPLGLGPDGLDLPGL PPDLDPDGPLDGDPLPPPGDGPDLGPLLPDLGDDGGLLLLLLDLD */ |