#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define int long long
#define st first
#define nd second
#define rd third
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define RE(i, n) FOR(i, 1, n)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define __builtin_ctz __builtin_ctzll
#define __builtin_clz __builtin_clzll
#define __builtin_popcount __builtin_popcountll
using namespace std;
template<class T1, class T2>
ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class A, class B, class C> struct Triple { A first; B second; C third;
bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } };
template<class T> void ResizeVec(T&, vector<int>) {}
template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
for (T& v : vec) { ResizeVec(v, sz); }
}
typedef Triple<int, int, int> TIII;
template<class A, class B, class C>
ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; }
template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif
typedef long double LD;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<pair<int, int> > VPII;
template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//mt19937 rng(57);
#define rand fgfdgfdgfd
struct Frac {
int num, den;
Frac(int a, int b) {
//assert(__gcd(a, b) == 1);
int g = __gcd(a, b);
a /= g;
b /= g;
num = a;
den = b;
}
bool operator<(const Frac& oth) const {
return num * oth.den < oth.num * den;
}
friend ostream& operator<<(ostream& out, Frac f) {
return out<<f.num<<"/"<<f.den;
}
};
struct Node {
int32_t rightmost_zero;
int32_t cnt;
};
const int maxM = 1 << 21;
Node drz[2 * maxM + 5];
int M;
void Rigcz(int v) {
if (drz[v].cnt) {
drz[v].rightmost_zero = 0;
return;
}
if (v >= M) {
drz[v].rightmost_zero = v - M + 1;
return;
}
drz[v].rightmost_zero = max(drz[2 * v].rightmost_zero, drz[2 * v + 1].rightmost_zero);
}
// Node Merge(Node L, Node R) {
// Node n;
// if (R.rightmost_zero == 0) {
// n.rightmost_zero = L.rightmost_zero;
// } else {
// n.rightmost_zero = R.rightmost_zero;
// }
// return n;
// }
int32_t RightMostZeroRec(int32_t bl, int32_t br, int32_t l, int32_t r, int32_t v) {
if (bl > r || br < l || drz[v].rightmost_zero == 0) { return 0; }
if (l <= bl && br <= r) {
return drz[v].rightmost_zero;
}
int32_t m = (bl + br) / 2;
int32_t rec = RightMostZeroRec(m + 1, br, l, r, 2 * v + 1);
if (rec) { return rec; }
return RightMostZeroRec(bl, m, l, r, 2 * v);
}
void AddRec(int32_t bl, int32_t br, int32_t l, int32_t r, int32_t v, int32_t to_add) {
if (bl > r || br < l) { return; }
if (l <= bl && br <= r) {
drz[v].cnt += to_add;
Rigcz(v);
return;
}
int32_t m = (bl + br) / 2;
AddRec(bl, m, l, r, 2 * v, to_add);
AddRec(m + 1, br, l, r, 2 * v + 1, to_add);
Rigcz(v);
}
void Add(int32_t l, int32_t r, int32_t to_add) {
if (l > r) { return; }
//debug(l, r, to_add);
AddRec(1, M, l, r, 1, to_add);
}
int32_t RightMostZero(int32_t l, int32_t r) {
if (l > r) { return 0; }
return RightMostZeroRec(1, M, l, r, 1);
}
struct IntvInfo {
int32_t L, R, ind;
bool operator<(const IntvInfo& oth) const {
return L < oth.L;
}
friend ostream& operator<<(ostream& out, IntvInfo i) {
return out<<"(["<<i.L<<", "<<i.R<<"), "<<i.ind<<")";
}
};
struct Sol {
void Test(int tescior) {
int n, L;
cin>>n>>L;
vector<Frac> fracs;
int nodes = 0;
vector<vector<char>> orig_board(n + 2, vector<char>(L + 2));
RE (i, n) {
RE (j, L) {
cin>>orig_board[i][j];
}
}
VI orig_cnt(L + 2);
RE (j, L) {
RE (i, n) {
if (orig_board[i][j] == '.') {
orig_cnt[j]++;
}
}
if (orig_cnt[j] == 0) {
cout<<"-1\n";
return;
}
if (orig_cnt[j] == 1) {
RE (i, n) {
orig_board[i][j] = 'X';
}
}
}
vector<VPII> orig_intvs(n + 2);
RE (i, n) {
int beg = -1;
RE (j, L) {
if (orig_board[i][j] == '.') {
if (beg == -1) {
beg = j;
}
if (j == L || orig_board[i][j + 1] == 'X') {
orig_intvs[i].PB({beg, j + 1});
}
} else {
beg = -1;
}
}
}
function<bool(Frac)> Check = [&](Frac f) {
//debug("??????????????", f);
int db = 0;//(f.num == 7 && f.den == 2);
//debug(f.num, f.den);
vector<vector<char>> board(n + 2);
int W = L * f.den;
vector<int> cnt(W + 2);
M = 1;
while (M <= W + 2) {
M *= 2;
}
function<void()> DebugTree = [&]() {
RE (i, 2 * M - 1) {
if (i > 1 && (i & (i - 1)) == 0) {
cerr<<endl;
}
cerr<<"("<<drz[i].cnt<<", "<<drz[i].rightmost_zero<<") ";
}
cerr<<endl;
};
REP (i, 2 * M + 2) {
drz[i].cnt = drz[i].rightmost_zero = 0;
}
// RE (i, n) {
// board[i].resize(W + 2);
// RE (j, W) {
// board[i][j] = orig_board[i][(j - 1) / f.den + 1];
// if (board[i][j] == '.') {
// orig_cnt[j]++;
// }
// }
// }
vector<vector<pair<int32_t, int32_t>>> intvs(n + 1);
vector<int> max_disjoint(n + 1);
VI in(n + 1);
RE (i, n) {
for (auto intv : orig_intvs[i]) {
intv.st = (intv.st - 1) * f.den + 1;
intv.nd = (intv.nd - 1) * f.den + 1;
int len = intv.nd - intv.st;
if (len < f.num) {
cnt[intv.nd]--;
cnt[intv.st]++;
} else {
max_disjoint[i] += len / f.num;
intvs[i].PB({intv.st, intv.nd});
}
}
if (max_disjoint[i] >= 2 * n - 1) {
in[i] = 1;
for (auto intv : intvs[i]) {
cnt[intv.st]++;
cnt[intv.nd]--;
}
}
}
RE (i, W) {
cnt[i] += cnt[i - 1];
if (cnt[i]) {
drz[i + M - 1].cnt = 1;
} else {
drz[i + M - 1].rightmost_zero = i;
}
}
FORD (i, M - 1, 1) {
Rigcz(i);
}
int not_in_cnt = 0;
function<void(int32_t, int32_t, int32_t)> WlozLubWyloz = [&](int32_t who, int32_t where, int32_t to_add) {
in[who] += to_add;
int32_t fir_free = where + f.num;
for (auto intv : intvs[who]) {
int32_t b = max(fir_free, intv.st);
if (b < intv.nd) {
Add(b, intv.nd - 1, to_add);
}
}
not_in_cnt -= to_add;
};
vector<IntvInfo> all_intvs;
RE (i, n) {
if (!in[i]) {
not_in_cnt++;
for (auto intv : intvs[i]) {
all_intvs.PB({intv.st, intv.nd, i});
}
}
}
if (not_in_cnt == 0) { return true; }
if (all_intvs.empty()) { return false; }
sort(ALL(all_intvs));
// int dep = 0;
// vector<vector<int32_t>> max_disj_suf(n + 2, vector<int32_t>(SZ(all_intvs) + 1));
// FORD (j, SZ(all_intvs) - 1, 0) {
// RE (i, n) {
// max_disj_suf[i][j] = max_disj_suf[i][j + 1];
// }
// max_disj_suf[all_intvs[j].ind][j] += (all_intvs[j].R - all_intvs[j].L) / f.num;
// }
function<bool(int32_t, int32_t)> Rec = [&](int32_t intv_ind, int32_t x) {
nodes++;
if (intv_ind == SZ(all_intvs)) { return false; }
maxi(x, all_intvs[intv_ind].L);
// if (not_in_cnt >= 1) {
// RE (i, n) {
// if (!in[i] && 2 * not_in_cnt - 1 <= max_disj_suf[i][intv_ind + 1]) {
// if (not_in_cnt == 1) { return true; }
// WlozLubWyloz(i, -W, 1);
// bool rec = Rec(intv_ind, x);
// WlozLubWyloz(i, -W, -1);
// return rec;
// }
// }
// }
IntvInfo me_info = all_intvs[intv_ind];
if (in[me_info.ind] || x >= me_info.R) { return Rec(intv_ind + 1, x); }
if (x > W - f.num + 1) { return false; }
int32_t r = x + f.num - 1;
int32_t nxt_intv = intv_ind + 1;
while (nxt_intv < SZ(all_intvs) && in[all_intvs[nxt_intv].ind]) {
nxt_intv++;
}
if (r >= me_info.R) {
if (nxt_intv == SZ(all_intvs)) { return false; }
Add(x, me_info.R - 1, 1);
bool rec = Rec(nxt_intv, max(x, all_intvs[nxt_intv].L));
Add(x, me_info.R - 1, -1);
return rec;
}
if (nxt_intv < SZ(all_intvs)) {
if (x <= all_intvs[nxt_intv].L) {
mini(r, all_intvs[nxt_intv].L - 1);
}
}
int32_t wh_zero = RightMostZero(x, r);
if (wh_zero) {
return Rec(intv_ind, wh_zero + 1);
}
if (r == x + f.num - 1) {
WlozLubWyloz(me_info.ind, x, 1);
if (not_in_cnt == 0) {
return true;
}
if (Rec(nxt_intv, x)) { return true; }
WlozLubWyloz(me_info.ind, x, -1);
return false;
}
IntvInfo nxt_info = all_intvs[nxt_intv];
WlozLubWyloz(me_info.ind, x, 1);
if (Rec(nxt_intv, nxt_info.L)) { return true; }
WlozLubWyloz(me_info.ind, x, -1);
WlozLubWyloz(nxt_info.ind, nxt_info.L, 1);
if (Rec(intv_ind, nxt_info.L)) { return true; }
WlozLubWyloz(nxt_info.ind, nxt_info.L, -1);
return false;
};
return Rec(0, 0);
};
int kl = 1, kp = L - 1, faj = 0;
while (kl <= kp) {
int aktc = (kl + kp) / 2;
if (Check(Frac(aktc, 1))) {
kl = aktc + 1;
faj = aktc;
} else {
kp = aktc - 1;
}
}
//debug(faj);
// Frac lb(faj, 1);
// Frac ub(faj + 1, 1);
int po = 2;
int nummm = faj;
while (po <= n) {
Frac mb(2 * nummm + 1, po);
if (Check(mb)) {
nummm = 2 * nummm + 1;
} else {
nummm = 2 * nummm;
}
po *= 2;
}
po /= 2;
Frac lb(nummm, po);
Frac ub(nummm + 1, po);
//debug(lb, ub);
RE (den, n) {
int start = lb.num * den / lb.den + 1;
assert(lb < Frac(start, den) && !(lb < Frac(start - 1, den)));
for (int c = start; Frac(c, den) < ub; c++) {
if (Check(Frac(c, den))) {
lb = Frac(c, den);
} else {
ub = Frac(c, den);
}
}
}
cout<<lb.num<<"/"<<lb.den<<"\n";
debug(nodes, n, (1 << n) * n);
}
};
int32_t main() {
ios_base::sync_with_stdio(0);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
cin.tie(0);
//double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC;
int t = 1;
#ifdef LOCAL
cin>>t;
#endif
RE (i, t) {
if (i % 100 == 0) {
debug(i);
}
Sol sol;
sol.Test(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 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 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 | #include <bits/stdc++.h> #define MP make_pair #define PB push_back #define int long long #define st first #define nd second #define rd third #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define RE(i, n) FOR(i, 1, n) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define REP(i, n) for(int i = 0;i <(n); ++i) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define __builtin_ctz __builtin_ctzll #define __builtin_clz __builtin_clzll #define __builtin_popcount __builtin_popcountll using namespace std; template<class T1, class T2> ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";} template<class A, class B, class C> struct Triple { A first; B second; C third; bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } }; template<class T> void ResizeVec(T&, vector<int>) {} template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) { vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; } for (T& v : vec) { ResizeVec(v, sz); } } typedef Triple<int, int, int> TIII; template<class A, class B, class C> ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; } template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; } template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; } template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; } template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; } template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) { while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...); } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif typedef long double LD; typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<pair<int, int> > VPII; template<class C> void mini(C&a4, C b4){a4=min(a4, b4); } template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //mt19937 rng(57); #define rand fgfdgfdgfd struct Frac { int num, den; Frac(int a, int b) { //assert(__gcd(a, b) == 1); int g = __gcd(a, b); a /= g; b /= g; num = a; den = b; } bool operator<(const Frac& oth) const { return num * oth.den < oth.num * den; } friend ostream& operator<<(ostream& out, Frac f) { return out<<f.num<<"/"<<f.den; } }; struct Node { int32_t rightmost_zero; int32_t cnt; }; const int maxM = 1 << 21; Node drz[2 * maxM + 5]; int M; void Rigcz(int v) { if (drz[v].cnt) { drz[v].rightmost_zero = 0; return; } if (v >= M) { drz[v].rightmost_zero = v - M + 1; return; } drz[v].rightmost_zero = max(drz[2 * v].rightmost_zero, drz[2 * v + 1].rightmost_zero); } // Node Merge(Node L, Node R) { // Node n; // if (R.rightmost_zero == 0) { // n.rightmost_zero = L.rightmost_zero; // } else { // n.rightmost_zero = R.rightmost_zero; // } // return n; // } int32_t RightMostZeroRec(int32_t bl, int32_t br, int32_t l, int32_t r, int32_t v) { if (bl > r || br < l || drz[v].rightmost_zero == 0) { return 0; } if (l <= bl && br <= r) { return drz[v].rightmost_zero; } int32_t m = (bl + br) / 2; int32_t rec = RightMostZeroRec(m + 1, br, l, r, 2 * v + 1); if (rec) { return rec; } return RightMostZeroRec(bl, m, l, r, 2 * v); } void AddRec(int32_t bl, int32_t br, int32_t l, int32_t r, int32_t v, int32_t to_add) { if (bl > r || br < l) { return; } if (l <= bl && br <= r) { drz[v].cnt += to_add; Rigcz(v); return; } int32_t m = (bl + br) / 2; AddRec(bl, m, l, r, 2 * v, to_add); AddRec(m + 1, br, l, r, 2 * v + 1, to_add); Rigcz(v); } void Add(int32_t l, int32_t r, int32_t to_add) { if (l > r) { return; } //debug(l, r, to_add); AddRec(1, M, l, r, 1, to_add); } int32_t RightMostZero(int32_t l, int32_t r) { if (l > r) { return 0; } return RightMostZeroRec(1, M, l, r, 1); } struct IntvInfo { int32_t L, R, ind; bool operator<(const IntvInfo& oth) const { return L < oth.L; } friend ostream& operator<<(ostream& out, IntvInfo i) { return out<<"(["<<i.L<<", "<<i.R<<"), "<<i.ind<<")"; } }; struct Sol { void Test(int tescior) { int n, L; cin>>n>>L; vector<Frac> fracs; int nodes = 0; vector<vector<char>> orig_board(n + 2, vector<char>(L + 2)); RE (i, n) { RE (j, L) { cin>>orig_board[i][j]; } } VI orig_cnt(L + 2); RE (j, L) { RE (i, n) { if (orig_board[i][j] == '.') { orig_cnt[j]++; } } if (orig_cnt[j] == 0) { cout<<"-1\n"; return; } if (orig_cnt[j] == 1) { RE (i, n) { orig_board[i][j] = 'X'; } } } vector<VPII> orig_intvs(n + 2); RE (i, n) { int beg = -1; RE (j, L) { if (orig_board[i][j] == '.') { if (beg == -1) { beg = j; } if (j == L || orig_board[i][j + 1] == 'X') { orig_intvs[i].PB({beg, j + 1}); } } else { beg = -1; } } } function<bool(Frac)> Check = [&](Frac f) { //debug("??????????????", f); int db = 0;//(f.num == 7 && f.den == 2); //debug(f.num, f.den); vector<vector<char>> board(n + 2); int W = L * f.den; vector<int> cnt(W + 2); M = 1; while (M <= W + 2) { M *= 2; } function<void()> DebugTree = [&]() { RE (i, 2 * M - 1) { if (i > 1 && (i & (i - 1)) == 0) { cerr<<endl; } cerr<<"("<<drz[i].cnt<<", "<<drz[i].rightmost_zero<<") "; } cerr<<endl; }; REP (i, 2 * M + 2) { drz[i].cnt = drz[i].rightmost_zero = 0; } // RE (i, n) { // board[i].resize(W + 2); // RE (j, W) { // board[i][j] = orig_board[i][(j - 1) / f.den + 1]; // if (board[i][j] == '.') { // orig_cnt[j]++; // } // } // } vector<vector<pair<int32_t, int32_t>>> intvs(n + 1); vector<int> max_disjoint(n + 1); VI in(n + 1); RE (i, n) { for (auto intv : orig_intvs[i]) { intv.st = (intv.st - 1) * f.den + 1; intv.nd = (intv.nd - 1) * f.den + 1; int len = intv.nd - intv.st; if (len < f.num) { cnt[intv.nd]--; cnt[intv.st]++; } else { max_disjoint[i] += len / f.num; intvs[i].PB({intv.st, intv.nd}); } } if (max_disjoint[i] >= 2 * n - 1) { in[i] = 1; for (auto intv : intvs[i]) { cnt[intv.st]++; cnt[intv.nd]--; } } } RE (i, W) { cnt[i] += cnt[i - 1]; if (cnt[i]) { drz[i + M - 1].cnt = 1; } else { drz[i + M - 1].rightmost_zero = i; } } FORD (i, M - 1, 1) { Rigcz(i); } int not_in_cnt = 0; function<void(int32_t, int32_t, int32_t)> WlozLubWyloz = [&](int32_t who, int32_t where, int32_t to_add) { in[who] += to_add; int32_t fir_free = where + f.num; for (auto intv : intvs[who]) { int32_t b = max(fir_free, intv.st); if (b < intv.nd) { Add(b, intv.nd - 1, to_add); } } not_in_cnt -= to_add; }; vector<IntvInfo> all_intvs; RE (i, n) { if (!in[i]) { not_in_cnt++; for (auto intv : intvs[i]) { all_intvs.PB({intv.st, intv.nd, i}); } } } if (not_in_cnt == 0) { return true; } if (all_intvs.empty()) { return false; } sort(ALL(all_intvs)); // int dep = 0; // vector<vector<int32_t>> max_disj_suf(n + 2, vector<int32_t>(SZ(all_intvs) + 1)); // FORD (j, SZ(all_intvs) - 1, 0) { // RE (i, n) { // max_disj_suf[i][j] = max_disj_suf[i][j + 1]; // } // max_disj_suf[all_intvs[j].ind][j] += (all_intvs[j].R - all_intvs[j].L) / f.num; // } function<bool(int32_t, int32_t)> Rec = [&](int32_t intv_ind, int32_t x) { nodes++; if (intv_ind == SZ(all_intvs)) { return false; } maxi(x, all_intvs[intv_ind].L); // if (not_in_cnt >= 1) { // RE (i, n) { // if (!in[i] && 2 * not_in_cnt - 1 <= max_disj_suf[i][intv_ind + 1]) { // if (not_in_cnt == 1) { return true; } // WlozLubWyloz(i, -W, 1); // bool rec = Rec(intv_ind, x); // WlozLubWyloz(i, -W, -1); // return rec; // } // } // } IntvInfo me_info = all_intvs[intv_ind]; if (in[me_info.ind] || x >= me_info.R) { return Rec(intv_ind + 1, x); } if (x > W - f.num + 1) { return false; } int32_t r = x + f.num - 1; int32_t nxt_intv = intv_ind + 1; while (nxt_intv < SZ(all_intvs) && in[all_intvs[nxt_intv].ind]) { nxt_intv++; } if (r >= me_info.R) { if (nxt_intv == SZ(all_intvs)) { return false; } Add(x, me_info.R - 1, 1); bool rec = Rec(nxt_intv, max(x, all_intvs[nxt_intv].L)); Add(x, me_info.R - 1, -1); return rec; } if (nxt_intv < SZ(all_intvs)) { if (x <= all_intvs[nxt_intv].L) { mini(r, all_intvs[nxt_intv].L - 1); } } int32_t wh_zero = RightMostZero(x, r); if (wh_zero) { return Rec(intv_ind, wh_zero + 1); } if (r == x + f.num - 1) { WlozLubWyloz(me_info.ind, x, 1); if (not_in_cnt == 0) { return true; } if (Rec(nxt_intv, x)) { return true; } WlozLubWyloz(me_info.ind, x, -1); return false; } IntvInfo nxt_info = all_intvs[nxt_intv]; WlozLubWyloz(me_info.ind, x, 1); if (Rec(nxt_intv, nxt_info.L)) { return true; } WlozLubWyloz(me_info.ind, x, -1); WlozLubWyloz(nxt_info.ind, nxt_info.L, 1); if (Rec(intv_ind, nxt_info.L)) { return true; } WlozLubWyloz(nxt_info.ind, nxt_info.L, -1); return false; }; return Rec(0, 0); }; int kl = 1, kp = L - 1, faj = 0; while (kl <= kp) { int aktc = (kl + kp) / 2; if (Check(Frac(aktc, 1))) { kl = aktc + 1; faj = aktc; } else { kp = aktc - 1; } } //debug(faj); // Frac lb(faj, 1); // Frac ub(faj + 1, 1); int po = 2; int nummm = faj; while (po <= n) { Frac mb(2 * nummm + 1, po); if (Check(mb)) { nummm = 2 * nummm + 1; } else { nummm = 2 * nummm; } po *= 2; } po /= 2; Frac lb(nummm, po); Frac ub(nummm + 1, po); //debug(lb, ub); RE (den, n) { int start = lb.num * den / lb.den + 1; assert(lb < Frac(start, den) && !(lb < Frac(start - 1, den))); for (int c = start; Frac(c, den) < ub; c++) { if (Check(Frac(c, den))) { lb = Frac(c, den); } else { ub = Frac(c, den); } } } cout<<lb.num<<"/"<<lb.den<<"\n"; debug(nodes, n, (1 << n) * n); } }; int32_t main() { ios_base::sync_with_stdio(0); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); cin.tie(0); //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC; int t = 1; #ifdef LOCAL cin>>t; #endif RE (i, t) { if (i % 100 == 0) { debug(i); } Sol sol; sol.Test(i); } return 0; } |
English