#include <iostream> #include <vector> #include <string> #include <list> #include <functional> using namespace std; long long gcd(long long a, long long b) { long long result = 1; while (a % 2 == 0 && b % 2 == 0) { a /= 2; b /= 2; result *= 2; } while (a % 3 == 0 && b % 3 == 0) { a /= 3; b /= 3; result *= 3; } return result; } /* long long gcd(long long a, long long b) { */ /* while (a && b) { */ /* if (a < b) */ /* b %= a; */ /* else */ /* a %= b; */ /* } */ /* return a + b; */ /* } */ struct Rational { long long x, y; Rational(int a = 0, int b = 1) : x(a), y(b) {} friend Rational operator+(const Rational& a, const Rational& b) { Rational res(a.x * b.y + a.y * b.x, a.y * b.y); int g = gcd(res.x, res.y); res.x /= g; res.y /= g; return res; } friend Rational operator-(const Rational& a, const Rational& b) { Rational res(a.x * b.y - a.y * b.x, a.y * b.y); int g = gcd(res.x, res.y); res.x /= g; res.y /= g; return res; } friend Rational operator*(const Rational& a, int b) { long long g = gcd(a.x * b, a.y); Rational res(a.x * b / g, a.y / g); return res; } friend Rational operator/(const Rational& a, int b) { long long g = gcd(a.x, a.y * b); Rational res(a.x / g, a.y * b / g); return res; } friend bool operator<(const Rational& a, const Rational& b) { return a.x * b.y < b.x * a.y; } bool zero() const { return x == 0; } friend ostream& operator<<(ostream& os, const Rational& r) { return os << r.x << '/' << r.y; } }; vector<string>P; vector<vector<int>>Ids; vector<Rational>AmoLeft; vector<int>Links; vector<pair<int,int>>Coords; Rational solve_wo_qm(int istart, int jstart, int iend, int jend) { /* cerr << "SOLVE WO QM" << endl; */ int total_cells = 0, link_sum = 0; list<int>still_alive; for (int i = istart; i < iend; i++) for (int j = jstart; j < jend; j++) if (P[i][j] == 'O') { Links[total_cells] = 0; if (i > istart && P[i-1][j]=='O') Links[total_cells]++; if (i < iend - 1 && P[i+1][j]=='O') Links[total_cells]++; if (j > jstart && P[i][j-1]=='O') Links[total_cells]++; if (j < jend - 1 && P[i][j+1]=='O') Links[total_cells]++; link_sum += Links[total_cells]; AmoLeft[total_cells] = Rational(1); Coords[total_cells] = {i, j}; if (Links[total_cells]) still_alive.push_back(total_cells); Ids[i][j] = total_cells++; } link_sum /= 2; while (link_sum) { /* cerr << "\tITERATION link_sum = " << link_sum << endl; */ // find best time-to-death bool any_tod = false; Rational best_tod; for (int i : still_alive) if (Links[i]) { Rational tod = AmoLeft[i] / Links[i]; /* cerr << "AmoLeft = " << AmoLeft[i] << " Links = " << Links[i] << " => tod = " << tod << endl; */ if (!any_tod || tod < best_tod) { any_tod = true; best_tod = tod; } } /* cerr << "\t\tbest tod = " << best_tod << endl; */ // go forward that amount of time vector<list<int>::iterator>to_del; for (auto it = still_alive.begin(); it != still_alive.end(); it++) { /* cerr << "AmoLeft before = " << AmoLeft[*it] << " Links = " << Links[*it] << endl; */ /* cerr << "Mult = " << best_tod * Links[*it] << endl; */ AmoLeft[*it] = AmoLeft[*it] - best_tod * Links[*it]; /* cerr << "AmoLeft after = " << AmoLeft[*it] << endl; */ if (AmoLeft[*it].zero()) to_del.push_back(it); } // delete dead cells for (auto it : to_del) { link_sum -= Links[*it]; Links[*it] = 0; auto [i, j] = Coords[*it]; if (i > istart && P[i-1][j] == 'O' && Links[Ids[i-1][j]]) Links[Ids[i-1][j]]--; if (i < iend - 1 && P[i+1][j] == 'O' && Links[Ids[i+1][j]]) Links[Ids[i+1][j]]--; if (j > jstart && P[i][j-1] == 'O' && Links[Ids[i][j-1]]) Links[Ids[i][j-1]]--; if (j < jend - 1 && P[i][j+1] == 'O' && Links[Ids[i][j+1]]) Links[Ids[i][j+1]]--; still_alive.erase(it); } } Rational result; for (int i = 0; i < total_cells; i++) result = result + AmoLeft[i]; return result; } Rational solve(int istart, int jstart, int iend, int jend, int it_i = 0, int it_j = 0) { for (int i = it_i; i < iend; i++) { for (int j = (i==it_i)?it_j:jstart; j < jend; j++) { if (P[i][j] == '?') { P[i][j] = '.'; Rational r1 = solve(istart, jstart, iend, jend, i, j + 1); P[i][j] = 'O'; Rational r2 = solve(istart, jstart, iend, jend, i, j + 1); P[i][j] = '?'; return r1 + r2; } } } return solve_wo_qm(istart, jstart, iend, jend); } int count_qm(int istart, int jstart, int iend, int jend) { int result = 0; for (int i = istart; i < iend; i++) for (int j = jstart; j < jend; j++) if (P[i][j] == '?') result++; return result; } Rational solve_n1(int jstart, int jend) { vector<int> qms; for (int j = jstart; j < jend; j++) if (P[0][j] == '?') qms.push_back(j); if (qms.empty()) { Rational result; int group = 0; for (int j = jstart; j < jend; j++) if (P[0][j] == 'O') group++; else { if (group == 1 || group > 2) result = result + 1; group = 0; } if (group == 1 || group > 2) result = result + 1; return result; } else { int qm = qms.size() / 2; P[0][qms[qm]] = 'O'; Rational r1 = solve_n1(jstart, jend); P[0][qms[qm]] = '.'; Rational r2l = solve_n1(jstart, qms[qm]); Rational r2r = solve_n1(qms[qm] + 1, jend); P[0][qms[qm]] = '?'; return (r1 + r2l + r2r) / 2; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; P.resize(n); for (auto& row : P) cin >> row; Ids.resize(n, vector<int>(m)); AmoLeft.resize(n*m); Links.resize(n*m); Coords.resize(n*m); // detect cage case bool cage_case = true; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (i*j % 5 == 0 && P[i][j] != '.') { cage_case = false; break; } // solve if (cage_case) { Rational result; for (int i = 1; i < n; i += 5) for (int j = 1; j < m; j += 5) { int iend = min(n, i+4); int jend = min(m, j+4); int qm = count_qm(i, j, iend, jend); result = result + solve(i, j, iend, jend) / (1 << qm); } cout << result << "\n"; } else if (n == 1) cout << solve_n1(0, m) << "\n"; else { int qm = count_qm(0, 0, n, m); cout << (solve(0, 0, n, m) / (1 << qm)) << "\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 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 | #include <iostream> #include <vector> #include <string> #include <list> #include <functional> using namespace std; long long gcd(long long a, long long b) { long long result = 1; while (a % 2 == 0 && b % 2 == 0) { a /= 2; b /= 2; result *= 2; } while (a % 3 == 0 && b % 3 == 0) { a /= 3; b /= 3; result *= 3; } return result; } /* long long gcd(long long a, long long b) { */ /* while (a && b) { */ /* if (a < b) */ /* b %= a; */ /* else */ /* a %= b; */ /* } */ /* return a + b; */ /* } */ struct Rational { long long x, y; Rational(int a = 0, int b = 1) : x(a), y(b) {} friend Rational operator+(const Rational& a, const Rational& b) { Rational res(a.x * b.y + a.y * b.x, a.y * b.y); int g = gcd(res.x, res.y); res.x /= g; res.y /= g; return res; } friend Rational operator-(const Rational& a, const Rational& b) { Rational res(a.x * b.y - a.y * b.x, a.y * b.y); int g = gcd(res.x, res.y); res.x /= g; res.y /= g; return res; } friend Rational operator*(const Rational& a, int b) { long long g = gcd(a.x * b, a.y); Rational res(a.x * b / g, a.y / g); return res; } friend Rational operator/(const Rational& a, int b) { long long g = gcd(a.x, a.y * b); Rational res(a.x / g, a.y * b / g); return res; } friend bool operator<(const Rational& a, const Rational& b) { return a.x * b.y < b.x * a.y; } bool zero() const { return x == 0; } friend ostream& operator<<(ostream& os, const Rational& r) { return os << r.x << '/' << r.y; } }; vector<string>P; vector<vector<int>>Ids; vector<Rational>AmoLeft; vector<int>Links; vector<pair<int,int>>Coords; Rational solve_wo_qm(int istart, int jstart, int iend, int jend) { /* cerr << "SOLVE WO QM" << endl; */ int total_cells = 0, link_sum = 0; list<int>still_alive; for (int i = istart; i < iend; i++) for (int j = jstart; j < jend; j++) if (P[i][j] == 'O') { Links[total_cells] = 0; if (i > istart && P[i-1][j]=='O') Links[total_cells]++; if (i < iend - 1 && P[i+1][j]=='O') Links[total_cells]++; if (j > jstart && P[i][j-1]=='O') Links[total_cells]++; if (j < jend - 1 && P[i][j+1]=='O') Links[total_cells]++; link_sum += Links[total_cells]; AmoLeft[total_cells] = Rational(1); Coords[total_cells] = {i, j}; if (Links[total_cells]) still_alive.push_back(total_cells); Ids[i][j] = total_cells++; } link_sum /= 2; while (link_sum) { /* cerr << "\tITERATION link_sum = " << link_sum << endl; */ // find best time-to-death bool any_tod = false; Rational best_tod; for (int i : still_alive) if (Links[i]) { Rational tod = AmoLeft[i] / Links[i]; /* cerr << "AmoLeft = " << AmoLeft[i] << " Links = " << Links[i] << " => tod = " << tod << endl; */ if (!any_tod || tod < best_tod) { any_tod = true; best_tod = tod; } } /* cerr << "\t\tbest tod = " << best_tod << endl; */ // go forward that amount of time vector<list<int>::iterator>to_del; for (auto it = still_alive.begin(); it != still_alive.end(); it++) { /* cerr << "AmoLeft before = " << AmoLeft[*it] << " Links = " << Links[*it] << endl; */ /* cerr << "Mult = " << best_tod * Links[*it] << endl; */ AmoLeft[*it] = AmoLeft[*it] - best_tod * Links[*it]; /* cerr << "AmoLeft after = " << AmoLeft[*it] << endl; */ if (AmoLeft[*it].zero()) to_del.push_back(it); } // delete dead cells for (auto it : to_del) { link_sum -= Links[*it]; Links[*it] = 0; auto [i, j] = Coords[*it]; if (i > istart && P[i-1][j] == 'O' && Links[Ids[i-1][j]]) Links[Ids[i-1][j]]--; if (i < iend - 1 && P[i+1][j] == 'O' && Links[Ids[i+1][j]]) Links[Ids[i+1][j]]--; if (j > jstart && P[i][j-1] == 'O' && Links[Ids[i][j-1]]) Links[Ids[i][j-1]]--; if (j < jend - 1 && P[i][j+1] == 'O' && Links[Ids[i][j+1]]) Links[Ids[i][j+1]]--; still_alive.erase(it); } } Rational result; for (int i = 0; i < total_cells; i++) result = result + AmoLeft[i]; return result; } Rational solve(int istart, int jstart, int iend, int jend, int it_i = 0, int it_j = 0) { for (int i = it_i; i < iend; i++) { for (int j = (i==it_i)?it_j:jstart; j < jend; j++) { if (P[i][j] == '?') { P[i][j] = '.'; Rational r1 = solve(istart, jstart, iend, jend, i, j + 1); P[i][j] = 'O'; Rational r2 = solve(istart, jstart, iend, jend, i, j + 1); P[i][j] = '?'; return r1 + r2; } } } return solve_wo_qm(istart, jstart, iend, jend); } int count_qm(int istart, int jstart, int iend, int jend) { int result = 0; for (int i = istart; i < iend; i++) for (int j = jstart; j < jend; j++) if (P[i][j] == '?') result++; return result; } Rational solve_n1(int jstart, int jend) { vector<int> qms; for (int j = jstart; j < jend; j++) if (P[0][j] == '?') qms.push_back(j); if (qms.empty()) { Rational result; int group = 0; for (int j = jstart; j < jend; j++) if (P[0][j] == 'O') group++; else { if (group == 1 || group > 2) result = result + 1; group = 0; } if (group == 1 || group > 2) result = result + 1; return result; } else { int qm = qms.size() / 2; P[0][qms[qm]] = 'O'; Rational r1 = solve_n1(jstart, jend); P[0][qms[qm]] = '.'; Rational r2l = solve_n1(jstart, qms[qm]); Rational r2r = solve_n1(qms[qm] + 1, jend); P[0][qms[qm]] = '?'; return (r1 + r2l + r2r) / 2; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; P.resize(n); for (auto& row : P) cin >> row; Ids.resize(n, vector<int>(m)); AmoLeft.resize(n*m); Links.resize(n*m); Coords.resize(n*m); // detect cage case bool cage_case = true; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (i*j % 5 == 0 && P[i][j] != '.') { cage_case = false; break; } // solve if (cage_case) { Rational result; for (int i = 1; i < n; i += 5) for (int j = 1; j < m; j += 5) { int iend = min(n, i+4); int jend = min(m, j+4); int qm = count_qm(i, j, iend, jend); result = result + solve(i, j, iend, jend) / (1 << qm); } cout << result << "\n"; } else if (n == 1) cout << solve_n1(0, m) << "\n"; else { int qm = count_qm(0, 0, n, m); cout << (solve(0, 0, n, m) / (1 << qm)) << "\n"; } return 0; } |