#include <bits/stdc++.h> using namespace std; const long long V = 24; long long solveNoWildcards(int n, int m, vector <vector<int>> &tab) { for (auto &e : tab) for (auto &f : e) assert(f != -1); vector <vector<int>> val(n, vector <int> (m, 0.0)); for (int x = 0; x < n; x++) for (int y = 0; y < m; y++) if (tab[x][y]) { val[x][y] = V; } int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; while (true) { vector <tuple<int,int,int>> decr; for (int x = 0; x < n; x++) for (int y = 0; y < m; y++) if (val[x][y]) { int deg = 0; for (int dir = 0; dir < 4; dir++) { int nx = x + dx[dir], ny = y + dy[dir]; if (nx >= 0 && nx < n && ny >= 0 && ny < m && val[nx][ny]) { deg++; } } if (deg > 0) { decr.push_back({x, y, deg}); } } if (decr.empty()) { break; } int time = V; for (auto &[x, y, deg] : decr) { assert(val[x][y] % deg == 0); time = min(time, val[x][y] / deg); } for (auto &[x, y, deg] : decr) { val[x][y] -= time * deg; } } long long totalLeft = 0; for (int x = 0; x < n; x++) for (int y = 0; y < m; y++) if (tab[x][y]) { totalLeft += val[x][y]; } return totalLeft; } pair <long long, long long> constructAns(long long num, long long denom) { long long gcd = __gcd(num, denom); return {num / gcd, denom / gcd}; } pair <long long, long long> solve(int n, int m, vector <vector<int>> &tab) { vector <pair<int,int>> wildcards; for (int x = 0; x < n; x++) for (int y = 0; y < m; y++) if (tab[x][y] == -1) { wildcards.push_back({x, y}); } int numWildcards = wildcards.size(); if (n == 1) { long long ansTotal = 0; auto numWildcardsInside = [&](int l, int r) { int cnt = 0; for (int i = max(0, l); i <= min(m - 1, r); i++) if (tab[0][i] == -1) { cnt++; } return cnt; }; auto canBeEmpty = [&](int y) { return y < 0 || y >= m || tab[0][y] != 1; }; auto canBeFilled = [&](int y) { return y >= 0 && y < m && tab[0][y] != 0; }; for (int y = 0; y < m; y++) if (canBeFilled(y) && canBeEmpty(y + 1)) { ansTotal += 1 << (4 - numWildcardsInside(y, y + 1)); } for (int y = 0; y < m - 1; y++) { if (canBeEmpty(y - 1) && canBeFilled(y) && canBeFilled(y + 1) && canBeEmpty(y + 2)) { ansTotal -= 1 << (4 - numWildcardsInside(y - 1, y + 2)); } } return constructAns(ansTotal, 16); } else if (numWildcards <= 5) { long long ansTotal = 0; for (int mask = 0; mask < (1 << numWildcards); mask++) { auto tabConcrete = tab; for (int i = 0; i < numWildcards; i++) { auto [x, y] = wildcards[i]; if (mask & (1 << i)) { tabConcrete[x][y] = 1; } else { tabConcrete[x][y] = 0; } } ansTotal += solveNoWildcards(n, m, tabConcrete); } return constructAns(ansTotal, V * (1 << numWildcards)); } else { bool segmented = true; for (int x = 0; x < n; x++) for (int y = 0; y < m; y++) { int prod = (x + 1) * (y + 1); if (prod % 5 == 0 && tab[x][y] != 0) { segmented = false; } } assert(segmented); const int F = 4; const int FMASKS = 1 << (F * F); vector <long long> allAnswers(FMASKS, 0); vector <vector<int>> tabSlice(F, vector <int> (F)); for (int mask = 0; mask < FMASKS; mask++) { for (int x = 0; x < F; x++) for (int y = 0; y < F; y++) { int idx = x * F + y; tabSlice[x][y] = (mask & (1 << idx)) != 0; } allAnswers[mask] = solveNoWildcards(F, F, tabSlice); } long long ansTotal = 0; for (int xs = 0; xs < n; xs += F + 1) for (int ys = 0; ys < m; ys += F + 1) { int maskOn = 0, maskWildcard = 0; for (int x = 0; x < F; x++) for (int y = 0; y < F; y++) { int idx = x * F + y, xReal = xs + x, yReal = ys + y; if (xReal < n && yReal < m) { if (tab[xReal][yReal] == 1) { maskOn |= 1 << idx; } else if (tab[xReal][yReal] == -1) { maskWildcard |= 1 << idx; } } } int nw = __builtin_popcount(maskWildcard); for (int mask = 0; mask < FMASKS; mask++) { if ((mask & maskOn) == maskOn) { int rest = mask ^ maskOn; if ((rest & maskWildcard) == rest) { ansTotal += allAnswers[mask] << (F * F - nw); } } } } return constructAns(ansTotal, V * FMASKS); } } int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; vector <vector<int>> tab(n, vector <int> (m)); for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < m; j++) { if (s[j] == 'O') { tab[i][j] = 1; } else if (s[j] == '?') { tab[i][j] = -1; } else { tab[i][j] = 0; } } } auto [x, y] = solve(n, m, tab); cout << x << "/" << y << '\n'; 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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | #include <bits/stdc++.h> using namespace std; const long long V = 24; long long solveNoWildcards(int n, int m, vector <vector<int>> &tab) { for (auto &e : tab) for (auto &f : e) assert(f != -1); vector <vector<int>> val(n, vector <int> (m, 0.0)); for (int x = 0; x < n; x++) for (int y = 0; y < m; y++) if (tab[x][y]) { val[x][y] = V; } int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; while (true) { vector <tuple<int,int,int>> decr; for (int x = 0; x < n; x++) for (int y = 0; y < m; y++) if (val[x][y]) { int deg = 0; for (int dir = 0; dir < 4; dir++) { int nx = x + dx[dir], ny = y + dy[dir]; if (nx >= 0 && nx < n && ny >= 0 && ny < m && val[nx][ny]) { deg++; } } if (deg > 0) { decr.push_back({x, y, deg}); } } if (decr.empty()) { break; } int time = V; for (auto &[x, y, deg] : decr) { assert(val[x][y] % deg == 0); time = min(time, val[x][y] / deg); } for (auto &[x, y, deg] : decr) { val[x][y] -= time * deg; } } long long totalLeft = 0; for (int x = 0; x < n; x++) for (int y = 0; y < m; y++) if (tab[x][y]) { totalLeft += val[x][y]; } return totalLeft; } pair <long long, long long> constructAns(long long num, long long denom) { long long gcd = __gcd(num, denom); return {num / gcd, denom / gcd}; } pair <long long, long long> solve(int n, int m, vector <vector<int>> &tab) { vector <pair<int,int>> wildcards; for (int x = 0; x < n; x++) for (int y = 0; y < m; y++) if (tab[x][y] == -1) { wildcards.push_back({x, y}); } int numWildcards = wildcards.size(); if (n == 1) { long long ansTotal = 0; auto numWildcardsInside = [&](int l, int r) { int cnt = 0; for (int i = max(0, l); i <= min(m - 1, r); i++) if (tab[0][i] == -1) { cnt++; } return cnt; }; auto canBeEmpty = [&](int y) { return y < 0 || y >= m || tab[0][y] != 1; }; auto canBeFilled = [&](int y) { return y >= 0 && y < m && tab[0][y] != 0; }; for (int y = 0; y < m; y++) if (canBeFilled(y) && canBeEmpty(y + 1)) { ansTotal += 1 << (4 - numWildcardsInside(y, y + 1)); } for (int y = 0; y < m - 1; y++) { if (canBeEmpty(y - 1) && canBeFilled(y) && canBeFilled(y + 1) && canBeEmpty(y + 2)) { ansTotal -= 1 << (4 - numWildcardsInside(y - 1, y + 2)); } } return constructAns(ansTotal, 16); } else if (numWildcards <= 5) { long long ansTotal = 0; for (int mask = 0; mask < (1 << numWildcards); mask++) { auto tabConcrete = tab; for (int i = 0; i < numWildcards; i++) { auto [x, y] = wildcards[i]; if (mask & (1 << i)) { tabConcrete[x][y] = 1; } else { tabConcrete[x][y] = 0; } } ansTotal += solveNoWildcards(n, m, tabConcrete); } return constructAns(ansTotal, V * (1 << numWildcards)); } else { bool segmented = true; for (int x = 0; x < n; x++) for (int y = 0; y < m; y++) { int prod = (x + 1) * (y + 1); if (prod % 5 == 0 && tab[x][y] != 0) { segmented = false; } } assert(segmented); const int F = 4; const int FMASKS = 1 << (F * F); vector <long long> allAnswers(FMASKS, 0); vector <vector<int>> tabSlice(F, vector <int> (F)); for (int mask = 0; mask < FMASKS; mask++) { for (int x = 0; x < F; x++) for (int y = 0; y < F; y++) { int idx = x * F + y; tabSlice[x][y] = (mask & (1 << idx)) != 0; } allAnswers[mask] = solveNoWildcards(F, F, tabSlice); } long long ansTotal = 0; for (int xs = 0; xs < n; xs += F + 1) for (int ys = 0; ys < m; ys += F + 1) { int maskOn = 0, maskWildcard = 0; for (int x = 0; x < F; x++) for (int y = 0; y < F; y++) { int idx = x * F + y, xReal = xs + x, yReal = ys + y; if (xReal < n && yReal < m) { if (tab[xReal][yReal] == 1) { maskOn |= 1 << idx; } else if (tab[xReal][yReal] == -1) { maskWildcard |= 1 << idx; } } } int nw = __builtin_popcount(maskWildcard); for (int mask = 0; mask < FMASKS; mask++) { if ((mask & maskOn) == maskOn) { int rest = mask ^ maskOn; if ((rest & maskWildcard) == rest) { ansTotal += allAnswers[mask] << (F * F - nw); } } } } return constructAns(ansTotal, V * FMASKS); } } int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; vector <vector<int>> tab(n, vector <int> (m)); for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < m; j++) { if (s[j] == 'O') { tab[i][j] = 1; } else if (s[j] == '?') { tab[i][j] = -1; } else { tab[i][j] = 0; } } } auto [x, y] = solve(n, m, tab); cout << x << "/" << y << '\n'; return 0; } |