//orig_brute //#ifdef DEBUG //#define _GLIBCXX_DEBUG //#endif #pragma GCC optimize("O3") #include<bits/stdc++.h> using namespace std; #ifdef DEBUG #include "lib/debug.h" #else #define debug(...) 228 #endif #define pb push_back typedef long long ll; typedef long double ld; int n, L; const int maxN = 18; const int maxL = 100'002; int a[maxN][maxL]; int cnt[maxL]; int orig_mask[maxL]; //const int maxS = maxN * maxL + 2; //int mask[maxS]; int T; int Len; //find previous guy with such cnt, to optimize memory don't mul int prv[maxN + 1][maxL + 1]; int jump[maxN + 1][maxL + 1]; int Coef = 0; int T_int = -1; //int states = 0; bool rec(int v, vector<int> vals, int lim) { // lim = 0; vector<pair<int,int>> best; assert(vals.size() == Len); // debug(v, "??", vals); if (v == (1 << n) - 1) return true; for (int i = 0; i < n; i++) { if (v & (1 << i)) continue; int X = 0; int Y = 0; int fnd = -1; for (int j = 0; j < T; j++) { if (vals[j] & (1 << i)) X++; if (vals[j] == (1 << i)) Y++; } for (int j = 0; j <= Len - T; j++) { if (X == T && Y == 0 && j >= lim) { fnd = j; break; } else { if (j + T < Len) { if (vals[j] & (1 << i)) X--; if (vals[j] == (1 << i)) Y--; if (vals[j + T] & (1 << i)) X++; if (vals[j + T] == (1 << i)) Y++; } } } // if (T == 3) { // debug(fnd, "WUT", v, i); // } if (fnd == -1) return false; best.emplace_back(fnd, i); } if (best.size() == 1) return true; sort(best.begin(), best.end()); best.resize(2); for (int p = 0; p < best.size(); p++) { int A = best[p].first; int who = best[p].second; auto nmask = vals; for (int j = A; j < A + T; j++) { assert(nmask[j] & (1 << who)); assert(nmask[j] != (1 << who)); nmask[j] ^= (1 << who); } if (rec(v ^ (1 << who), nmask, A)) return true; } return false; } //recheck constraints!!! int Segs[maxN + 1]; int Seg_int[maxN + 1]; int smart_rec(int v, int lim) { if (v == (1 << n) - 1) return true; if (lim > Len - T) return false; int sz = __builtin_popcount(v); const int INF = 1e9; int arr[2] = {INF, INF}; int who[2] = {-1, -1}; int zapas = Coef - 1 - (T % Coef); int Limcoef = lim % Coef; /*if (Limcoef > zapas + 1) { if (v == 4) { debug("WUTT", Limcoef, zapas); } lim = ((lim + Coef - 1) / Coef) * Coef; }*/ if (lim > Len - T) return false; int lim_int = lim / Coef; // debug(v, lim); for (int i = 0; i < n; i++) { if (v & (1 << i)) continue; int he = jump[i][lim_int]; if (he == -1) return false; if (he == lim_int) { if (Limcoef <= zapas + 1) { he = lim; } else { // assert(lim_int + T_int < L); if (jump[i][lim_int + 1] == lim_int + 1) he = lim; else { he = jump[i][lim_int + 1]; if (he == -1) return false; else he = Coef * jump[i][lim_int + 1]; } } } else { he *= Coef; } int c = i; // debug(i, v, he, lim, T, Coef, lim_int); // assert(he >= lim); for (int z = 0; z < 2; z++) { if (arr[z] > he) { swap(arr[z], he); swap(who[z], c); } } } if (sz == n - 1) return true; // debug(v, lim); for (int b = 0; b < 2; b++) { // assert(who[b] != -1); int nv = v ^ (1 << who[b]); // debug(who[b]); Segs[sz] = arr[b] + T - 1; Seg_int[sz] = (Segs[sz]) / Coef; int nlim = arr[b]; // assert(nlim >= lim); // if (who[b] == 0) { // debug(v, nlim); // } // debug(arr[0], arr[1], Segs[sz], Seg_int[sz]); for (int j = sz; j >= 0; j--) { int c = prv[sz - j + 2][Seg_int[j]]; // debug(c, "hi", Seg_int[j], prv[2][0]); if (c == Seg_int[j]) { nlim = max(nlim, Segs[j] + 1); } else { nlim = max(nlim, (c + 1) * Coef); } // debug("upd", nlim); } // if (nv == 1) { // debug(nlim); // debug((Segs[sz] + T - 1) / Coef); // } if (smart_rec(nv, nlim)) return true; } return false; } bool check(int a, int b) { // debug(a, b); int new_val = (a + b - 1) / b; if (T_int != new_val) { T_int = new_val; // assert(T_int > 0); //need2reinit //jump {I start from j and want to find first guy >= T_int for (int i = 0; i < n; i++) { int bad = 0; for (int j = L - T_int; j < L; j++) { if (cnt[j] == 1 || !(orig_mask[j] & (1 << i))) bad++; } for (int j = L - T_int; j >= 0; j--) { if (j == L - T_int) jump[i][j] = -1; else jump[i][j] = jump[i][j + 1]; if (!bad) jump[i][j] = j; if (j > 0) { if (cnt[j + T_int - 1] == 1 || !(orig_mask[j + T_int - 1] & (1 << i))) bad--; if (cnt[j - 1] == 1 || !(orig_mask[j - 1] & (1 << i))) bad++; } } } } T = a; Coef = b; //multiply everything by b Len = b * L; // auto ff = smart_rec(0, 0); // assert(ff); return smart_rec(0, 0); // for (int i = 0; i < L; i++) { // for (int j = 0; j < b; j++) { // mask[i * b + j] = orig_mask[i]; // } // } /* memset(mask, 0, (Len + 1) * sizeof(int)); vector<int> masks(Len); for (int i = 0; i < L; i++) { for (int j = 0; j < b; j++) { mask[i * b + j] = orig_mask[i]; masks[i * b + j] = orig_mask[i]; } } //stupid for now return rec(0, masks, 0); */ } void solve() { T_int = -1; cin >> n >> L; for (int i = 0; i < L; i++) { cnt[i] = 0; orig_mask[i] = 0; } for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < L; j++) { a[i][j] = (s[j] == 'X' ? 0 : 1); cnt[j] += a[i][j]; if (a[i][j]) orig_mask[j] |= (1 << i); } } for (int i = 0; i < L; i++) { if (cnt[i] == 0) { cout << -1 << '\n'; return; } } for (int i = 0; i <= n; i++) { for (int j = 0; j < L; j++) { if (j == 0) prv[i][j] = -1; else prv[i][j] = prv[i][j - 1]; if (cnt[j] == i) prv[i][j] = j; } } vector<pair<int,int>> frac; for (int i = 0; i <= L; i++) { for (int j = 1; j <= n; j++) { if (__gcd(i, j) == 1) frac.emplace_back(i, j); } } // check(2, 3); // exit(0); // check(1, 2); // exit(0); // check(4, 3); // exit(0); sort(frac.begin(), frac.end(), [&](const pair<int,int>& a, const pair<int,int>& b) { return 1LL * a.first * b.second < 1LL * a.second * b.first; }); int ri = frac.size(); int le = 0; assert(frac[0] == make_pair(0, 1)); while (ri - le > 1) { int mid = (le + ri) / 2; if (check(frac[mid].first, frac[mid].second)) { le = mid; } else { ri = mid; } } cout << frac[le].first << "/" << frac[le].second << '\n'; // debug(states); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); auto start = std::chrono::high_resolution_clock::now(); #ifdef DEBUG freopen("input.txt", "r", stdin); // freopen("ans.out", "w", stdout); #endif int tst = 1; // cin >> tst; while (tst--) solve(); auto end = std::chrono::high_resolution_clock::now(); std::cerr << "Execution Time: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms" << std::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 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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | //orig_brute //#ifdef DEBUG //#define _GLIBCXX_DEBUG //#endif #pragma GCC optimize("O3") #include<bits/stdc++.h> using namespace std; #ifdef DEBUG #include "lib/debug.h" #else #define debug(...) 228 #endif #define pb push_back typedef long long ll; typedef long double ld; int n, L; const int maxN = 18; const int maxL = 100'002; int a[maxN][maxL]; int cnt[maxL]; int orig_mask[maxL]; //const int maxS = maxN * maxL + 2; //int mask[maxS]; int T; int Len; //find previous guy with such cnt, to optimize memory don't mul int prv[maxN + 1][maxL + 1]; int jump[maxN + 1][maxL + 1]; int Coef = 0; int T_int = -1; //int states = 0; bool rec(int v, vector<int> vals, int lim) { // lim = 0; vector<pair<int,int>> best; assert(vals.size() == Len); // debug(v, "??", vals); if (v == (1 << n) - 1) return true; for (int i = 0; i < n; i++) { if (v & (1 << i)) continue; int X = 0; int Y = 0; int fnd = -1; for (int j = 0; j < T; j++) { if (vals[j] & (1 << i)) X++; if (vals[j] == (1 << i)) Y++; } for (int j = 0; j <= Len - T; j++) { if (X == T && Y == 0 && j >= lim) { fnd = j; break; } else { if (j + T < Len) { if (vals[j] & (1 << i)) X--; if (vals[j] == (1 << i)) Y--; if (vals[j + T] & (1 << i)) X++; if (vals[j + T] == (1 << i)) Y++; } } } // if (T == 3) { // debug(fnd, "WUT", v, i); // } if (fnd == -1) return false; best.emplace_back(fnd, i); } if (best.size() == 1) return true; sort(best.begin(), best.end()); best.resize(2); for (int p = 0; p < best.size(); p++) { int A = best[p].first; int who = best[p].second; auto nmask = vals; for (int j = A; j < A + T; j++) { assert(nmask[j] & (1 << who)); assert(nmask[j] != (1 << who)); nmask[j] ^= (1 << who); } if (rec(v ^ (1 << who), nmask, A)) return true; } return false; } //recheck constraints!!! int Segs[maxN + 1]; int Seg_int[maxN + 1]; int smart_rec(int v, int lim) { if (v == (1 << n) - 1) return true; if (lim > Len - T) return false; int sz = __builtin_popcount(v); const int INF = 1e9; int arr[2] = {INF, INF}; int who[2] = {-1, -1}; int zapas = Coef - 1 - (T % Coef); int Limcoef = lim % Coef; /*if (Limcoef > zapas + 1) { if (v == 4) { debug("WUTT", Limcoef, zapas); } lim = ((lim + Coef - 1) / Coef) * Coef; }*/ if (lim > Len - T) return false; int lim_int = lim / Coef; // debug(v, lim); for (int i = 0; i < n; i++) { if (v & (1 << i)) continue; int he = jump[i][lim_int]; if (he == -1) return false; if (he == lim_int) { if (Limcoef <= zapas + 1) { he = lim; } else { // assert(lim_int + T_int < L); if (jump[i][lim_int + 1] == lim_int + 1) he = lim; else { he = jump[i][lim_int + 1]; if (he == -1) return false; else he = Coef * jump[i][lim_int + 1]; } } } else { he *= Coef; } int c = i; // debug(i, v, he, lim, T, Coef, lim_int); // assert(he >= lim); for (int z = 0; z < 2; z++) { if (arr[z] > he) { swap(arr[z], he); swap(who[z], c); } } } if (sz == n - 1) return true; // debug(v, lim); for (int b = 0; b < 2; b++) { // assert(who[b] != -1); int nv = v ^ (1 << who[b]); // debug(who[b]); Segs[sz] = arr[b] + T - 1; Seg_int[sz] = (Segs[sz]) / Coef; int nlim = arr[b]; // assert(nlim >= lim); // if (who[b] == 0) { // debug(v, nlim); // } // debug(arr[0], arr[1], Segs[sz], Seg_int[sz]); for (int j = sz; j >= 0; j--) { int c = prv[sz - j + 2][Seg_int[j]]; // debug(c, "hi", Seg_int[j], prv[2][0]); if (c == Seg_int[j]) { nlim = max(nlim, Segs[j] + 1); } else { nlim = max(nlim, (c + 1) * Coef); } // debug("upd", nlim); } // if (nv == 1) { // debug(nlim); // debug((Segs[sz] + T - 1) / Coef); // } if (smart_rec(nv, nlim)) return true; } return false; } bool check(int a, int b) { // debug(a, b); int new_val = (a + b - 1) / b; if (T_int != new_val) { T_int = new_val; // assert(T_int > 0); //need2reinit //jump {I start from j and want to find first guy >= T_int for (int i = 0; i < n; i++) { int bad = 0; for (int j = L - T_int; j < L; j++) { if (cnt[j] == 1 || !(orig_mask[j] & (1 << i))) bad++; } for (int j = L - T_int; j >= 0; j--) { if (j == L - T_int) jump[i][j] = -1; else jump[i][j] = jump[i][j + 1]; if (!bad) jump[i][j] = j; if (j > 0) { if (cnt[j + T_int - 1] == 1 || !(orig_mask[j + T_int - 1] & (1 << i))) bad--; if (cnt[j - 1] == 1 || !(orig_mask[j - 1] & (1 << i))) bad++; } } } } T = a; Coef = b; //multiply everything by b Len = b * L; // auto ff = smart_rec(0, 0); // assert(ff); return smart_rec(0, 0); // for (int i = 0; i < L; i++) { // for (int j = 0; j < b; j++) { // mask[i * b + j] = orig_mask[i]; // } // } /* memset(mask, 0, (Len + 1) * sizeof(int)); vector<int> masks(Len); for (int i = 0; i < L; i++) { for (int j = 0; j < b; j++) { mask[i * b + j] = orig_mask[i]; masks[i * b + j] = orig_mask[i]; } } //stupid for now return rec(0, masks, 0); */ } void solve() { T_int = -1; cin >> n >> L; for (int i = 0; i < L; i++) { cnt[i] = 0; orig_mask[i] = 0; } for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < L; j++) { a[i][j] = (s[j] == 'X' ? 0 : 1); cnt[j] += a[i][j]; if (a[i][j]) orig_mask[j] |= (1 << i); } } for (int i = 0; i < L; i++) { if (cnt[i] == 0) { cout << -1 << '\n'; return; } } for (int i = 0; i <= n; i++) { for (int j = 0; j < L; j++) { if (j == 0) prv[i][j] = -1; else prv[i][j] = prv[i][j - 1]; if (cnt[j] == i) prv[i][j] = j; } } vector<pair<int,int>> frac; for (int i = 0; i <= L; i++) { for (int j = 1; j <= n; j++) { if (__gcd(i, j) == 1) frac.emplace_back(i, j); } } // check(2, 3); // exit(0); // check(1, 2); // exit(0); // check(4, 3); // exit(0); sort(frac.begin(), frac.end(), [&](const pair<int,int>& a, const pair<int,int>& b) { return 1LL * a.first * b.second < 1LL * a.second * b.first; }); int ri = frac.size(); int le = 0; assert(frac[0] == make_pair(0, 1)); while (ri - le > 1) { int mid = (le + ri) / 2; if (check(frac[mid].first, frac[mid].second)) { le = mid; } else { ri = mid; } } cout << frac[le].first << "/" << frac[le].second << '\n'; // debug(states); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); auto start = std::chrono::high_resolution_clock::now(); #ifdef DEBUG freopen("input.txt", "r", stdin); // freopen("ans.out", "w", stdout); #endif int tst = 1; // cin >> tst; while (tst--) solve(); auto end = std::chrono::high_resolution_clock::now(); std::cerr << "Execution Time: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms" << std::endl; return 0; } |