#include <bits/stdc++.h> using namespace std; constexpr int N = 220; int n, m, a[N][N]; vector<pair<int, int>> vec; struct frac { int x, y; frac(int _x = 0) : x(_x), y(1) {} frac(int _x, int _y) : x(_x), y(_y){} frac relax() { int gcd = __gcd(x, y); return frac(x / gcd, y / gcd); } friend frac operator / (frac x, int y) { return frac(x.x, x.y * y).relax(); } friend frac operator * (frac x, int y) { return frac(x.x * y, x.y).relax(); } friend bool operator < (frac x, frac y) { return x.x * y.y < x.y * y.x; } friend frac operator - (frac x, frac y) { return frac(x.x * y.y - x.y * y.x, x.y * y.y).relax(); } friend frac operator + (frac x, frac y) { return frac(x.x * y.y + x.y * y.x, x.y * y.y).relax(); } friend ostream& operator << (ostream &out, frac x) { out << x.x << '/' << x.y; return out; } }; namespace sub1 { frac b[N][N]; int deg[N][N]; const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; frac solve() { for(int i = 1; i <= n; i ++) { for(int j = 1; j <= m; j ++) b[i][j] = a[i][j] ? frac(1) : frac(0); } int upd; do { upd = 0; for(int i = 1; i <= n; i ++) for(int j = 1; j <= m; j ++) deg[i][j] = 0; for(int i = 1; i <= n; i ++) { for(int j = 1; j <= m; j ++) { for(int k = 0; k < 4; k ++) { int x = i + dx[k], y = j + dy[k]; if(x >= 1 && x <= n && y >= 1 && y <= m && 0 < b[i][j] && 0 < b[x][y]) { deg[i][j] ++; upd = 1; } } } } frac mn(1); for(int i = 1; i <= n; i ++) { for(int j = 1; j <= m; j ++) if(deg[i][j]) { frac x = b[i][j] / deg[i][j]; mn = min(mn, x); } } for(int i = 1; i <= n; i ++) { for(int j = 1; j <= m; j ++) b[i][j] = b[i][j] - mn * deg[i][j]; } }while(upd); frac ans(0); for(int i = 1; i <= n; i ++) { for(int j = 1; j <= m; j ++) ans = ans + b[i][j]; } return ans; } void work() { frac ans(0); for(int s = 0; s < 1 << vec.size(); s ++) { for(int i = 0; i < vec.size(); i ++) { a[vec[i].first][vec[i].second] = s >> i & 1; } ans = (ans + solve()); } for(int i = 0; i < vec.size(); i ++) ans = ans / 2; cout << ans.relax() << '\n'; } } namespace sub2 { using i128 = __int128; i128 ans1, ans2, mod = 1; int cnt[N], b[N]; void print(i128 x){ if(!x) return ; print(x / 10), cout << char(x % 10 + '0'); } void work() { mod = 1; for(int i = 0; i < 37; i ++) mod *= 10; int mul = 0; for(int i = 1; i <= m; i ++) { b[i] = a[1][i]; if(b[i] == 2) mul ++; } for(int i = 1; i <= m + 1; i ++) { if(b[i] != 1) { int inv = b[i] == 2 ? 1 : 0; for(int j = i - 1; j >= 0; j --) { if(b[j] == 2) inv ++; if(b[j] != 1 && i - j - 1 != 2 && i - j - 1 != 0) { cnt[mul - inv] ++; } if(b[j] == 0) break; } } } for(int i = 0; i < N - 1; i ++) { cnt[i + 1] += cnt[i] / 2; cnt[i] %= 2; } int p = 0; while(p < mul && !cnt[p]) p ++; i128 ans1 = 0, ans2 = 0, ans3 = 1, ans4 = 0; for(int i = N - 1; i >= p; i --) { ans2 *= 2; ans1 *= 2; ans2 += cnt[i]; ans1 += ans2 / mod; ans2 %= mod; } for(int i = p; i < mul; i ++) { ans3 *= 2; ans4 *= 2; ans4 += ans3 / mod; ans3 %= mod; } print(ans2); print(ans1); if(!ans1 && !ans2) cout << 0; cout << '/'; print(ans3); print(ans4); cout << '\n'; } } int main() { // freopen("in.txt", "r", stdin); ios::sync_with_stdio(false), cin.tie(0); cin >> n >> m; for(int i = 1; i <= n; i ++) { string str; cin >> str; str = "?" + str; for(int j = 1; j <= m; j ++) a[i][j] = str[j] == '.' ? 0 : str[j] == 'O' ? 1 : (vec.emplace_back(i, j), 2); } if(vec.size() <= 5) sub1::work(); else if(n == 1) sub2::work(); 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 | #include <bits/stdc++.h> using namespace std; constexpr int N = 220; int n, m, a[N][N]; vector<pair<int, int>> vec; struct frac { int x, y; frac(int _x = 0) : x(_x), y(1) {} frac(int _x, int _y) : x(_x), y(_y){} frac relax() { int gcd = __gcd(x, y); return frac(x / gcd, y / gcd); } friend frac operator / (frac x, int y) { return frac(x.x, x.y * y).relax(); } friend frac operator * (frac x, int y) { return frac(x.x * y, x.y).relax(); } friend bool operator < (frac x, frac y) { return x.x * y.y < x.y * y.x; } friend frac operator - (frac x, frac y) { return frac(x.x * y.y - x.y * y.x, x.y * y.y).relax(); } friend frac operator + (frac x, frac y) { return frac(x.x * y.y + x.y * y.x, x.y * y.y).relax(); } friend ostream& operator << (ostream &out, frac x) { out << x.x << '/' << x.y; return out; } }; namespace sub1 { frac b[N][N]; int deg[N][N]; const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; frac solve() { for(int i = 1; i <= n; i ++) { for(int j = 1; j <= m; j ++) b[i][j] = a[i][j] ? frac(1) : frac(0); } int upd; do { upd = 0; for(int i = 1; i <= n; i ++) for(int j = 1; j <= m; j ++) deg[i][j] = 0; for(int i = 1; i <= n; i ++) { for(int j = 1; j <= m; j ++) { for(int k = 0; k < 4; k ++) { int x = i + dx[k], y = j + dy[k]; if(x >= 1 && x <= n && y >= 1 && y <= m && 0 < b[i][j] && 0 < b[x][y]) { deg[i][j] ++; upd = 1; } } } } frac mn(1); for(int i = 1; i <= n; i ++) { for(int j = 1; j <= m; j ++) if(deg[i][j]) { frac x = b[i][j] / deg[i][j]; mn = min(mn, x); } } for(int i = 1; i <= n; i ++) { for(int j = 1; j <= m; j ++) b[i][j] = b[i][j] - mn * deg[i][j]; } }while(upd); frac ans(0); for(int i = 1; i <= n; i ++) { for(int j = 1; j <= m; j ++) ans = ans + b[i][j]; } return ans; } void work() { frac ans(0); for(int s = 0; s < 1 << vec.size(); s ++) { for(int i = 0; i < vec.size(); i ++) { a[vec[i].first][vec[i].second] = s >> i & 1; } ans = (ans + solve()); } for(int i = 0; i < vec.size(); i ++) ans = ans / 2; cout << ans.relax() << '\n'; } } namespace sub2 { using i128 = __int128; i128 ans1, ans2, mod = 1; int cnt[N], b[N]; void print(i128 x){ if(!x) return ; print(x / 10), cout << char(x % 10 + '0'); } void work() { mod = 1; for(int i = 0; i < 37; i ++) mod *= 10; int mul = 0; for(int i = 1; i <= m; i ++) { b[i] = a[1][i]; if(b[i] == 2) mul ++; } for(int i = 1; i <= m + 1; i ++) { if(b[i] != 1) { int inv = b[i] == 2 ? 1 : 0; for(int j = i - 1; j >= 0; j --) { if(b[j] == 2) inv ++; if(b[j] != 1 && i - j - 1 != 2 && i - j - 1 != 0) { cnt[mul - inv] ++; } if(b[j] == 0) break; } } } for(int i = 0; i < N - 1; i ++) { cnt[i + 1] += cnt[i] / 2; cnt[i] %= 2; } int p = 0; while(p < mul && !cnt[p]) p ++; i128 ans1 = 0, ans2 = 0, ans3 = 1, ans4 = 0; for(int i = N - 1; i >= p; i --) { ans2 *= 2; ans1 *= 2; ans2 += cnt[i]; ans1 += ans2 / mod; ans2 %= mod; } for(int i = p; i < mul; i ++) { ans3 *= 2; ans4 *= 2; ans4 += ans3 / mod; ans3 %= mod; } print(ans2); print(ans1); if(!ans1 && !ans2) cout << 0; cout << '/'; print(ans3); print(ans4); cout << '\n'; } } int main() { // freopen("in.txt", "r", stdin); ios::sync_with_stdio(false), cin.tie(0); cin >> n >> m; for(int i = 1; i <= n; i ++) { string str; cin >> str; str = "?" + str; for(int j = 1; j <= m; j ++) a[i][j] = str[j] == '.' ? 0 : str[j] == 'O' ? 1 : (vec.emplace_back(i, j), 2); } if(vec.size() <= 5) sub1::work(); else if(n == 1) sub2::work(); return 0; } |