//
#ifndef __SIZEOF_INT128__
#define __SIZEOF_INT128__
#endif
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;
template <typename T> using oset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define rep(i, p, k) for(int i(p); i < (k); ++i)
#define per(i, p, k) for(int i(p); i > (k); --i)
#define sz(x) (int)(x).size()
#define sc static_cast
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef __int128_t lll;
//#define int ll
template <typename T = int> using par = std::pair <T, T>;
#define fi first
#define se second
#define test int _number_of_tests(in()); while(_number_of_tests--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb emplace_back
struct Timer {
string name{""};
time_point<high_resolution_clock> end, start{high_resolution_clock::now()};
duration<float, std::milli> dur;
Timer() = default;
Timer(string nm): name(nm) {}
~Timer() {
end = high_resolution_clock::now(); dur= end - start;
cout << "@" << name << "> " << dur.count() << " ms" << '\n';
}
};
template <typename T = int> inline T in()
{
static T x;
std::cin >> x;
return x;
}
std::string yn(bool b)
{
if(b) return "YES\n";
else return "NO\n";
}
template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par);
template <typename T> std::ostream& operator<< (std::ostream& out, const std::vector <T>& wek)
{
for(const auto& i : wek)out << i << ' ';
return out;
}
template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par)
{
out << '{'<<par.first<<", "<<par.second<<"}";
return out;
}
#define show(x) cerr << #x << " = " << x << '\n';
void solv1(vector <vector <bool>> ar){
int n(sz(ar)), l(sz(ar[0]));
int c(1);
rep(i, 1, n+1) c = lcm(c, i);
vector <int> pil(l*c);
rep(i, 0, n)rep(j, 0, l)rep(k, 0, c)pil[j*c+k] += !ar[i][j];
int p(0), k(l*c);
while(k > p){
int S((p+k+1)/2);
vector <int> pr(n);
iota(all(pr), 0);
bool kt = 0;
vector <vector <int>> mo(n);
rep(i, 0, n){
int k(-1), s(0);
rep(p, 0, l*c){
while(k - p+1 < S){
++k;
if(k == l*c)goto end;
s += ar[i][k/c];
}
if(!s)mo[i].pb(p);
s -= ar[i][p/c];
}
end:;
}
do{
vector <int> v(pil);
set <int> zl;
rep(i, 0, l*c)if(v[i] < 2)zl.insert(i);
for(auto i: pr){
for(auto j: mo[i]){
auto ptr(zl.lower_bound(j));
if(ptr == zl.end() || *ptr >= j+S){
rep(k, j, j+S) if(--v[k] < 2)zl.insert(k);
goto ok;
}
}
goto nie;
ok:;
}
kt = 1;
break;
nie:;
}while(next_permutation(all(pr)));
if(kt)p = S;
else k = S-1;
}
int g(gcd(p,c));
p /= g;
c /= g;
cout << p << '/' << c << '\n';
}
void solv2(vector <vector <bool>> ar){
int n(sz(ar)), l(sz(ar[0]));
vector <int> pr{0,0};
int d(0);
rep(i, 0, l+1){
if(i == l || ar[0][i] || ar[1][i]){
if(d)pr.pb(d);
d = 0;
}
else ++d;
}
sort(rall(pr));
if(pr[0] > pr[1]*2){
if(pr[0] % 2)cout << pr[0] << '/' << 2 << '\n';
else cout << pr[0]/2 << '/' << 1 << '\n';
}
else cout << pr[1] << '/' << 1 << '\n';
}
std::int32_t main()
{
std::cout.tie(nullptr); //for luck
std::cin.tie(nullptr); std::ios_base::sync_with_stdio(0);
int n(in()), l(in());
vector <vector <bool>> ar(n, vector <bool>(l));
rep(i, 0, n)rep(j, 0, l)ar[i][j] = in<char>() == 'X';
rep(j, 0, l){
bool b(1);
rep(i, 0, n)b &= ar[i][j];
if(b){
cout << "-1\n";
return 0;
}
}
if(n == 1)cout << "0/1\n";
else if(n == 2)solv2(ar);
else solv1(ar);
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 | // #ifndef __SIZEOF_INT128__ #define __SIZEOF_INT128__ #endif #pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace chrono; using namespace __gnu_pbds; template <typename T> using oset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>; #define rep(i, p, k) for(int i(p); i < (k); ++i) #define per(i, p, k) for(int i(p); i > (k); --i) #define sz(x) (int)(x).size() #define sc static_cast typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef unsigned long long ull; typedef __int128_t lll; //#define int ll template <typename T = int> using par = std::pair <T, T>; #define fi first #define se second #define test int _number_of_tests(in()); while(_number_of_tests--) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define pb emplace_back struct Timer { string name{""}; time_point<high_resolution_clock> end, start{high_resolution_clock::now()}; duration<float, std::milli> dur; Timer() = default; Timer(string nm): name(nm) {} ~Timer() { end = high_resolution_clock::now(); dur= end - start; cout << "@" << name << "> " << dur.count() << " ms" << '\n'; } }; template <typename T = int> inline T in() { static T x; std::cin >> x; return x; } std::string yn(bool b) { if(b) return "YES\n"; else return "NO\n"; } template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par); template <typename T> std::ostream& operator<< (std::ostream& out, const std::vector <T>& wek) { for(const auto& i : wek)out << i << ' '; return out; } template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par) { out << '{'<<par.first<<", "<<par.second<<"}"; return out; } #define show(x) cerr << #x << " = " << x << '\n'; void solv1(vector <vector <bool>> ar){ int n(sz(ar)), l(sz(ar[0])); int c(1); rep(i, 1, n+1) c = lcm(c, i); vector <int> pil(l*c); rep(i, 0, n)rep(j, 0, l)rep(k, 0, c)pil[j*c+k] += !ar[i][j]; int p(0), k(l*c); while(k > p){ int S((p+k+1)/2); vector <int> pr(n); iota(all(pr), 0); bool kt = 0; vector <vector <int>> mo(n); rep(i, 0, n){ int k(-1), s(0); rep(p, 0, l*c){ while(k - p+1 < S){ ++k; if(k == l*c)goto end; s += ar[i][k/c]; } if(!s)mo[i].pb(p); s -= ar[i][p/c]; } end:; } do{ vector <int> v(pil); set <int> zl; rep(i, 0, l*c)if(v[i] < 2)zl.insert(i); for(auto i: pr){ for(auto j: mo[i]){ auto ptr(zl.lower_bound(j)); if(ptr == zl.end() || *ptr >= j+S){ rep(k, j, j+S) if(--v[k] < 2)zl.insert(k); goto ok; } } goto nie; ok:; } kt = 1; break; nie:; }while(next_permutation(all(pr))); if(kt)p = S; else k = S-1; } int g(gcd(p,c)); p /= g; c /= g; cout << p << '/' << c << '\n'; } void solv2(vector <vector <bool>> ar){ int n(sz(ar)), l(sz(ar[0])); vector <int> pr{0,0}; int d(0); rep(i, 0, l+1){ if(i == l || ar[0][i] || ar[1][i]){ if(d)pr.pb(d); d = 0; } else ++d; } sort(rall(pr)); if(pr[0] > pr[1]*2){ if(pr[0] % 2)cout << pr[0] << '/' << 2 << '\n'; else cout << pr[0]/2 << '/' << 1 << '\n'; } else cout << pr[1] << '/' << 1 << '\n'; } std::int32_t main() { std::cout.tie(nullptr); //for luck std::cin.tie(nullptr); std::ios_base::sync_with_stdio(0); int n(in()), l(in()); vector <vector <bool>> ar(n, vector <bool>(l)); rep(i, 0, n)rep(j, 0, l)ar[i][j] = in<char>() == 'X'; rep(j, 0, l){ bool b(1); rep(i, 0, n)b &= ar[i][j]; if(b){ cout << "-1\n"; return 0; } } if(n == 1)cout << "0/1\n"; else if(n == 2)solv2(ar); else solv1(ar); return 0; } |
English