#include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #ifdef DEBUG auto operator<<(auto&o,auto x)->decltype(x.end(),o); auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto&operator<<(auto&o,tuple<auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<")";} auto&operator<<(auto&o,tuple<auto,auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<", "<<get<3>(t)<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif /* * Opis: O(\log max\_val), szuka największego \texttt{a/b}, że \texttt{is\_ok(a/b)} oraz \texttt{0 <= a,b <= max\_value}. * Zakłada, że \texttt{is\_ok(0) == true}. */ using Frac = pair<LL, LL>; Frac my_max(Frac l, Frac r) { return l.first * __int128_t(r.second) > r.first * __int128_t(l.second) ? l : r; } Frac binsearch(LL max_value, function<bool (Frac)> is_ok) { assert(is_ok(pair(0, 1)) == true); Frac left = {0, 1}, right = {1, 0}, best_found = left; int current_dir = 0; while(max(left.first, left.second) <= max_value) { best_found = my_max(best_found, left); auto get_frac = [&](LL mul) { LL mull = current_dir ? 1 : mul; LL mulr = current_dir ? mul : 1; return pair(left.first * mull + right.first * mulr, left.second * mull + right.second * mulr); }; auto is_good_mul = [&](LL mul) { Frac mid = get_frac(mul); return max(mid.first, mid.second) <= max_value and is_ok(mid) == current_dir; }; LL power = 1; for(; is_good_mul(power); power *= 2) {} LL bl = power / 2 + 1, br = power; while(bl != br) { LL bm = (bl + br) / 2; if(not is_good_mul(bm)) br = bm; else bl = bm + 1; } tie(left, right) = pair(get_frac(bl - 1), get_frac(bl)); if(current_dir == 0) swap(left, right); current_dir ^= 1; } return best_found; } Frac to_frac(LL x) { return {x, 1}; } Frac operator+(Frac a, Frac b) { auto [xa, ya] = a; auto [xb, yb] = b; if (ya != yb) { return {xa * yb + xb * ya, ya * yb}; } else { return {xa + xb, ya}; } } Frac operator-(Frac a, Frac b) { auto [xa, ya] = a; auto [xb, yb] = b; if (ya != yb) { return {xa * yb - xb * ya, ya * yb}; } else { return {xa - xb, ya}; } } int floor(Frac a) { auto [x, y] = a; return int(x / y); } int ceil(Frac a) { auto [x, y] = a; return int((x + y - 1) / y); } bool lt(Frac a, Frac b) { return (a - b).first < 0; } bool leq(Frac a, Frac b) { return (a - b).first <= 0; } bool eq(Frac a, Frac b) { return (a - b).first == 0; } using T = pair<int, int>; struct Node { T sum = {0, 0}; int lazy = 0; int sz = 1; }; void push_to_sons(Node &n, Node &l, Node &r) { auto push_to_son = [&](Node &c) { c.sum.first += n.lazy; c.lazy += n.lazy; }; push_to_son(l); push_to_son(r); n.lazy = 0; } Node merge(Node l, Node r) { return Node{ .sum = min(l.sum, r.sum), .lazy = 0, .sz = l.sz + r.sz }; } void add_to_base(Node &n, int val) { n.sum.first += val; n.lazy += val; } struct Tree { vector<Node> tree; int sz = 1; Tree(int n, const vector<int>& initial) { while(sz < n) sz *= 2; tree.resize(sz * 2); const int len = min(sz, ssize(initial)); REP(i, len) { tree[i + sz].sum = {initial[i], -i}; } for(int v = sz - 1; v >= 1; v--) tree[v] = merge(tree[2 * v], tree[2 * v + 1]); } void push(int v) { push_to_sons(tree[v], tree[2 * v], tree[2 * v + 1]); } Node get(int l, int r, int v = 1) { if(l == 0 and r == tree[v].sz - 1) return tree[v]; push(v); int m = tree[v].sz / 2; if(r < m) return get(l, r, 2 * v); else if(m <= l) return get(l - m, r - m, 2 * v + 1); else return merge(get(l, m - 1, 2 * v), get(0, r - m, 2 * v + 1)); } void update(int l, int r, int val, int v = 1) { if(l == 0 && r == tree[v].sz - 1) { add_to_base(tree[v], val); return; } push(v); int m = tree[v].sz / 2; if(r < m) update(l, r, val, 2 * v); else if(m <= l) update(l - m, r - m, val, 2 * v + 1); else { update(l, m - 1, val, 2 * v); update(0, r - m, val, 2 * v + 1); } tree[v] = merge(tree[2 * v], tree[2 * v + 1]); } }; /* * Opis: Kolejka wspierająca dowolną operację łączną, O(1) zamortyzowany. * Konstruktor przyjmuje dwuargumentową funkcję oraz jej element neutralny. * Dla minów jest \texttt{AssocQueue<int> q([](int a, int b)\{ return min(a, b); \}, numeric\_limits<int>::max());} */ template<typename T> struct AssocQueue { using fn = function<T(T, T)>; fn f; vector<pair<T, T>> s1, s2; // {x, f(pref)} AssocQueue(fn _f, T e = T()) : f(_f), s1({{e, e}}), s2({{e, e}}) {} void mv() { if (ssize(s2) == 1) while (ssize(s1) > 1) { s2.emplace_back(s1.back().first, f(s1.back().first, s2.back().second)); s1.pop_back(); } } void emplace(T x) { s1.emplace_back(x, f(s1.back().second, x)); } void pop() { mv(); s2.pop_back(); } T calc() { return f(s2.back().second, s1.back().second); } T front() { mv(); return s2.back().first; } int size() { return ssize(s1) + ssize(s2) - 2; } void clear() { s1.resize(1); s2.resize(1); } }; void solve() { int n, L; cin >> n >> L; vector occupied(n, vector (L, false)); REP(i, n) { string s; cin >> s; REP(j, L) occupied[i][j] = s[j] == 'X'; } debug(n, L, occupied); REP(i, L) { int sum = 0; REP(j, n) { sum += occupied[j][i]; } if (sum == n) { cout << -1 << '\n'; return; } } auto adjust_frac = [&](Frac frac) -> Frac { auto [a, b] = frac; if (b <= n) return frac; Frac best = to_frac(L + 1); FOR(k, 1, n) { LL x = (a * k + b - 1) / b; best = min(best, Frac{x, k}, lt); } //debug(frac, best); return best; }; vector initial_cnt(n * L, 0); vector initial_can_sleep(n * L, false); map<Frac, bool> is_okay_cache; auto is_okay = [&](Frac frac) -> bool { if (frac.first == 0) return true; frac = adjust_frac(frac); { auto it = is_okay_cache.find(frac); if (it != is_okay_cache.end()) return it->second; } const auto hash = frac; debug(frac); const int length = int(frac.first); const int factor = int(frac.second); const int size = L * int(factor); REP(i, L) { int sum = 0; REP(j, n) sum += not occupied[j][i]; REP(j, factor) initial_cnt[i * factor + j] = sum; } debug(initial_cnt); Tree tree(size, initial_cnt); // return {can, blocker} auto can_sleep_here = [&](int position) -> pair<bool, int> { if (position + length > size) return {false, size}; const auto [value, id] = tree.get(position, position + length - 1).sum; if (value > 1) return {true, -1}; else return {false, -id}; }; auto get_elem = [&](int position) { if (position < size) return initial_cnt[position]; return 0; }; AssocQueue<int> q([](int a, int b){ return min(a, b); }, numeric_limits<int>::max()); { const int len = min(length, size); REP(i, len) { q.emplace(get_elem(i)); } } REP(i, size) { initial_can_sleep[i] = q.calc(); q.emplace(get_elem(i + length)); q.pop(); } debug(initial_can_sleep); vector initial_can_sleep_compressed(L, -1); REP(i, size) { if (initial_can_sleep[i]) initial_can_sleep_compressed[i / factor] = i; } vector options(n, vector<int>{}); REP(i, n) { int last_occupied = size; for (int j = L - 1; j >= 0; --j) { if (occupied[i][j]) last_occupied = j * factor; if (initial_can_sleep[j] == -1) continue; const int bound = min(last_occupied - length, initial_can_sleep_compressed[j]); if (bound >= j * factor) options[i].emplace_back(bound); } reverse(options[i].begin(), options[i].end()); } debug(options); auto find_first_good = [&](int id) -> int { int last_obstacle = -1; while (true) { auto it = upper_bound(options[id].begin(), options[id].end(), last_obstacle); if (it == options[id].end()) return -1; const int position = max((*it) / factor * factor, last_obstacle + 1); const auto [value, obstacle] = can_sleep_here(position); if (value) return position; last_obstacle = obstacle; } }; auto mark_as_sleep = [&](int position) { tree.update(position, position + length - 1, -1); }; auto unmark_as_sleep = [&](int position) { tree.update(position, position + length - 1, 1); }; using Last = pair<int, int>; auto rec = [&](auto&& self, int mask, Last last) -> bool { if (mask == 0) return true; vector<Last> moves; REP(i, n) { if (((mask >> i) & 1) == 0) continue; auto my_time = find_first_good(i); if (my_time == -1) return false; moves.emplace_back(my_time, i); } sort(moves.begin(), moves.end()); while (ssize(moves) > 2) { moves.pop_back(); } for (const auto& [my_time, id] : moves) { if (Last{my_time, id} < last) continue; mark_as_sleep(my_time); const int new_mask = mask ^ (1 << id); if (self(self, new_mask, Last{my_time, id})) return true; unmark_as_sleep(my_time); } return false; }; const bool ret = rec(rec, (1 << n) - 1, Last{-1, -1}); is_okay_cache[hash] = ret; return ret; }; const int binsearch_limit = max(n, L); auto [nom, denom] = binsearch(binsearch_limit, is_okay); cout << nom << '/' << denom << '\n'; } int main() { cin.tie(0)->sync_with_stdio(0); #ifdef TESTS int t; cin >> t; REP(tt, t) { if (tt % 10000 == 0) cerr << tt << endl; solve(); } #else solve(); #endif }
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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #ifdef DEBUG auto operator<<(auto&o,auto x)->decltype(x.end(),o); auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto&operator<<(auto&o,tuple<auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<")";} auto&operator<<(auto&o,tuple<auto,auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<", "<<get<3>(t)<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif /* * Opis: O(\log max\_val), szuka największego \texttt{a/b}, że \texttt{is\_ok(a/b)} oraz \texttt{0 <= a,b <= max\_value}. * Zakłada, że \texttt{is\_ok(0) == true}. */ using Frac = pair<LL, LL>; Frac my_max(Frac l, Frac r) { return l.first * __int128_t(r.second) > r.first * __int128_t(l.second) ? l : r; } Frac binsearch(LL max_value, function<bool (Frac)> is_ok) { assert(is_ok(pair(0, 1)) == true); Frac left = {0, 1}, right = {1, 0}, best_found = left; int current_dir = 0; while(max(left.first, left.second) <= max_value) { best_found = my_max(best_found, left); auto get_frac = [&](LL mul) { LL mull = current_dir ? 1 : mul; LL mulr = current_dir ? mul : 1; return pair(left.first * mull + right.first * mulr, left.second * mull + right.second * mulr); }; auto is_good_mul = [&](LL mul) { Frac mid = get_frac(mul); return max(mid.first, mid.second) <= max_value and is_ok(mid) == current_dir; }; LL power = 1; for(; is_good_mul(power); power *= 2) {} LL bl = power / 2 + 1, br = power; while(bl != br) { LL bm = (bl + br) / 2; if(not is_good_mul(bm)) br = bm; else bl = bm + 1; } tie(left, right) = pair(get_frac(bl - 1), get_frac(bl)); if(current_dir == 0) swap(left, right); current_dir ^= 1; } return best_found; } Frac to_frac(LL x) { return {x, 1}; } Frac operator+(Frac a, Frac b) { auto [xa, ya] = a; auto [xb, yb] = b; if (ya != yb) { return {xa * yb + xb * ya, ya * yb}; } else { return {xa + xb, ya}; } } Frac operator-(Frac a, Frac b) { auto [xa, ya] = a; auto [xb, yb] = b; if (ya != yb) { return {xa * yb - xb * ya, ya * yb}; } else { return {xa - xb, ya}; } } int floor(Frac a) { auto [x, y] = a; return int(x / y); } int ceil(Frac a) { auto [x, y] = a; return int((x + y - 1) / y); } bool lt(Frac a, Frac b) { return (a - b).first < 0; } bool leq(Frac a, Frac b) { return (a - b).first <= 0; } bool eq(Frac a, Frac b) { return (a - b).first == 0; } using T = pair<int, int>; struct Node { T sum = {0, 0}; int lazy = 0; int sz = 1; }; void push_to_sons(Node &n, Node &l, Node &r) { auto push_to_son = [&](Node &c) { c.sum.first += n.lazy; c.lazy += n.lazy; }; push_to_son(l); push_to_son(r); n.lazy = 0; } Node merge(Node l, Node r) { return Node{ .sum = min(l.sum, r.sum), .lazy = 0, .sz = l.sz + r.sz }; } void add_to_base(Node &n, int val) { n.sum.first += val; n.lazy += val; } struct Tree { vector<Node> tree; int sz = 1; Tree(int n, const vector<int>& initial) { while(sz < n) sz *= 2; tree.resize(sz * 2); const int len = min(sz, ssize(initial)); REP(i, len) { tree[i + sz].sum = {initial[i], -i}; } for(int v = sz - 1; v >= 1; v--) tree[v] = merge(tree[2 * v], tree[2 * v + 1]); } void push(int v) { push_to_sons(tree[v], tree[2 * v], tree[2 * v + 1]); } Node get(int l, int r, int v = 1) { if(l == 0 and r == tree[v].sz - 1) return tree[v]; push(v); int m = tree[v].sz / 2; if(r < m) return get(l, r, 2 * v); else if(m <= l) return get(l - m, r - m, 2 * v + 1); else return merge(get(l, m - 1, 2 * v), get(0, r - m, 2 * v + 1)); } void update(int l, int r, int val, int v = 1) { if(l == 0 && r == tree[v].sz - 1) { add_to_base(tree[v], val); return; } push(v); int m = tree[v].sz / 2; if(r < m) update(l, r, val, 2 * v); else if(m <= l) update(l - m, r - m, val, 2 * v + 1); else { update(l, m - 1, val, 2 * v); update(0, r - m, val, 2 * v + 1); } tree[v] = merge(tree[2 * v], tree[2 * v + 1]); } }; /* * Opis: Kolejka wspierająca dowolną operację łączną, O(1) zamortyzowany. * Konstruktor przyjmuje dwuargumentową funkcję oraz jej element neutralny. * Dla minów jest \texttt{AssocQueue<int> q([](int a, int b)\{ return min(a, b); \}, numeric\_limits<int>::max());} */ template<typename T> struct AssocQueue { using fn = function<T(T, T)>; fn f; vector<pair<T, T>> s1, s2; // {x, f(pref)} AssocQueue(fn _f, T e = T()) : f(_f), s1({{e, e}}), s2({{e, e}}) {} void mv() { if (ssize(s2) == 1) while (ssize(s1) > 1) { s2.emplace_back(s1.back().first, f(s1.back().first, s2.back().second)); s1.pop_back(); } } void emplace(T x) { s1.emplace_back(x, f(s1.back().second, x)); } void pop() { mv(); s2.pop_back(); } T calc() { return f(s2.back().second, s1.back().second); } T front() { mv(); return s2.back().first; } int size() { return ssize(s1) + ssize(s2) - 2; } void clear() { s1.resize(1); s2.resize(1); } }; void solve() { int n, L; cin >> n >> L; vector occupied(n, vector (L, false)); REP(i, n) { string s; cin >> s; REP(j, L) occupied[i][j] = s[j] == 'X'; } debug(n, L, occupied); REP(i, L) { int sum = 0; REP(j, n) { sum += occupied[j][i]; } if (sum == n) { cout << -1 << '\n'; return; } } auto adjust_frac = [&](Frac frac) -> Frac { auto [a, b] = frac; if (b <= n) return frac; Frac best = to_frac(L + 1); FOR(k, 1, n) { LL x = (a * k + b - 1) / b; best = min(best, Frac{x, k}, lt); } //debug(frac, best); return best; }; vector initial_cnt(n * L, 0); vector initial_can_sleep(n * L, false); map<Frac, bool> is_okay_cache; auto is_okay = [&](Frac frac) -> bool { if (frac.first == 0) return true; frac = adjust_frac(frac); { auto it = is_okay_cache.find(frac); if (it != is_okay_cache.end()) return it->second; } const auto hash = frac; debug(frac); const int length = int(frac.first); const int factor = int(frac.second); const int size = L * int(factor); REP(i, L) { int sum = 0; REP(j, n) sum += not occupied[j][i]; REP(j, factor) initial_cnt[i * factor + j] = sum; } debug(initial_cnt); Tree tree(size, initial_cnt); // return {can, blocker} auto can_sleep_here = [&](int position) -> pair<bool, int> { if (position + length > size) return {false, size}; const auto [value, id] = tree.get(position, position + length - 1).sum; if (value > 1) return {true, -1}; else return {false, -id}; }; auto get_elem = [&](int position) { if (position < size) return initial_cnt[position]; return 0; }; AssocQueue<int> q([](int a, int b){ return min(a, b); }, numeric_limits<int>::max()); { const int len = min(length, size); REP(i, len) { q.emplace(get_elem(i)); } } REP(i, size) { initial_can_sleep[i] = q.calc(); q.emplace(get_elem(i + length)); q.pop(); } debug(initial_can_sleep); vector initial_can_sleep_compressed(L, -1); REP(i, size) { if (initial_can_sleep[i]) initial_can_sleep_compressed[i / factor] = i; } vector options(n, vector<int>{}); REP(i, n) { int last_occupied = size; for (int j = L - 1; j >= 0; --j) { if (occupied[i][j]) last_occupied = j * factor; if (initial_can_sleep[j] == -1) continue; const int bound = min(last_occupied - length, initial_can_sleep_compressed[j]); if (bound >= j * factor) options[i].emplace_back(bound); } reverse(options[i].begin(), options[i].end()); } debug(options); auto find_first_good = [&](int id) -> int { int last_obstacle = -1; while (true) { auto it = upper_bound(options[id].begin(), options[id].end(), last_obstacle); if (it == options[id].end()) return -1; const int position = max((*it) / factor * factor, last_obstacle + 1); const auto [value, obstacle] = can_sleep_here(position); if (value) return position; last_obstacle = obstacle; } }; auto mark_as_sleep = [&](int position) { tree.update(position, position + length - 1, -1); }; auto unmark_as_sleep = [&](int position) { tree.update(position, position + length - 1, 1); }; using Last = pair<int, int>; auto rec = [&](auto&& self, int mask, Last last) -> bool { if (mask == 0) return true; vector<Last> moves; REP(i, n) { if (((mask >> i) & 1) == 0) continue; auto my_time = find_first_good(i); if (my_time == -1) return false; moves.emplace_back(my_time, i); } sort(moves.begin(), moves.end()); while (ssize(moves) > 2) { moves.pop_back(); } for (const auto& [my_time, id] : moves) { if (Last{my_time, id} < last) continue; mark_as_sleep(my_time); const int new_mask = mask ^ (1 << id); if (self(self, new_mask, Last{my_time, id})) return true; unmark_as_sleep(my_time); } return false; }; const bool ret = rec(rec, (1 << n) - 1, Last{-1, -1}); is_okay_cache[hash] = ret; return ret; }; const int binsearch_limit = max(n, L); auto [nom, denom] = binsearch(binsearch_limit, is_okay); cout << nom << '/' << denom << '\n'; } int main() { cin.tie(0)->sync_with_stdio(0); #ifdef TESTS int t; cin >> t; REP(tt, t) { if (tt % 10000 == 0) cerr << tt << endl; solve(); } #else solve(); #endif } |