#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define FORD(i,a,b) for(int i=a;i>=b;--i)
#define PB push_back
#define EB emplace_back
#define FI first
#define SE second
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define vvi vector<vi>
#define vll vector<ll>
#define vvll vector<vll>
#define vpii vector<pii>
#define pii pair<ll, ll>
#define pll pair<ll, ll>
#define ALL(X) (X).begin(),(X).end()
#ifndef DEBUG
//#define endl (char)10
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <class T>
ostream& operator<< (ostream& os, vector<T>& vec){
for(auto& t : vec) os << t << " ";
return os;
}
template<class T, class U>
ostream& operator<< (ostream& os, const pair<T, U>& p){
os << p.FI << " " << p.SE;
return os;
}
template<class T, class U>
istream& operator>> (istream& is, pair<T, U>& p){
is >> p.FI >> p.SE;
return is;
}
template <class T>
istream& operator>> (istream& is, vector<T>& vec){
for(auto& p : vec) is >> p;
return is;
}
/*
3 6
..X.XX
.X..X.
X..X..
*/
bool canrek(vector<string>& V, vi& wol, int r, int x, vi& pom, int idx) {
if(idx == V.size()) return true;
int m = V[0].size() * r;
FOR(i,0,m-x+1) {
bool ok = true;
FOR(j,0,x) if (V[idx][(i + j) / r] == 'X' || wol[(i + j) / r] == pom[i + j] + 1) {
i += j;
ok = false;
break;
}
if(!ok) continue;
FOR(j,0,x) pom[i + j]++;
if (canrek(V, wol, r, x, pom, idx + 1)) return true;
FOR(j,0,x) pom[i + j]--;
}
return false;
}
bool can(vector<string>& V, vi& wol, int r, int x) {
vi pom(V[0].size() * r, 0);
return canrek(V, wol, r, x, pom, 0);
}
int solve(vector<string>& V, vi& wol, int r) {
int b = 1, e = (V[0].size() * r / 2) + 2;
while(b < e) {
int x = (b + e) / 2;
if(can(V, wol, r, x)) b = x + 1;
else e = x;
}
return b - 1;
}
void st() {
int n, m;
cin >> n >> m;
vector<string> V(n);
cin >> V;
vi wol(m, 0);
FOR(i,0,m) {
FOR(j,0,n) if (V[j][i] != 'X') wol[i]++;
if(wol[i] == 0) {
cout << -1 << endl;
return;
}
}
if(n == 1) {
cout << "0/1" << endl;
return;
}
int bl = 0, bm = 1;
FOR(i,1+n/2,n+1){
int c = solve(V, wol, i);
if (c * bm > bl * i) {
bl = c;
bm = i;
}
}
int d = gcd(bl, bm);
bl /= d;
bm /= d;
cout << bl << "/" << bm << endl;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
//cin >> t;
FOR(tid,0,t){
//if(tid % 100000 == 0) cerr << tid << "/" << t << " = " << (double)(tid) / t * 100.0 << endl;
st();
}
}
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> #define FOR(i,a,b) for(int i=a;i<b;++i) #define FORD(i,a,b) for(int i=a;i>=b;--i) #define PB push_back #define EB emplace_back #define FI first #define SE second #define umap unordered_map #define uset unordered_set #define vi vector<int> #define vvi vector<vi> #define vll vector<ll> #define vvll vector<vll> #define vpii vector<pii> #define pii pair<ll, ll> #define pll pair<ll, ll> #define ALL(X) (X).begin(),(X).end() #ifndef DEBUG //#define endl (char)10 #endif using namespace std; using ll = long long; using ld = long double; template <class T> ostream& operator<< (ostream& os, vector<T>& vec){ for(auto& t : vec) os << t << " "; return os; } template<class T, class U> ostream& operator<< (ostream& os, const pair<T, U>& p){ os << p.FI << " " << p.SE; return os; } template<class T, class U> istream& operator>> (istream& is, pair<T, U>& p){ is >> p.FI >> p.SE; return is; } template <class T> istream& operator>> (istream& is, vector<T>& vec){ for(auto& p : vec) is >> p; return is; } /* 3 6 ..X.XX .X..X. X..X.. */ bool canrek(vector<string>& V, vi& wol, int r, int x, vi& pom, int idx) { if(idx == V.size()) return true; int m = V[0].size() * r; FOR(i,0,m-x+1) { bool ok = true; FOR(j,0,x) if (V[idx][(i + j) / r] == 'X' || wol[(i + j) / r] == pom[i + j] + 1) { i += j; ok = false; break; } if(!ok) continue; FOR(j,0,x) pom[i + j]++; if (canrek(V, wol, r, x, pom, idx + 1)) return true; FOR(j,0,x) pom[i + j]--; } return false; } bool can(vector<string>& V, vi& wol, int r, int x) { vi pom(V[0].size() * r, 0); return canrek(V, wol, r, x, pom, 0); } int solve(vector<string>& V, vi& wol, int r) { int b = 1, e = (V[0].size() * r / 2) + 2; while(b < e) { int x = (b + e) / 2; if(can(V, wol, r, x)) b = x + 1; else e = x; } return b - 1; } void st() { int n, m; cin >> n >> m; vector<string> V(n); cin >> V; vi wol(m, 0); FOR(i,0,m) { FOR(j,0,n) if (V[j][i] != 'X') wol[i]++; if(wol[i] == 0) { cout << -1 << endl; return; } } if(n == 1) { cout << "0/1" << endl; return; } int bl = 0, bm = 1; FOR(i,1+n/2,n+1){ int c = solve(V, wol, i); if (c * bm > bl * i) { bl = c; bm = i; } } int d = gcd(bl, bm); bl /= d; bm /= d; cout << bl << "/" << bm << endl; } int main () { ios_base::sync_with_stdio(false); cin.tie(0); int t = 1; //cin >> t; FOR(tid,0,t){ //if(tid % 100000 == 0) cerr << tid << "/" << t << " = " << (double)(tid) / t * 100.0 << endl; st(); } } |
English