#include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) ll gcd(ll a, ll b) { while (b) a %= b, swap(a, b); return a; } struct frac { ll a, b; frac(ll x = 0) : a(x), b(1) {} frac(ll x, ll y) : a(x), b(y) { if (y < 0) a *= -1, b *= -1; reduce(); } void reduce() { ll g = gcd(abs(a), b); a /= g; b /= g; } frac operator+(frac r) const { return {a*r.b + r.a*b, b*r.b}; } frac operator-(frac r) const { return {a*r.b - r.a*b, b*r.b}; } frac operator*(frac r) const { return {a*r.a, b*r.b}; } frac operator/(frac r) const { return {a*r.b, b*r.a}; } frac& operator+=(frac r) { return *this = *this + r; } frac& operator-=(frac r) { return *this = *this - r; } frac& operator*=(frac r) { return *this = *this * r; } frac& operator/=(frac r) { return *this = *this / r; } bool operator==(frac r) const { return a == r.a && b == r.b; } bool operator!=(frac r) const { return a != r.a || b != r.b; } bool operator<(frac r) const { return a*r.b < b*r.a; } bool operator>(frac r) const { return a*r.b > b*r.a; } bool operator<=(frac r) const { return a*r.b <= b*r.a; } bool operator>=(frac r) const { return a*r.b >= b*r.a; } }; ostream& operator<<(ostream& out, frac f) { return out << f.a << '/' << f.b; } // --- int n, m; vector<string> mat; vector<vector<frac>> rem; vector<vi> seen; frac solveDecided(pii start, int depth) { if (rem[start.x][start.y].a == 0) return 0; if (seen[start.x][start.y] >= depth) return 0; seen[start.x][start.y] = depth; vector<pii> comp = {start}; vi deg; frac sub = 1; auto check = [&](int i, int j) { if (i < 0 || j < 0 || i >= n || j >= m) return 0; if (rem[i][j].a == 0) return 0; if (seen[i][j] < depth) { seen[i][j] = depth; comp.pb({i, j}); } return 1; }; rep(i, 0, sz(comp)) { pii v = comp[i]; int k = check(v.x-1, v.y); k += check(v.x+1, v.y); k += check(v.x, v.y-1); k += check(v.x, v.y+1); deg.pb(k); sub = min(sub, rem[v.x][v.y] / k); } if (sz(comp) <= 1) { return rem[start.x][start.y]; } rep(i, 0, sz(comp)) { pii v = comp[i]; rem[v.x][v.y] -= sub * deg[i]; } frac ret = 0; rep(i, 0, sz(comp)) { ret += solveDecided(comp[i], depth+1); } return ret; } frac solveDecided() { frac ret = 0; seen.assign(n, vi(m, 0)); rep(i, 0, n) rep(j, 0, m) { ret += solveDecided({i, j}, 1); } return ret; } frac solveExp() { int undecided = 0; each(row, mat) undecided += int(count(all(row), '?')); frac ans = 0; rem.assign(n, vector<frac>(m, 0)); rep(mask, 0, 1<<undecided) { int rest = mask; rep(i, 0, n) { rep(j, 0, m) { if (mat[i][j] == '?') { rem[i][j] = (rest & 1); rest >>= 1; } else { rem[i][j] = (mat[i][j] == 'O'); } } } ans += solveDecided(); } ans /= 1 << undecided; return ans; } // --- frac pieces[1<<16]; void precompPieces() { int savedN = n, savedM = m; n = m = 4; rem.assign(4, vector<frac>(4)); rep(mask, 0, 1<<16) { rep(i, 0, 16) { rem[i/4][i%4] = (mask >> i) & 1; } pieces[mask] = solveDecided(); } n = savedN; m = savedM; } frac solvePiece(int i, int j) { int decided = 0; int undecided = 0; frac ret = 0; rep(a, 0, 4) rep(b, 0, 4) { if (i+a < n && j+b < m) { char chr = mat[i+a][j+b]; int bit = a*4+b; if (chr == 'O') decided |= 1 << bit; else if (chr == '?') undecided |= 1 << bit; } } for (int part = undecided;; part = (part-1) & undecided) { ret += pieces[decided | part]; if (!part) break; } return ret / (1 << __builtin_popcount(undecided)); } frac solvePiecewise() { frac ret = 0; precompPieces(); for (int i = 0; i < n; i += 5) { for (int j = 0; j < m; j += 5) { ret += solvePiece(i, j); } } return ret; } // --- frac prob1(int i, int j) { if (i < 0 || j < 0 || i >= n || j >= m) return 0; return mat[i][j] == 'O' ? frac(1) : mat[i][j] == '.' ? frac(0) : frac(1, 2); } frac prob0(int i, int j) { return frac(1) - prob1(i, j); } frac patternAt(int offset, const vector<vi>& pat) { frac ret = 1; rep(i, 0, n) { rep(j, 0, sz(pat[0])) { if (pat[i][j] == 1) ret *= prob1(i, offset+j); else if (pat[i][j] == 0) ret *= prob0(i, offset+j); } } return ret; } frac pattern(const vector<vi>& pat) { frac ret = 0; rep(offset, -sz(pat[0])+1, m) { ret += patternAt(offset, pat); } return ret; } frac solveN1() { return pattern({{0,1,0}}) + pattern({{1,1,1,0}}); } // --- void run() { cin >> n >> m; mat.resize(n); each(row, mat) { cin >> row; } if (n == 1) { cout << solveN1() << '\n'; return; } bool piecewise = 1; rep(i, 0, n) rep(j, 0, m) { if ((i+1)*(j+1) % 5 == 0 && mat[i][j] != '.') { piecewise = 0; break; } } if (piecewise) { cout << solvePiecewise() << '\n'; } else { cout << solveExp() << '\n'; } } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); run(); cout << flush; _Exit(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 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | #include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) ll gcd(ll a, ll b) { while (b) a %= b, swap(a, b); return a; } struct frac { ll a, b; frac(ll x = 0) : a(x), b(1) {} frac(ll x, ll y) : a(x), b(y) { if (y < 0) a *= -1, b *= -1; reduce(); } void reduce() { ll g = gcd(abs(a), b); a /= g; b /= g; } frac operator+(frac r) const { return {a*r.b + r.a*b, b*r.b}; } frac operator-(frac r) const { return {a*r.b - r.a*b, b*r.b}; } frac operator*(frac r) const { return {a*r.a, b*r.b}; } frac operator/(frac r) const { return {a*r.b, b*r.a}; } frac& operator+=(frac r) { return *this = *this + r; } frac& operator-=(frac r) { return *this = *this - r; } frac& operator*=(frac r) { return *this = *this * r; } frac& operator/=(frac r) { return *this = *this / r; } bool operator==(frac r) const { return a == r.a && b == r.b; } bool operator!=(frac r) const { return a != r.a || b != r.b; } bool operator<(frac r) const { return a*r.b < b*r.a; } bool operator>(frac r) const { return a*r.b > b*r.a; } bool operator<=(frac r) const { return a*r.b <= b*r.a; } bool operator>=(frac r) const { return a*r.b >= b*r.a; } }; ostream& operator<<(ostream& out, frac f) { return out << f.a << '/' << f.b; } // --- int n, m; vector<string> mat; vector<vector<frac>> rem; vector<vi> seen; frac solveDecided(pii start, int depth) { if (rem[start.x][start.y].a == 0) return 0; if (seen[start.x][start.y] >= depth) return 0; seen[start.x][start.y] = depth; vector<pii> comp = {start}; vi deg; frac sub = 1; auto check = [&](int i, int j) { if (i < 0 || j < 0 || i >= n || j >= m) return 0; if (rem[i][j].a == 0) return 0; if (seen[i][j] < depth) { seen[i][j] = depth; comp.pb({i, j}); } return 1; }; rep(i, 0, sz(comp)) { pii v = comp[i]; int k = check(v.x-1, v.y); k += check(v.x+1, v.y); k += check(v.x, v.y-1); k += check(v.x, v.y+1); deg.pb(k); sub = min(sub, rem[v.x][v.y] / k); } if (sz(comp) <= 1) { return rem[start.x][start.y]; } rep(i, 0, sz(comp)) { pii v = comp[i]; rem[v.x][v.y] -= sub * deg[i]; } frac ret = 0; rep(i, 0, sz(comp)) { ret += solveDecided(comp[i], depth+1); } return ret; } frac solveDecided() { frac ret = 0; seen.assign(n, vi(m, 0)); rep(i, 0, n) rep(j, 0, m) { ret += solveDecided({i, j}, 1); } return ret; } frac solveExp() { int undecided = 0; each(row, mat) undecided += int(count(all(row), '?')); frac ans = 0; rem.assign(n, vector<frac>(m, 0)); rep(mask, 0, 1<<undecided) { int rest = mask; rep(i, 0, n) { rep(j, 0, m) { if (mat[i][j] == '?') { rem[i][j] = (rest & 1); rest >>= 1; } else { rem[i][j] = (mat[i][j] == 'O'); } } } ans += solveDecided(); } ans /= 1 << undecided; return ans; } // --- frac pieces[1<<16]; void precompPieces() { int savedN = n, savedM = m; n = m = 4; rem.assign(4, vector<frac>(4)); rep(mask, 0, 1<<16) { rep(i, 0, 16) { rem[i/4][i%4] = (mask >> i) & 1; } pieces[mask] = solveDecided(); } n = savedN; m = savedM; } frac solvePiece(int i, int j) { int decided = 0; int undecided = 0; frac ret = 0; rep(a, 0, 4) rep(b, 0, 4) { if (i+a < n && j+b < m) { char chr = mat[i+a][j+b]; int bit = a*4+b; if (chr == 'O') decided |= 1 << bit; else if (chr == '?') undecided |= 1 << bit; } } for (int part = undecided;; part = (part-1) & undecided) { ret += pieces[decided | part]; if (!part) break; } return ret / (1 << __builtin_popcount(undecided)); } frac solvePiecewise() { frac ret = 0; precompPieces(); for (int i = 0; i < n; i += 5) { for (int j = 0; j < m; j += 5) { ret += solvePiece(i, j); } } return ret; } // --- frac prob1(int i, int j) { if (i < 0 || j < 0 || i >= n || j >= m) return 0; return mat[i][j] == 'O' ? frac(1) : mat[i][j] == '.' ? frac(0) : frac(1, 2); } frac prob0(int i, int j) { return frac(1) - prob1(i, j); } frac patternAt(int offset, const vector<vi>& pat) { frac ret = 1; rep(i, 0, n) { rep(j, 0, sz(pat[0])) { if (pat[i][j] == 1) ret *= prob1(i, offset+j); else if (pat[i][j] == 0) ret *= prob0(i, offset+j); } } return ret; } frac pattern(const vector<vi>& pat) { frac ret = 0; rep(offset, -sz(pat[0])+1, m) { ret += patternAt(offset, pat); } return ret; } frac solveN1() { return pattern({{0,1,0}}) + pattern({{1,1,1,0}}); } // --- void run() { cin >> n >> m; mat.resize(n); each(row, mat) { cin >> row; } if (n == 1) { cout << solveN1() << '\n'; return; } bool piecewise = 1; rep(i, 0, n) rep(j, 0, m) { if ((i+1)*(j+1) % 5 == 0 && mat[i][j] != '.') { piecewise = 0; break; } } if (piecewise) { cout << solvePiecewise() << '\n'; } else { cout << solveExp() << '\n'; } } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); run(); cout << flush; _Exit(0); } |