This documentation is automatically generated by online-judge-tools/verification-helper
#include "other/connected_dp.hpp"
#include "ds/hashmap.hpp"
#include "random/hash_vector.hpp"
namespace connected_dp_squares {
// pair<新しい状態、今の成分 → 新しい成分>
vc<pair<vc<int>, vc<int>>> next_states(const vc<int>& now) {
int N = len(now);
vc<pair<vc<int>, vc<int>>> res;
FOR(s, 1 << N) {
vc<int> par(N + N);
FOR(i, N) par[i] = (s & 1 << i ? i : -1);
FOR(i, N) par[N + i] = (now[i] == -1 ? -1 : now[i] + N);
auto find = [&](int x) -> int {
while (par[x] != x) { x = par[x] = par[par[x]]; }
return x;
};
auto merge = [&](int a, int b) -> void {
a = find(a), b = find(b);
if (a == b) return;
if (a > b) swap(a, b);
par[b] = a;
};
FOR(i, N - 1) if (par[i] != -1 && par[i + 1] != -1) merge(i, i + 1);
FOR(i, N) if (par[i] != -1 && par[N + i] != -1) merge(i, N + i);
FOR(i, N + N) if (par[i] != -1) par[i] = find(i);
FOR(i, N, N + N) if (par[i] >= N) par[i] = -1;
res.eb(vc<int>(par.begin(), par.begin() + N),
vc<int>(par.begin() + N, par.end()));
}
return res;
}
vc<int> reverse_state(const vc<int>& now) {
int N = len(now);
vc<int> max_i(N, -1);
FOR(i, N) if (now[i] != -1) max_i[now[i]] = i;
vc<int> rev(N, -1);
FOR(i, N) {
if (now[i] == -1) continue;
int x = max_i[now[i]];
rev[N - 1 - i] = N - 1 - x;
}
return rev;
}
// 0, 1 :空の列、領域の手前、後ろ
// 連結領域をひとつ作る。
// 状態:-1 が選んでいない。0,1,2,3 等は同じ成分には同じ値が入る。
// [states, edges]
pair<vvc<int>, vc<pair<int, int>>> connedted_dp_graph(int N,
bool merge_reverse) {
static HashMap<int, 20, true> MP;
MP.reset();
vvc<int> states;
vc<pair<int, int>> edges;
states.eb(vc<int>(N, -1));
states.eb(vc<int>(N, -1));
MP[hash_vector<int>(states[0])] = 0;
int p = -1;
while (1) {
if (++p == len(states)) break;
if (p == 1) {
edges.eb(1, 1);
continue;
}
vc<int> now = states[p];
for (auto&& [nxt, convert]: next_states(now)) {
// 今の成分数、消える成分数
int a = 0, b = 0;
FOR(v, N) if (now[v] == v) {
++a;
if (convert[v] == -1) ++b;
}
// 消える成分があってよいのは、終状態にいくときのみ
if (b >= 2) continue;
if (b == 1) {
if (MAX(nxt) != -1) continue;
edges.eb(p, 1);
continue;
}
u64 h = hash_vector<int>(nxt);
if (merge_reverse) { chmin(h, hash_vector<int>(reverse_state(nxt))); }
if (!MP.count(h)) { MP[h] = len(states), states.eb(nxt); }
edges.eb(p, MP[h]);
}
}
return {states, edges};
}
// 0, 1 :空の列、領域の手前、後ろ
// 多角形(空洞なし)をひとつ作る。
// 状態:-1 が選んでいない。0,1,2,3 等は同じ成分には同じ値が入る。
// [states, edges]
pair<vvc<int>, vc<pair<int, int>>> polygon_dp_graph(int N) {
static HashMap<int, 20, true> MP;
MP.reset();
vvc<int> states;
vc<pair<int, int>> edges;
states.eb(vc<int>(N, -1));
states.eb(vc<int>(N, -1));
MP[hash_vector<int>(states[0])] = 0;
int p = -1;
while (1) {
if (++p == len(states)) break;
if (p == 1) {
edges.eb(1, 1);
continue;
}
vc<int> now = states[p];
for (auto&& [nxt, convert]: next_states(now)) {
// 今の成分数、消える成分数
int a = 0, b = 0;
FOR(v, N) if (now[v] == v) {
++a;
if (convert[v] == -1) ++b;
}
// 消える成分があってよいのは、終状態にいくときのみ
if (b >= 2) continue;
if (b == 1) {
if (MAX(nxt) != -1) continue;
edges.eb(p, 1);
continue;
}
bool ok = [&](vc<int>& now, vc<int>& nxt, vc<int>& convert) -> bool {
// 頂点のみで接するのはダメ
FOR(i, N - 1) {
bool a1 = now[i] != -1, a2 = now[i + 1] != -1;
bool b1 = nxt[i] != -1, b2 = nxt[i + 1] != -1;
if (a1 && !a2 && !b1 && b2) return false;
if (!a1 && a2 && b1 && !b2) return false;
}
// empty region を閉じることと、異なる連結成分がマージされることが同値
int close = 0;
int after = 0;
vc<bool> is_new(N, 1);
FOR(i, N) if (convert[i] != -1) is_new[convert[i]] = 0;
FOR(i, N) if (nxt[i] == i && !is_new[i])++ after;
vc<int> I;
FOR(i, N) if (now[i] != -1) I.eb(i);
FOR(k, len(I) - 1) {
int i = I[k], j = I[k + 1];
if (j == i + 1) continue;
bool cl = 1;
FOR(p, i + 1, j) if (nxt[p] == -1) cl = 0;
if (cl) close++;
}
return a - close == after;
}(now, nxt, convert);
if (!ok) continue;
u64 h = hash_vector<int>(nxt);
int idx = MP.index(h);
if (!MP.used[idx]) {
MP.used[idx] = 1, MP.IDS.eb(idx), MP.key[idx] = h,
MP.val[idx] = len(states);
states.eb(nxt);
}
edges.eb(p, MP.val[idx]);
}
}
return {states, edges};
}
} // namespace connected_dp_squares
#line 2 "ds/hashmap.hpp"
// u64 -> Val
template <typename Val, int LOG = 20, bool KEEP_IDS = false>
struct HashMap {
static constexpr int N = (1 << LOG);
u64* key;
Val* val;
vc<int> IDS;
bitset<N> used;
const int shift;
const u64 r = 11995408973635179863ULL;
HashMap() : key(new u64[N]), val(new Val[N]), shift(64 - LOG) {}
u32 hash(u64 x) {
static const u64 FIXED_RANDOM
= std::chrono::steady_clock::now().time_since_epoch().count();
return (u64(x + FIXED_RANDOM) * r) >> shift;
}
int index(const u64& k) {
int i = 0;
for (i = hash(k); used[i] && key[i] != k; (i += 1) &= (N - 1)) {}
return i;
}
Val& operator[](const u64& k) {
int i = index(k);
if (!used[i]) {
used[i] = 1, key[i] = k, val[i] = Val{};
if constexpr (KEEP_IDS) IDS.eb(i);
}
return val[i];
}
Val get(const u64& k, Val default_value) {
int i = index(k);
if (!used[i]) return default_value;
return val[i];
}
bool count(const u64& k) {
int i = index(k);
return used[i] && key[i] == k;
}
void reset() {
static_assert(KEEP_IDS);
for (auto&& i: IDS) used[i] = 0;
IDS.clear();
}
// f(key, val)
template <typename F>
void enumerate_all(F f) {
static_assert(KEEP_IDS);
for (auto&& i: IDS) f(key[i], val[i]);
}
};
#line 2 "random/hash_vector.hpp"
#line 2 "random/base.hpp"
u64 RNG_64() {
static uint64_t x_
= uint64_t(chrono::duration_cast<chrono::nanoseconds>(
chrono::high_resolution_clock::now().time_since_epoch())
.count())
* 10150724397891781847ULL;
x_ ^= x_ << 7;
return x_ ^= x_ >> 9;
}
u64 RNG(u64 lim) { return RNG_64() % lim; }
ll RNG(ll l, ll r) { return l + RNG_64() % (r - l); }
#line 2 "mod/modint61.hpp"
struct modint61 {
static constexpr u64 mod = (1ULL << 61) - 1;
u64 val;
constexpr modint61() : val(0ULL) {}
constexpr modint61(u32 x) : val(x) {}
constexpr modint61(u64 x) : val(x % mod) {}
constexpr modint61(int x) : val((x < 0) ? (x + static_cast<ll>(mod)) : x) {}
constexpr modint61(ll x)
: val(((x %= static_cast<ll>(mod)) < 0) ? (x + static_cast<ll>(mod))
: x) {}
static constexpr u64 get_mod() { return mod; }
modint61 &operator+=(const modint61 &a) {
val = ((val += a.val) >= mod) ? (val - mod) : val;
return *this;
}
modint61 &operator-=(const modint61 &a) {
val = ((val -= a.val) >= mod) ? (val + mod) : val;
return *this;
}
modint61 &operator*=(const modint61 &a) {
const unsigned __int128 y = static_cast<unsigned __int128>(val) * a.val;
val = (y >> 61) + (y & mod);
val = (val >= mod) ? (val - mod) : val;
return *this;
}
modint61 &operator/=(const modint61 &a) { return (*this *= a.inverse()); }
modint61 operator+(const modint61 &p) const { return modint61(*this) += p; }
modint61 operator-(const modint61 &p) const { return modint61(*this) -= p; }
modint61 operator*(const modint61 &p) const { return modint61(*this) *= p; }
modint61 operator/(const modint61 &p) const { return modint61(*this) /= p; }
bool operator==(const modint61 &p) const { return val == p.val; }
bool operator!=(const modint61 &p) const { return val != p.val; }
modint61 inverse() const {
ll a = val, b = mod, u = 1, v = 0, t;
while (b > 0) {
t = a / b;
swap(a -= t * b, b), swap(u -= t * v, v);
}
return modint61(u);
}
modint61 pow(ll n) const {
assert(n >= 0);
modint61 ret(1), mul(val);
while (n > 0) {
if (n & 1) ret *= mul;
mul *= mul, n >>= 1;
}
return ret;
}
};
#ifdef FASTIO
void rd(modint61 &x) {
fastio::rd(x.val);
assert(0 <= x.val && x.val < modint61::mod);
}
void wt(modint61 x) { fastio::wt(x.val); }
#endif
#line 5 "random/hash_vector.hpp"
template <typename T>
u64 hash_vector(vc<T> X) {
using mint = modint61;
static vc<mint> hash_base;
int n = len(X);
while (len(hash_base) <= n) { hash_base.eb(RNG(mint::get_mod())); }
mint H = 0;
FOR(i, n) H += hash_base[i] * mint(X[i]);
H += hash_base[n];
return H.val;
}
#line 3 "other/connected_dp.hpp"
namespace connected_dp_squares {
// pair<新しい状態、今の成分 → 新しい成分>
vc<pair<vc<int>, vc<int>>> next_states(const vc<int>& now) {
int N = len(now);
vc<pair<vc<int>, vc<int>>> res;
FOR(s, 1 << N) {
vc<int> par(N + N);
FOR(i, N) par[i] = (s & 1 << i ? i : -1);
FOR(i, N) par[N + i] = (now[i] == -1 ? -1 : now[i] + N);
auto find = [&](int x) -> int {
while (par[x] != x) { x = par[x] = par[par[x]]; }
return x;
};
auto merge = [&](int a, int b) -> void {
a = find(a), b = find(b);
if (a == b) return;
if (a > b) swap(a, b);
par[b] = a;
};
FOR(i, N - 1) if (par[i] != -1 && par[i + 1] != -1) merge(i, i + 1);
FOR(i, N) if (par[i] != -1 && par[N + i] != -1) merge(i, N + i);
FOR(i, N + N) if (par[i] != -1) par[i] = find(i);
FOR(i, N, N + N) if (par[i] >= N) par[i] = -1;
res.eb(vc<int>(par.begin(), par.begin() + N),
vc<int>(par.begin() + N, par.end()));
}
return res;
}
vc<int> reverse_state(const vc<int>& now) {
int N = len(now);
vc<int> max_i(N, -1);
FOR(i, N) if (now[i] != -1) max_i[now[i]] = i;
vc<int> rev(N, -1);
FOR(i, N) {
if (now[i] == -1) continue;
int x = max_i[now[i]];
rev[N - 1 - i] = N - 1 - x;
}
return rev;
}
// 0, 1 :空の列、領域の手前、後ろ
// 連結領域をひとつ作る。
// 状態:-1 が選んでいない。0,1,2,3 等は同じ成分には同じ値が入る。
// [states, edges]
pair<vvc<int>, vc<pair<int, int>>> connedted_dp_graph(int N,
bool merge_reverse) {
static HashMap<int, 20, true> MP;
MP.reset();
vvc<int> states;
vc<pair<int, int>> edges;
states.eb(vc<int>(N, -1));
states.eb(vc<int>(N, -1));
MP[hash_vector<int>(states[0])] = 0;
int p = -1;
while (1) {
if (++p == len(states)) break;
if (p == 1) {
edges.eb(1, 1);
continue;
}
vc<int> now = states[p];
for (auto&& [nxt, convert]: next_states(now)) {
// 今の成分数、消える成分数
int a = 0, b = 0;
FOR(v, N) if (now[v] == v) {
++a;
if (convert[v] == -1) ++b;
}
// 消える成分があってよいのは、終状態にいくときのみ
if (b >= 2) continue;
if (b == 1) {
if (MAX(nxt) != -1) continue;
edges.eb(p, 1);
continue;
}
u64 h = hash_vector<int>(nxt);
if (merge_reverse) { chmin(h, hash_vector<int>(reverse_state(nxt))); }
if (!MP.count(h)) { MP[h] = len(states), states.eb(nxt); }
edges.eb(p, MP[h]);
}
}
return {states, edges};
}
// 0, 1 :空の列、領域の手前、後ろ
// 多角形(空洞なし)をひとつ作る。
// 状態:-1 が選んでいない。0,1,2,3 等は同じ成分には同じ値が入る。
// [states, edges]
pair<vvc<int>, vc<pair<int, int>>> polygon_dp_graph(int N) {
static HashMap<int, 20, true> MP;
MP.reset();
vvc<int> states;
vc<pair<int, int>> edges;
states.eb(vc<int>(N, -1));
states.eb(vc<int>(N, -1));
MP[hash_vector<int>(states[0])] = 0;
int p = -1;
while (1) {
if (++p == len(states)) break;
if (p == 1) {
edges.eb(1, 1);
continue;
}
vc<int> now = states[p];
for (auto&& [nxt, convert]: next_states(now)) {
// 今の成分数、消える成分数
int a = 0, b = 0;
FOR(v, N) if (now[v] == v) {
++a;
if (convert[v] == -1) ++b;
}
// 消える成分があってよいのは、終状態にいくときのみ
if (b >= 2) continue;
if (b == 1) {
if (MAX(nxt) != -1) continue;
edges.eb(p, 1);
continue;
}
bool ok = [&](vc<int>& now, vc<int>& nxt, vc<int>& convert) -> bool {
// 頂点のみで接するのはダメ
FOR(i, N - 1) {
bool a1 = now[i] != -1, a2 = now[i + 1] != -1;
bool b1 = nxt[i] != -1, b2 = nxt[i + 1] != -1;
if (a1 && !a2 && !b1 && b2) return false;
if (!a1 && a2 && b1 && !b2) return false;
}
// empty region を閉じることと、異なる連結成分がマージされることが同値
int close = 0;
int after = 0;
vc<bool> is_new(N, 1);
FOR(i, N) if (convert[i] != -1) is_new[convert[i]] = 0;
FOR(i, N) if (nxt[i] == i && !is_new[i])++ after;
vc<int> I;
FOR(i, N) if (now[i] != -1) I.eb(i);
FOR(k, len(I) - 1) {
int i = I[k], j = I[k + 1];
if (j == i + 1) continue;
bool cl = 1;
FOR(p, i + 1, j) if (nxt[p] == -1) cl = 0;
if (cl) close++;
}
return a - close == after;
}(now, nxt, convert);
if (!ok) continue;
u64 h = hash_vector<int>(nxt);
int idx = MP.index(h);
if (!MP.used[idx]) {
MP.used[idx] = 1, MP.IDS.eb(idx), MP.key[idx] = h,
MP.val[idx] = len(states);
states.eb(nxt);
}
edges.eb(p, MP.val[idx]);
}
}
return {states, edges};
}
} // namespace connected_dp_squares