#include "bits/stdc++.h" using namespace std; #define rep(i, b, e) for(int i = (b); i <= (e); i++) #define per(i, b, e) for(int i = (e); i >= (b); i--) #define FOR(i, b, e) rep(i, b, (e) - 1) #define SZ(x) int(x.size()) #define all(x) x.begin(), x.end() #define pb push_back #define mp make_pair #define st first #define nd second using ll = long long; using vi = vector<int>; using pii = pair<int, int>; auto &operator<<(auto &o, pair<auto, auto> p) { return o << "(" << p.st << ", " << p.nd << ")"; } auto operator<<(auto &o, auto x)->decltype(end(x), o) { o << "{"; int i=0; for(auto e: x) o << ", " + 2*!i++ << e; return o << "}"; } #ifdef LOCAL #define deb(x...) cerr << "[" #x "]: ", [](auto...$) { \ ((cerr << $ << "; "),...) << endl; }(x) #else #define deb(...) #endif #define LSB(x) ((x)&(-x)) vi dal, rem; vector<pii> rangeW; vector<vi> jestem; int RR; bool pick(int kt) { if(kt < 0) return 1; FOR(x, 0, RR) { bool good = 1; for(auto &id: jestem[kt]) { int satisfy = x <= rangeW[id].st || x >= rangeW[id].nd; if(dal[id] + satisfy == 0 && rem[id] == 1) { good = 0; break; } } if(!good) continue; for(auto &id: jestem[kt]) { dal[id] += x <= rangeW[id].st || x >= rangeW[id].nd; rem[id]--; } if(pick(kt - 1)) return 1; for(auto &id: jestem[kt]) { dal[id] -= x <= rangeW[id].st || x >= rangeW[id].nd; rem[id]++; } } return 0; } bool check(pii ulamek, vector<string> &s) { int n = SZ(s), L = SZ(s[0]); auto [d, scale] = ulamek; dal = rem = {}; rangeW = {}; jestem = vector<vi>(n); int full = (1 << n) - 1; auto dodajJestem = [](int mask) { for(int mk = mask; mk; mk ^= LSB(mk)) { jestem[__builtin_ctz(mk)].pb(SZ(dal)); } dal.pb(0), rem.pb(__builtin_popcount(mask)); }; FOR(j, 0, L) { int kropki = 0; FOR(i, 0, n) if(s[i][j] == '.') kropki ^= 1 << i; FOR(k, 0, scale) { int me = j * scale + k; rangeW.pb({me - d, me + 1}); dodajJestem(kropki); for(int mk = full ^ kropki; mk; mk ^= LSB(mk)) { rangeW.pb({me - d, me + 1}); dodajJestem(LSB(mk)); } } } RR = L * scale - d + 1; return pick(n - 1); } void solve() { int n, L; cin >> n >> L; vector<string> s(n); for(auto &x: s) cin >> x; FOR(j, 0, L) { bool good = 0; FOR(i, 0, n) good |= s[i][j] == '.'; if(!good) { cout << "-1\n"; return; } } vector<pii> ulamki; FOR(dol, 1, n + 1) FOR(gora, 0, L * dol + 1) { int a = gora, b = dol, g = gcd(gora, dol); ulamki.pb({a / g, b / g}); } auto ulComp = [](const pii &a, const pii &b) { return a.st * b.nd < b.st * a.nd; }; sort(all(ulamki), ulComp); ulamki.erase(unique(all(ulamki)), ulamki.end()); int l = 0, r = SZ(ulamki); while(l + 1 < r) { int mid = (l + r) / 2; (check(ulamki[mid], s) ? l : r) = mid; } cout << ulamki[l].st << '/' << ulamki[l].nd << '\n'; } int main() { cin.tie(0)->sync_with_stdio(0); int tt = 1; // cin >> tt; FOR(te, 0, tt) solve(); 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 | #include "bits/stdc++.h" using namespace std; #define rep(i, b, e) for(int i = (b); i <= (e); i++) #define per(i, b, e) for(int i = (e); i >= (b); i--) #define FOR(i, b, e) rep(i, b, (e) - 1) #define SZ(x) int(x.size()) #define all(x) x.begin(), x.end() #define pb push_back #define mp make_pair #define st first #define nd second using ll = long long; using vi = vector<int>; using pii = pair<int, int>; auto &operator<<(auto &o, pair<auto, auto> p) { return o << "(" << p.st << ", " << p.nd << ")"; } auto operator<<(auto &o, auto x)->decltype(end(x), o) { o << "{"; int i=0; for(auto e: x) o << ", " + 2*!i++ << e; return o << "}"; } #ifdef LOCAL #define deb(x...) cerr << "[" #x "]: ", [](auto...$) { \ ((cerr << $ << "; "),...) << endl; }(x) #else #define deb(...) #endif #define LSB(x) ((x)&(-x)) vi dal, rem; vector<pii> rangeW; vector<vi> jestem; int RR; bool pick(int kt) { if(kt < 0) return 1; FOR(x, 0, RR) { bool good = 1; for(auto &id: jestem[kt]) { int satisfy = x <= rangeW[id].st || x >= rangeW[id].nd; if(dal[id] + satisfy == 0 && rem[id] == 1) { good = 0; break; } } if(!good) continue; for(auto &id: jestem[kt]) { dal[id] += x <= rangeW[id].st || x >= rangeW[id].nd; rem[id]--; } if(pick(kt - 1)) return 1; for(auto &id: jestem[kt]) { dal[id] -= x <= rangeW[id].st || x >= rangeW[id].nd; rem[id]++; } } return 0; } bool check(pii ulamek, vector<string> &s) { int n = SZ(s), L = SZ(s[0]); auto [d, scale] = ulamek; dal = rem = {}; rangeW = {}; jestem = vector<vi>(n); int full = (1 << n) - 1; auto dodajJestem = [](int mask) { for(int mk = mask; mk; mk ^= LSB(mk)) { jestem[__builtin_ctz(mk)].pb(SZ(dal)); } dal.pb(0), rem.pb(__builtin_popcount(mask)); }; FOR(j, 0, L) { int kropki = 0; FOR(i, 0, n) if(s[i][j] == '.') kropki ^= 1 << i; FOR(k, 0, scale) { int me = j * scale + k; rangeW.pb({me - d, me + 1}); dodajJestem(kropki); for(int mk = full ^ kropki; mk; mk ^= LSB(mk)) { rangeW.pb({me - d, me + 1}); dodajJestem(LSB(mk)); } } } RR = L * scale - d + 1; return pick(n - 1); } void solve() { int n, L; cin >> n >> L; vector<string> s(n); for(auto &x: s) cin >> x; FOR(j, 0, L) { bool good = 0; FOR(i, 0, n) good |= s[i][j] == '.'; if(!good) { cout << "-1\n"; return; } } vector<pii> ulamki; FOR(dol, 1, n + 1) FOR(gora, 0, L * dol + 1) { int a = gora, b = dol, g = gcd(gora, dol); ulamki.pb({a / g, b / g}); } auto ulComp = [](const pii &a, const pii &b) { return a.st * b.nd < b.st * a.nd; }; sort(all(ulamki), ulComp); ulamki.erase(unique(all(ulamki)), ulamki.end()); int l = 0, r = SZ(ulamki); while(l + 1 < r) { int mid = (l + r) / 2; (check(ulamki[mid], s) ? l : r) = mid; } cout << ulamki[l].st << '/' << ulamki[l].nd << '\n'; } int main() { cin.tie(0)->sync_with_stdio(0); int tt = 1; // cin >> tt; FOR(te, 0, tt) solve(); return 0; } |