// Author: Olaf Surgut (surgutti)
// Created on 12-03-2025 23:55:25
#include "bits/stdc++.h"
using namespace std;
// #define int long long
#define ll long long
#define ld long double
#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define all(x) begin(x),end(x)
#define FOR(i,l,r) for(int i=(l);i<=(r);i++)
#define ROF(i,r,l) for(int i=(r);i>=(l);i--)
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 << ","+!i++ << e;
return o << "}";}
#ifdef LOCAL
#define debug(x...) cerr << "[" #x "]: ", [](auto...$) { \
((cerr << $ << "; "),...) << endl; }(x)
#else
#define debug(...)
#endif
#define rep(i,a,b) for(int i = a; i < (b); i++)
using pii = pair<int, int>;
using vi = vector<int>;
const int N = 18;
const int L = 100000 + 7;
int n, l, m;
string grid[N];
int cnt[L * N];
vi dp[1 << N];
void solve(int tt) {
cin >> n >> l;
rep(i, 0, n)
cin >> grid[i];
pii res{0, 1};
ROF(x, n, 1) {
bool done = false;
rep(y, x + 1, n) {
if (y % x == 0) {
done = true;
}
}
if (done)
continue;
m = x * l;
rep(j, 0, m) {
cnt[j] = 0;
rep(i, 0, n)
if (grid[i][j / x] == '.')
cnt[j]++;
}
if (*min_element(cnt, cnt + m) == 0) {
cout << "-1\n";
return;
}
auto check = [&](int t) {
rep(mask, 1, 1 << n) {
dp[mask].clear();
rep(bit, 0, n) if (mask >> bit & 1) {
auto const& dq = dp[mask ^ (1 << bit)];
for (int i : dq)
rep(k, i, i + t)
cnt[k]--;
int j = (sz(dq) > 0 ? dq.back() : 0);
int ile = 0;
rep(k, j, j + t) {
if (cnt[k] < 2 || grid[bit][k / x] == 'X')
ile++;
}
while (j + t - 1 < m) {
if (ile == 0) {
auto new_dp = dq;
new_dp.insert(new_dp.begin(), j);
if (sz(dp[mask]) == 0 || dp[mask] > new_dp)
dp[mask] = new_dp;
break;
}
if (sz(dp[mask]) && dp[mask][0] <= j)
break;
if (cnt[j] < 2 || grid[bit][j / x] == 'X')
ile--;
if (cnt[j + t] < 2 || grid[bit][(j + t) / x] == 'X')
ile++;
j++;
}
for (int i : dq)
rep(k, i, i + t)
cnt[k]++;
}
if (sz(dp[mask]) == 0)
return false;
}
return true;
};
int left = 0, right = m / 2 + 1;
while ((left + 1) * res.nd <= res.st * x)
left++;
if (res != pii{0, 1}) {
while ((right - 1) * res.nd >= (res.st + 1) * x)
right--;
}
while (right - left > 1) {
int mid = (left + right) >> 1;
if (check(mid))
left = mid;
else
right = mid;
}
if (left * res.nd > res.st * x) {
res = {left, x};
}
}
int gg = gcd(res.st, res.nd);
cout << res.st / gg << "/" << res.nd / gg << '\n';
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int tt = 1;
// cin >> tt;
FOR(i, 1, tt) {
solve(i);
}
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 | // Author: Olaf Surgut (surgutti) // Created on 12-03-2025 23:55:25 #include "bits/stdc++.h" using namespace std; // #define int long long #define ll long long #define ld long double #define endl '\n' #define st first #define nd second #define pb push_back #define eb emplace_back #define sz(x) (int)(x).size() #define all(x) begin(x),end(x) #define FOR(i,l,r) for(int i=(l);i<=(r);i++) #define ROF(i,r,l) for(int i=(r);i>=(l);i--) 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 << ","+!i++ << e; return o << "}";} #ifdef LOCAL #define debug(x...) cerr << "[" #x "]: ", [](auto...$) { \ ((cerr << $ << "; "),...) << endl; }(x) #else #define debug(...) #endif #define rep(i,a,b) for(int i = a; i < (b); i++) using pii = pair<int, int>; using vi = vector<int>; const int N = 18; const int L = 100000 + 7; int n, l, m; string grid[N]; int cnt[L * N]; vi dp[1 << N]; void solve(int tt) { cin >> n >> l; rep(i, 0, n) cin >> grid[i]; pii res{0, 1}; ROF(x, n, 1) { bool done = false; rep(y, x + 1, n) { if (y % x == 0) { done = true; } } if (done) continue; m = x * l; rep(j, 0, m) { cnt[j] = 0; rep(i, 0, n) if (grid[i][j / x] == '.') cnt[j]++; } if (*min_element(cnt, cnt + m) == 0) { cout << "-1\n"; return; } auto check = [&](int t) { rep(mask, 1, 1 << n) { dp[mask].clear(); rep(bit, 0, n) if (mask >> bit & 1) { auto const& dq = dp[mask ^ (1 << bit)]; for (int i : dq) rep(k, i, i + t) cnt[k]--; int j = (sz(dq) > 0 ? dq.back() : 0); int ile = 0; rep(k, j, j + t) { if (cnt[k] < 2 || grid[bit][k / x] == 'X') ile++; } while (j + t - 1 < m) { if (ile == 0) { auto new_dp = dq; new_dp.insert(new_dp.begin(), j); if (sz(dp[mask]) == 0 || dp[mask] > new_dp) dp[mask] = new_dp; break; } if (sz(dp[mask]) && dp[mask][0] <= j) break; if (cnt[j] < 2 || grid[bit][j / x] == 'X') ile--; if (cnt[j + t] < 2 || grid[bit][(j + t) / x] == 'X') ile++; j++; } for (int i : dq) rep(k, i, i + t) cnt[k]++; } if (sz(dp[mask]) == 0) return false; } return true; }; int left = 0, right = m / 2 + 1; while ((left + 1) * res.nd <= res.st * x) left++; if (res != pii{0, 1}) { while ((right - 1) * res.nd >= (res.st + 1) * x) right--; } while (right - left > 1) { int mid = (left + right) >> 1; if (check(mid)) left = mid; else right = mid; } if (left * res.nd > res.st * x) { res = {left, x}; } } int gg = gcd(res.st, res.nd); cout << res.st / gg << "/" << res.nd / gg << '\n'; } signed main() { cin.tie(0)->sync_with_stdio(0); int tt = 1; // cin >> tt; FOR(i, 1, tt) { solve(i); } return 0; } |
English