#include <iostream> #include <vector> #include <iomanip> using namespace std; // #define DEBUG #ifdef DEBUG #define dassert(x) assert(x); #define df(...) printf(__VA_ARGS__) #else #define dassert(x) #define df(...) #endif #define x first #define y second #define mp make_pair #define pb push_back #define ir(x, a, b) ((a) <= (x) && (x) <= (b)) #define vec vector #define sz(x) (ll)x.size() #define foru(i, n) for (ll i = 0; (i) < (n); ++(i)) #define all(x) (x).begin(), (x).end() #define fori(i, a, b) for (ll i = (a); i <= (b); ++i) typedef int64_t ll; int read() { int n = 0; bool b = 0; char c = getchar(); for (; !ir(c, '0', '9'); c = getchar()) b = (c == '-'); for (; ir(c, '0', '9'); c = getchar()) n = 10*n + (c-'0'); if (b) return -n; return n; } int moves(int n, int m, vec<vec<bool>> a) { int s = 0; foru (x, n) foru (y, m) { if (a[x][y]) { fori (dx, -1, 1) fori (dy, -1, 1) { if ((dx == 0) + (dy == 0) != 1) continue; if (ir(x+dx, 0, n-1) && ir(y+dy, 0, m-1) && !a[x+dx][y+dy]) { // cout << x << " " << y << " " << x+dx << " " << y+dy << "\n"; ++s; } } } } return s; } // int n, m; // int ct[10]; // vec<vec<bool>> a; // void bt(int x, int y, int on = 0) { // if (y == m) { // ct[on] += moves(n, m, a); // return; // } // if (x == n) { // bt(0, y+1, on); // return; // } // a[x][y] = 0; // bt(x+1, y, on); // a[x][y] = 1; // bt(x+1, y, on+1); // } ll choose(ll a, ll b) { if (b < a-b) return choose(a, a-b); __int128 x = 1; for (__int128 m = b+1; m <= a; ++m) x *= m; for (__int128 m = 2; m <= a-b; ++m) x /= m; return x; } ll calc(ll n, ll m, ll k) { if (n < m) swap(n, m); // n >= m ll s = 0; if (m == 1) { // cout << "special case" << endl; fori (corners, 0, min(k, ll(2))) { ll mid = k - corners; // cout << corners << " " << mid << " " << n-2 << endl; if (mid > n-2) continue; ll ways = choose(2, corners)*choose(n-2, mid); s += ways * (corners*1 + mid*2); // cout << "ways: " << ways << endl; } return s; } ll ne = 2*(n+m-4), nm = (n-2)*(m-2); // cout << ne << " " << nm << endl; fori (corners, 0, min(k, ll(4))) { fori (edges, 0, min(k-corners, ne)) { ll mid = k - edges - corners; if (mid > nm) continue; // cout << corners << " " << edges << " " << mid << endl; ll ways = choose(4, corners)*choose(ne, edges)*choose(nm, mid); s += ways * (corners*2 + edges*3 + mid*4); // cout << choose(4, corners)*choose(ne, edges)*choose(nm, mid) << endl; } } return s; } int count(int n, int m, vec<vec<bool>> a) { int s = 0; foru (x, n) foru (y, m) { if (a[x][y]) { ++s; } } return s; } bool bit(int n, int m, vec<vec<bool>> a) { bool s = 0; foru (x, n) foru (y, m) { if (a[x][y]) { s ^= (y&1) ^ (x&1); } } return s; } int main() { df("debug mode\n"); #ifndef DEBUG ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #endif // for (int i = 1; i <= 8; ++i) { // cout << choose(64, i) << endl; // } int n, m; cin >> n >> m; vec<vec<bool>> t(n, vec<bool>(m)); vec<vec<bool>> sn(n, vec<bool>(m)); // a.resize(n, vec<bool>(m)); // calc // bt(0,0); // fori (i, 0, n*m) { // cout << ct[i] << "\n"; // } string s; foru (i, n) { cin >> s; // cout << s << endl; foru (j, m) { sn[i][j] = (s[j] == 'O'); } } foru (i, n) { cin >> s; // cout << s << endl; foru (j, m) { t[i][j] = (s[j] == 'O'); } } if (bit(n,m,t) != bit(n,m,sn)) { cout << "0\n"; return 0; } ll ct = count(n, m, t); vec<ll> dp(9); fori (i, 1, ct) { dp[i] = calc(n, m, i) - dp[i-1]; // cout << dp[i] << endl; } // cout << moves(n, m, t) << endl; cout << fixed << setprecision(15); cout << (long double)(2*moves(n,m,t)) / (long double)(dp[ct]) << endl; return 0; }
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 | #include <iostream> #include <vector> #include <iomanip> using namespace std; // #define DEBUG #ifdef DEBUG #define dassert(x) assert(x); #define df(...) printf(__VA_ARGS__) #else #define dassert(x) #define df(...) #endif #define x first #define y second #define mp make_pair #define pb push_back #define ir(x, a, b) ((a) <= (x) && (x) <= (b)) #define vec vector #define sz(x) (ll)x.size() #define foru(i, n) for (ll i = 0; (i) < (n); ++(i)) #define all(x) (x).begin(), (x).end() #define fori(i, a, b) for (ll i = (a); i <= (b); ++i) typedef int64_t ll; int read() { int n = 0; bool b = 0; char c = getchar(); for (; !ir(c, '0', '9'); c = getchar()) b = (c == '-'); for (; ir(c, '0', '9'); c = getchar()) n = 10*n + (c-'0'); if (b) return -n; return n; } int moves(int n, int m, vec<vec<bool>> a) { int s = 0; foru (x, n) foru (y, m) { if (a[x][y]) { fori (dx, -1, 1) fori (dy, -1, 1) { if ((dx == 0) + (dy == 0) != 1) continue; if (ir(x+dx, 0, n-1) && ir(y+dy, 0, m-1) && !a[x+dx][y+dy]) { // cout << x << " " << y << " " << x+dx << " " << y+dy << "\n"; ++s; } } } } return s; } // int n, m; // int ct[10]; // vec<vec<bool>> a; // void bt(int x, int y, int on = 0) { // if (y == m) { // ct[on] += moves(n, m, a); // return; // } // if (x == n) { // bt(0, y+1, on); // return; // } // a[x][y] = 0; // bt(x+1, y, on); // a[x][y] = 1; // bt(x+1, y, on+1); // } ll choose(ll a, ll b) { if (b < a-b) return choose(a, a-b); __int128 x = 1; for (__int128 m = b+1; m <= a; ++m) x *= m; for (__int128 m = 2; m <= a-b; ++m) x /= m; return x; } ll calc(ll n, ll m, ll k) { if (n < m) swap(n, m); // n >= m ll s = 0; if (m == 1) { // cout << "special case" << endl; fori (corners, 0, min(k, ll(2))) { ll mid = k - corners; // cout << corners << " " << mid << " " << n-2 << endl; if (mid > n-2) continue; ll ways = choose(2, corners)*choose(n-2, mid); s += ways * (corners*1 + mid*2); // cout << "ways: " << ways << endl; } return s; } ll ne = 2*(n+m-4), nm = (n-2)*(m-2); // cout << ne << " " << nm << endl; fori (corners, 0, min(k, ll(4))) { fori (edges, 0, min(k-corners, ne)) { ll mid = k - edges - corners; if (mid > nm) continue; // cout << corners << " " << edges << " " << mid << endl; ll ways = choose(4, corners)*choose(ne, edges)*choose(nm, mid); s += ways * (corners*2 + edges*3 + mid*4); // cout << choose(4, corners)*choose(ne, edges)*choose(nm, mid) << endl; } } return s; } int count(int n, int m, vec<vec<bool>> a) { int s = 0; foru (x, n) foru (y, m) { if (a[x][y]) { ++s; } } return s; } bool bit(int n, int m, vec<vec<bool>> a) { bool s = 0; foru (x, n) foru (y, m) { if (a[x][y]) { s ^= (y&1) ^ (x&1); } } return s; } int main() { df("debug mode\n"); #ifndef DEBUG ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #endif // for (int i = 1; i <= 8; ++i) { // cout << choose(64, i) << endl; // } int n, m; cin >> n >> m; vec<vec<bool>> t(n, vec<bool>(m)); vec<vec<bool>> sn(n, vec<bool>(m)); // a.resize(n, vec<bool>(m)); // calc // bt(0,0); // fori (i, 0, n*m) { // cout << ct[i] << "\n"; // } string s; foru (i, n) { cin >> s; // cout << s << endl; foru (j, m) { sn[i][j] = (s[j] == 'O'); } } foru (i, n) { cin >> s; // cout << s << endl; foru (j, m) { t[i][j] = (s[j] == 'O'); } } if (bit(n,m,t) != bit(n,m,sn)) { cout << "0\n"; return 0; } ll ct = count(n, m, t); vec<ll> dp(9); fori (i, 1, ct) { dp[i] = calc(n, m, i) - dp[i-1]; // cout << dp[i] << endl; } // cout << moves(n, m, t) << endl; cout << fixed << setprecision(15); cout << (long double)(2*moves(n,m,t)) / (long double)(dp[ct]) << endl; return 0; } |