This documentation is automatically generated by online-judge-tools/verification-helper
// パス上の頂点更新・パス上の頂点積取得が可能な Link-Cut tree
// 各頂点に 2x2 行列を載せ,演算として行列積が入る非可換・パス上更新の例.
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A" // DUMMY
#include "../link_cut_tree.hpp"
#include "../../linear_algebra_matrix/matrix.hpp"
#include "../../modint.hpp"
#include "../../random/xorshift.hpp"
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <unordered_set>
#include <utility>
using namespace std;
constexpr int md = 998244353;
const int NTRY = 1000;
const int VMAX = 50;
const int QPERTRY = 1000;
const int dim = 2;
using mint = ModInt<md>;
using S = tuple<int, matrix<mint>, matrix<mint>>;
using F = pair<bool, matrix<mint>>;
S op(S l, S r) {
int sl, sr;
matrix<mint> ml1, ml2, mr1, mr2;
tie(sl, ml1, ml2) = l;
tie(sr, mr1, mr2) = r;
return {sl + sr, mr1 * ml1, ml2 * mr2};
}
S mapping(F f, S x) {
int sz = get<0>(x);
if (sz) {
auto m = f.second.pow(sz);
return {sz, m, m};
}
return x;
}
S reversal(S x) { return {get<0>(x), get<2>(x), get<1>(x)}; }
F composition(F f, F g) { return f.first ? f : g; }
F id() { return {false, matrix<mint>::Identity(dim)}; }
using LCT = lazy_linkcuttree<S, F, op, reversal, mapping, composition, id>;
vector<int> connected_vertices(int N, int r, const vector<unordered_set<int>> &to) {
vector<int> visited(N);
vector<int> ret, tmp{r};
while (tmp.size()) {
int now = tmp.back();
tmp.pop_back();
ret.push_back(now);
visited[now] = 1;
for (auto nxt : to[now]) {
if (!visited[nxt]) tmp.push_back(nxt);
}
}
return ret;
}
vector<int> get_rev_path(int s, int t, int prv, const vector<unordered_set<int>> &to) {
if (s == t) return {s};
for (auto nxt : to[s]) {
if (nxt == prv) continue;
auto v = get_rev_path(nxt, t, s, to);
if (v.size()) {
v.push_back(s);
return v;
}
}
return {};
}
S gen_rand_a() {
matrix<mint> ret(dim, dim);
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) ret[i][j] = rand_int() % md;
}
return {1, ret, ret};
}
int main() {
for (int ntry = 0; ntry < NTRY; ntry++) {
const int N = 2 + rand_int() % (VMAX - 1);
vector<S> A(N);
LCT tree;
vector<LCT::Node *> nodes;
for (int i = 0; i < N; i++) {
A[i] = gen_rand_a();
nodes.push_back(tree.make_node(A[i]));
}
vector<pair<int, int>> edges;
vector<unordered_set<int>> to(N);
auto try_to_add_edge = [&]() {
int a = rand_int() % N;
vector<int> is_cmp(N, 1);
for (auto i : connected_vertices(N, a, to)) is_cmp[i] = 0;
vector<int> cmp;
for (int i = 0; i < N; i++) {
if (is_cmp[i]) cmp.push_back(i);
}
if (cmp.empty()) return;
int b = cmp[rand_int() % cmp.size()];
edges.emplace_back(a, b);
to[a].insert(b), to[b].insert(a);
tree.link(nodes[a], nodes[b]);
};
for (int i = 0; i < N / 2; i++) try_to_add_edge();
for (int q = 0; q < QPERTRY; q++) {
const int tp = rand_int() % 6;
if (tp == 0) {
// cut() if possible
if (edges.empty()) continue;
int e = rand_int() % edges.size();
int a = edges[e].first, b = edges[e].second;
edges.erase(edges.begin() + e);
to[a].erase(b), to[b].erase(a);
tree.cut(nodes[a], nodes[b]);
} else if (tp == 1) {
// link() if possible
try_to_add_edge();
} else if (tp == 2) {
// apply()
const int u = rand_int() % N;
auto conn = connected_vertices(N, u, to);
int v = conn[rand_int() % conn.size()];
const auto a = gen_rand_a();
tree.apply(nodes[u], nodes[v], {true, get<1>(a)});
for (auto i : get_rev_path(u, v, -1, to)) A[i] = a;
} else if (tp == 3) {
// prod()
const int u = rand_int() % N;
auto conn = connected_vertices(N, u, to);
int v = conn[rand_int() % conn.size()];
S ret1 = tree.prod(nodes[u], nodes[v]);
auto ret2 = S(A[u]);
for (auto i : get_rev_path(v, u, -1, to)) {
if (i != u) ret2 = op(ret2, A[i]);
}
assert(ret1 == ret2);
} else if (tp == 4) {
// set()
const int u = rand_int() % N;
const auto a = gen_rand_a();
tree.set(nodes[u], a);
A[u] = a;
} else if (tp == 5) {
// get()
const int u = rand_int() % N;
const S a = tree.get(nodes[u]);
assert(a == A[u]);
} else {
exit(8);
}
}
}
puts("Hello World");
}
#line 1 "data_structure/test/link_cut_tree.noncommutative.stress.test.cpp"
// パス上の頂点更新・パス上の頂点積取得が可能な Link-Cut tree
// 各頂点に 2x2 行列を載せ,演算として行列積が入る非可換・パス上更新の例.
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A" // DUMMY
#line 2 "data_structure/link_cut_tree.hpp"
// CUT begin
// Link-Cut Tree
// Reference:
// - https://www.slideshare.net/iwiwi/2-12188845
// - https://ei1333.github.io/library/structure/lct/link-cut-tree-lazy-path.cpp
template <class S, class F, S (*op)(S, S), S (*reversal)(S), S (*mapping)(F, S),
F (*composition)(F, F), F (*id)()>
class lazy_linkcuttree {
public:
struct Node {
Node *l, *r, *p;
S d, sum;
F lz;
bool is_reversed;
int sz;
Node(S val)
: l(nullptr), r(nullptr), p(nullptr), d(val), sum(val), lz(id()), is_reversed(false),
sz(1) {}
bool is_root() const { return !p || (p->l != this and p->r != this); }
template <class OStream> friend OStream &operator<<(OStream &os, const Node &n) {
os << '[';
if (n.l) os << *(n.l) << ',';
os << n.d << ',';
if (n.r) os << *(n.r);
return os << ']';
}
};
protected:
void update(Node *t) {
if (t == nullptr) return;
t->sz = 1;
t->sum = t->d;
if (t->l) {
t->sz += t->l->sz;
t->sum = op(t->l->sum, t->sum);
}
if (t->r) {
t->sz += t->r->sz;
t->sum = op(t->sum, t->r->sum);
}
}
void all_apply(Node *a, F b) {
a->d = mapping(b, a->d);
a->sum = mapping(b, a->sum);
a->lz = composition(b, a->lz);
}
void _toggle(Node *t) {
auto tmp = t->l;
t->l = t->r, t->r = tmp;
t->sum = reversal(t->sum);
t->is_reversed ^= true;
}
void push(Node *&t) {
if (t->lz != id()) {
if (t->l) all_apply(t->l, t->lz);
if (t->r) all_apply(t->r, t->lz);
t->lz = id();
}
if (t->is_reversed) {
if (t->l) _toggle(t->l);
if (t->r) _toggle(t->r);
t->is_reversed = false;
}
}
void _rot_r(Node *t) {
Node *x = t->p, *y = x->p;
if ((x->l = t->r)) t->r->p = x;
t->r = x, x->p = t;
update(x), update(t);
if ((t->p = y)) {
if (y->l == x) y->l = t;
if (y->r == x) y->r = t;
update(y);
}
}
void _rot_l(Node *t) {
Node *x = t->p, *y = x->p;
if ((x->r = t->l)) t->l->p = x;
t->l = x, x->p = t;
update(x), update(t);
if ((t->p = y)) {
if (y->l == x) y->l = t;
if (y->r == x) y->r = t;
update(y);
}
}
void _splay(Node *t) {
push(t);
while (!t->is_root()) {
Node *q = t->p;
if (q->is_root()) {
push(q), push(t);
if (q->l == t)
_rot_r(t);
else
_rot_l(t);
} else {
Node *r = q->p;
push(r), push(q), push(t);
if (r->l == q) {
if (q->l == t)
_rot_r(q), _rot_r(t);
else
_rot_l(t), _rot_r(t);
} else {
if (q->r == t)
_rot_l(q), _rot_l(t);
else
_rot_r(t), _rot_l(t);
}
}
}
}
public:
[[nodiscard]] Node *make_node(S val) { return new Node(val); }
void evert(Node *t) { expose(t), _toggle(t), push(t); }
Node *expose(Node *t) {
Node *rp = nullptr;
for (Node *cur = t; cur; cur = cur->p) {
_splay(cur);
cur->r = rp;
update(cur);
rp = cur;
}
_splay(t);
return rp;
}
void link(Node *chi, Node *par) {
evert(chi);
expose(par);
chi->p = par;
par->r = chi;
update(par);
}
void cut(Node *chi) {
expose(chi);
Node *par = chi->l;
chi->l = nullptr;
update(chi);
par->p = nullptr;
}
void cut(Node *u, Node *v) { evert(u), cut(v); }
Node *lca(Node *u, Node *v) { return expose(u), expose(v); }
void set(Node *t, S x) { expose(t), t->d = x, update(t); }
S get(Node *t) { return expose(t), t->d; }
void apply(Node *u, Node *v, const F &x) {
evert(u);
expose(v);
all_apply(v, x);
push(v);
}
S prod(Node *u, Node *v) {
evert(u);
expose(v);
return v->sum;
}
Node *kth_parent(Node *t, int k) {
expose(t);
while (t) {
push(t);
if (t->r and t->r->sz > k) {
t = t->r;
} else {
if (t->r) k -= t->r->sz;
if (k == 0) return t;
k--;
t = t->l;
}
}
return nullptr;
}
bool is_connected(Node *u, Node *v) {
expose(u), expose(v);
return u == v or u->p;
}
};
/* example usage:
struct S {
int sz, sum, lhi, rhi, inhi;
S(int x) : sz(1), sum(x), lhi(x), rhi(x), inhi(x) {}
S(int sz_, int sum_, int lhi_, int rhi_, int inhi_)
: sz(sz_), sum(sum_), lhi(lhi_), rhi(rhi_), inhi(inhi_) {}
};
using F = pair<bool, int>;
S op(S l, S r) {
return S(l.sz + r.sz, l.sum + r.sum, max(l.sum + r.lhi, l.lhi), max(l.rhi + r.sum, r.rhi),
max<int>({l.inhi, r.inhi, l.rhi + r.lhi}));
}
S reversal(S x) { return S(x.sz, x.sum, x.rhi, x.lhi, x.inhi); }
S mapping(F f, S x) {
if (f.first) {
auto v = f.second;
auto sum = x.sz * v;
return S{x.sz, sum, max(v, sum), max(v, sum), max(v, sum)};
} else {
return x;
}
}
F composition(F fnew, F gold) { return fnew.first ? fnew : gold; }
F id() { return {false, 0}; }
using LCT = lazy_linkcuttree<S, F, op, reversal, mapping, composition, id>;
vector<LCT::Node*> vs;
*/
#line 2 "linear_algebra_matrix/matrix.hpp"
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iterator>
#include <type_traits>
#include <utility>
#include <vector>
namespace matrix_ {
struct has_id_method_impl {
template <class T_> static auto check(T_ *) -> decltype(T_::id(), std::true_type());
template <class T_> static auto check(...) -> std::false_type;
};
template <class T_> struct has_id : decltype(has_id_method_impl::check<T_>(nullptr)) {};
} // namespace matrix_
template <typename T> struct matrix {
int H, W;
std::vector<T> elem;
typename std::vector<T>::iterator operator[](int i) { return elem.begin() + i * W; }
inline T &at(int i, int j) { return elem[i * W + j]; }
inline T get(int i, int j) const { return elem[i * W + j]; }
int height() const { return H; }
int width() const { return W; }
std::vector<std::vector<T>> vecvec() const {
std::vector<std::vector<T>> ret(H);
for (int i = 0; i < H; i++) {
std::copy(elem.begin() + i * W, elem.begin() + (i + 1) * W, std::back_inserter(ret[i]));
}
return ret;
}
operator std::vector<std::vector<T>>() const { return vecvec(); }
matrix() = default;
matrix(int H, int W) : H(H), W(W), elem(H * W) {}
matrix(const std::vector<std::vector<T>> &d) : H(d.size()), W(d.size() ? d[0].size() : 0) {
for (auto &raw : d) std::copy(raw.begin(), raw.end(), std::back_inserter(elem));
}
template <typename T2, typename std::enable_if<matrix_::has_id<T2>::value>::type * = nullptr>
static T2 _T_id() {
return T2::id();
}
template <typename T2, typename std::enable_if<!matrix_::has_id<T2>::value>::type * = nullptr>
static T2 _T_id() {
return T2(1);
}
static matrix Identity(int N) {
matrix ret(N, N);
for (int i = 0; i < N; i++) ret.at(i, i) = _T_id<T>();
return ret;
}
matrix operator-() const {
matrix ret(H, W);
for (int i = 0; i < H * W; i++) ret.elem[i] = -elem[i];
return ret;
}
matrix operator*(const T &v) const {
matrix ret = *this;
for (auto &x : ret.elem) x *= v;
return ret;
}
matrix operator/(const T &v) const {
matrix ret = *this;
const T vinv = _T_id<T>() / v;
for (auto &x : ret.elem) x *= vinv;
return ret;
}
matrix operator+(const matrix &r) const {
matrix ret = *this;
for (int i = 0; i < H * W; i++) ret.elem[i] += r.elem[i];
return ret;
}
matrix operator-(const matrix &r) const {
matrix ret = *this;
for (int i = 0; i < H * W; i++) ret.elem[i] -= r.elem[i];
return ret;
}
matrix operator*(const matrix &r) const {
matrix ret(H, r.W);
for (int i = 0; i < H; i++) {
for (int k = 0; k < W; k++) {
for (int j = 0; j < r.W; j++) ret.at(i, j) += this->get(i, k) * r.get(k, j);
}
}
return ret;
}
matrix &operator*=(const T &v) { return *this = *this * v; }
matrix &operator/=(const T &v) { return *this = *this / v; }
matrix &operator+=(const matrix &r) { return *this = *this + r; }
matrix &operator-=(const matrix &r) { return *this = *this - r; }
matrix &operator*=(const matrix &r) { return *this = *this * r; }
bool operator==(const matrix &r) const { return H == r.H and W == r.W and elem == r.elem; }
bool operator!=(const matrix &r) const { return H != r.H or W != r.W or elem != r.elem; }
bool operator<(const matrix &r) const { return elem < r.elem; }
matrix pow(int64_t n) const {
matrix ret = Identity(H);
bool ret_is_id = true;
if (n == 0) return ret;
for (int i = 63 - __builtin_clzll(n); i >= 0; i--) {
if (!ret_is_id) ret *= ret;
if ((n >> i) & 1) ret *= (*this), ret_is_id = false;
}
return ret;
}
std::vector<T> pow_vec(int64_t n, std::vector<T> vec) const {
matrix x = *this;
while (n) {
if (n & 1) vec = x * vec;
x *= x;
n >>= 1;
}
return vec;
};
matrix transpose() const {
matrix ret(W, H);
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) ret.at(j, i) = this->get(i, j);
}
return ret;
}
// Gauss-Jordan elimination
// - Require inverse for every non-zero element
// - Complexity: O(H^2 W)
template <typename T2, typename std::enable_if<std::is_floating_point<T2>::value>::type * = nullptr>
static int choose_pivot(const matrix<T2> &mtr, int h, int c) noexcept {
int piv = -1;
for (int j = h; j < mtr.H; j++) {
if (mtr.get(j, c) and (piv < 0 or std::abs(mtr.get(j, c)) > std::abs(mtr.get(piv, c))))
piv = j;
}
return piv;
}
template <typename T2, typename std::enable_if<!std::is_floating_point<T2>::value>::type * = nullptr>
static int choose_pivot(const matrix<T2> &mtr, int h, int c) noexcept {
for (int j = h; j < mtr.H; j++) {
if (mtr.get(j, c) != T2()) return j;
}
return -1;
}
matrix gauss_jordan() const {
int c = 0;
matrix mtr(*this);
std::vector<int> ws;
ws.reserve(W);
for (int h = 0; h < H; h++) {
if (c == W) break;
int piv = choose_pivot(mtr, h, c);
if (piv == -1) {
c++;
h--;
continue;
}
if (h != piv) {
for (int w = 0; w < W; w++) {
std::swap(mtr[piv][w], mtr[h][w]);
mtr.at(piv, w) *= -_T_id<T>(); // To preserve sign of determinant
}
}
ws.clear();
for (int w = c; w < W; w++) {
if (mtr.at(h, w) != T()) ws.emplace_back(w);
}
const T hcinv = _T_id<T>() / mtr.at(h, c);
for (int hh = 0; hh < H; hh++)
if (hh != h) {
const T coeff = mtr.at(hh, c) * hcinv;
for (auto w : ws) mtr.at(hh, w) -= mtr.at(h, w) * coeff;
mtr.at(hh, c) = T();
}
c++;
}
return mtr;
}
int rank_of_gauss_jordan() const {
for (int i = H * W - 1; i >= 0; i--) {
if (elem[i] != 0) return i / W + 1;
}
return 0;
}
int rank() const { return gauss_jordan().rank_of_gauss_jordan(); }
T determinant_of_upper_triangle() const {
T ret = _T_id<T>();
for (int i = 0; i < H; i++) ret *= get(i, i);
return ret;
}
int inverse() {
assert(H == W);
std::vector<std::vector<T>> ret = Identity(H), tmp = *this;
int rank = 0;
for (int i = 0; i < H; i++) {
int ti = i;
while (ti < H and tmp[ti][i] == T()) ti++;
if (ti == H) {
continue;
} else {
rank++;
}
ret[i].swap(ret[ti]), tmp[i].swap(tmp[ti]);
T inv = _T_id<T>() / tmp[i][i];
for (int j = 0; j < W; j++) ret[i][j] *= inv;
for (int j = i + 1; j < W; j++) tmp[i][j] *= inv;
for (int h = 0; h < H; h++) {
if (i == h) continue;
const T c = -tmp[h][i];
for (int j = 0; j < W; j++) ret[h][j] += ret[i][j] * c;
for (int j = i + 1; j < W; j++) tmp[h][j] += tmp[i][j] * c;
}
}
*this = ret;
return rank;
}
friend std::vector<T> operator*(const matrix &m, const std::vector<T> &v) {
assert(m.W == int(v.size()));
std::vector<T> ret(m.H);
for (int i = 0; i < m.H; i++) {
for (int j = 0; j < m.W; j++) ret[i] += m.get(i, j) * v[j];
}
return ret;
}
friend std::vector<T> operator*(const std::vector<T> &v, const matrix &m) {
assert(int(v.size()) == m.H);
std::vector<T> ret(m.W);
for (int i = 0; i < m.H; i++) {
for (int j = 0; j < m.W; j++) ret[j] += v[i] * m.get(i, j);
}
return ret;
}
std::vector<T> prod(const std::vector<T> &v) const { return (*this) * v; }
std::vector<T> prod_left(const std::vector<T> &v) const { return v * (*this); }
template <class OStream> friend OStream &operator<<(OStream &os, const matrix &x) {
os << "[(" << x.H << " * " << x.W << " matrix)";
os << "\n[column sums: ";
for (int j = 0; j < x.W; j++) {
T s = T();
for (int i = 0; i < x.H; i++) s += x.get(i, j);
os << s << ",";
}
os << "]";
for (int i = 0; i < x.H; i++) {
os << "\n[";
for (int j = 0; j < x.W; j++) os << x.get(i, j) << ",";
os << "]";
}
os << "]\n";
return os;
}
template <class IStream> friend IStream &operator>>(IStream &is, matrix &x) {
for (auto &v : x.elem) is >> v;
return is;
}
};
#line 3 "modint.hpp"
#include <iostream>
#include <set>
#line 6 "modint.hpp"
template <int md> struct ModInt {
using lint = long long;
constexpr static int mod() { return md; }
static int get_primitive_root() {
static int primitive_root = 0;
if (!primitive_root) {
primitive_root = [&]() {
std::set<int> fac;
int v = md - 1;
for (lint i = 2; i * i <= v; i++)
while (v % i == 0) fac.insert(i), v /= i;
if (v > 1) fac.insert(v);
for (int g = 1; g < md; g++) {
bool ok = true;
for (auto i : fac)
if (ModInt(g).pow((md - 1) / i) == 1) {
ok = false;
break;
}
if (ok) return g;
}
return -1;
}();
}
return primitive_root;
}
int val_;
int val() const noexcept { return val_; }
constexpr ModInt() : val_(0) {}
constexpr ModInt &_setval(lint v) { return val_ = (v >= md ? v - md : v), *this; }
constexpr ModInt(lint v) { _setval(v % md + md); }
constexpr explicit operator bool() const { return val_ != 0; }
constexpr ModInt operator+(const ModInt &x) const {
return ModInt()._setval((lint)val_ + x.val_);
}
constexpr ModInt operator-(const ModInt &x) const {
return ModInt()._setval((lint)val_ - x.val_ + md);
}
constexpr ModInt operator*(const ModInt &x) const {
return ModInt()._setval((lint)val_ * x.val_ % md);
}
constexpr ModInt operator/(const ModInt &x) const {
return ModInt()._setval((lint)val_ * x.inv().val() % md);
}
constexpr ModInt operator-() const { return ModInt()._setval(md - val_); }
constexpr ModInt &operator+=(const ModInt &x) { return *this = *this + x; }
constexpr ModInt &operator-=(const ModInt &x) { return *this = *this - x; }
constexpr ModInt &operator*=(const ModInt &x) { return *this = *this * x; }
constexpr ModInt &operator/=(const ModInt &x) { return *this = *this / x; }
friend constexpr ModInt operator+(lint a, const ModInt &x) { return ModInt(a) + x; }
friend constexpr ModInt operator-(lint a, const ModInt &x) { return ModInt(a) - x; }
friend constexpr ModInt operator*(lint a, const ModInt &x) { return ModInt(a) * x; }
friend constexpr ModInt operator/(lint a, const ModInt &x) { return ModInt(a) / x; }
constexpr bool operator==(const ModInt &x) const { return val_ == x.val_; }
constexpr bool operator!=(const ModInt &x) const { return val_ != x.val_; }
constexpr bool operator<(const ModInt &x) const {
return val_ < x.val_;
} // To use std::map<ModInt, T>
friend std::istream &operator>>(std::istream &is, ModInt &x) {
lint t;
return is >> t, x = ModInt(t), is;
}
constexpr friend std::ostream &operator<<(std::ostream &os, const ModInt &x) {
return os << x.val_;
}
constexpr ModInt pow(lint n) const {
ModInt ans = 1, tmp = *this;
while (n) {
if (n & 1) ans *= tmp;
tmp *= tmp, n >>= 1;
}
return ans;
}
static constexpr int cache_limit = std::min(md, 1 << 21);
static std::vector<ModInt> facs, facinvs, invs;
constexpr static void _precalculation(int N) {
const int l0 = facs.size();
if (N > md) N = md;
if (N <= l0) return;
facs.resize(N), facinvs.resize(N), invs.resize(N);
for (int i = l0; i < N; i++) facs[i] = facs[i - 1] * i;
facinvs[N - 1] = facs.back().pow(md - 2);
for (int i = N - 2; i >= l0; i--) facinvs[i] = facinvs[i + 1] * (i + 1);
for (int i = N - 1; i >= l0; i--) invs[i] = facinvs[i] * facs[i - 1];
}
constexpr ModInt inv() const {
if (this->val_ < cache_limit) {
if (facs.empty()) facs = {1}, facinvs = {1}, invs = {0};
while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2);
return invs[this->val_];
} else {
return this->pow(md - 2);
}
}
constexpr ModInt fac() const {
while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2);
return facs[this->val_];
}
constexpr ModInt facinv() const {
while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2);
return facinvs[this->val_];
}
constexpr ModInt doublefac() const {
lint k = (this->val_ + 1) / 2;
return (this->val_ & 1) ? ModInt(k * 2).fac() / (ModInt(2).pow(k) * ModInt(k).fac())
: ModInt(k).fac() * ModInt(2).pow(k);
}
constexpr ModInt nCr(int r) const {
if (r < 0 or this->val_ < r) return ModInt(0);
return this->fac() * (*this - r).facinv() * ModInt(r).facinv();
}
constexpr ModInt nPr(int r) const {
if (r < 0 or this->val_ < r) return ModInt(0);
return this->fac() * (*this - r).facinv();
}
static ModInt binom(int n, int r) {
static long long bruteforce_times = 0;
if (r < 0 or n < r) return ModInt(0);
if (n <= bruteforce_times or n < (int)facs.size()) return ModInt(n).nCr(r);
r = std::min(r, n - r);
ModInt ret = ModInt(r).facinv();
for (int i = 0; i < r; ++i) ret *= n - i;
bruteforce_times += r;
return ret;
}
// Multinomial coefficient, (k_1 + k_2 + ... + k_m)! / (k_1! k_2! ... k_m!)
// Complexity: O(sum(ks))
template <class Vec> static ModInt multinomial(const Vec &ks) {
ModInt ret{1};
int sum = 0;
for (int k : ks) {
assert(k >= 0);
ret *= ModInt(k).facinv(), sum += k;
}
return ret * ModInt(sum).fac();
}
// Catalan number, C_n = binom(2n, n) / (n + 1)
// C_0 = 1, C_1 = 1, C_2 = 2, C_3 = 5, C_4 = 14, ...
// https://oeis.org/A000108
// Complexity: O(n)
static ModInt catalan(int n) {
if (n < 0) return ModInt(0);
return ModInt(n * 2).fac() * ModInt(n + 1).facinv() * ModInt(n).facinv();
}
ModInt sqrt() const {
if (val_ == 0) return 0;
if (md == 2) return val_;
if (pow((md - 1) / 2) != 1) return 0;
ModInt b = 1;
while (b.pow((md - 1) / 2) == 1) b += 1;
int e = 0, m = md - 1;
while (m % 2 == 0) m >>= 1, e++;
ModInt x = pow((m - 1) / 2), y = (*this) * x * x;
x *= (*this);
ModInt z = b.pow(m);
while (y != 1) {
int j = 0;
ModInt t = y;
while (t != 1) j++, t *= t;
z = z.pow(1LL << (e - j - 1));
x *= z, z *= z, y *= z;
e = j;
}
return ModInt(std::min(x.val_, md - x.val_));
}
};
template <int md> std::vector<ModInt<md>> ModInt<md>::facs = {1};
template <int md> std::vector<ModInt<md>> ModInt<md>::facinvs = {1};
template <int md> std::vector<ModInt<md>> ModInt<md>::invs = {0};
using ModInt998244353 = ModInt<998244353>;
// using mint = ModInt<998244353>;
// using mint = ModInt<1000000007>;
#line 2 "random/xorshift.hpp"
#include <cstdint>
// CUT begin
uint32_t rand_int() // XorShift random integer generator
{
static uint32_t x = 123456789, y = 362436069, z = 521288629, w = 88675123;
uint32_t t = x ^ (x << 11);
x = y;
y = z;
z = w;
return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
}
double rand_double() { return (double)rand_int() / UINT32_MAX; }
#line 8 "data_structure/test/link_cut_tree.noncommutative.stress.test.cpp"
#line 11 "data_structure/test/link_cut_tree.noncommutative.stress.test.cpp"
#include <cstdio>
#include <unordered_set>
#line 14 "data_structure/test/link_cut_tree.noncommutative.stress.test.cpp"
using namespace std;
constexpr int md = 998244353;
const int NTRY = 1000;
const int VMAX = 50;
const int QPERTRY = 1000;
const int dim = 2;
using mint = ModInt<md>;
using S = tuple<int, matrix<mint>, matrix<mint>>;
using F = pair<bool, matrix<mint>>;
S op(S l, S r) {
int sl, sr;
matrix<mint> ml1, ml2, mr1, mr2;
tie(sl, ml1, ml2) = l;
tie(sr, mr1, mr2) = r;
return {sl + sr, mr1 * ml1, ml2 * mr2};
}
S mapping(F f, S x) {
int sz = get<0>(x);
if (sz) {
auto m = f.second.pow(sz);
return {sz, m, m};
}
return x;
}
S reversal(S x) { return {get<0>(x), get<2>(x), get<1>(x)}; }
F composition(F f, F g) { return f.first ? f : g; }
F id() { return {false, matrix<mint>::Identity(dim)}; }
using LCT = lazy_linkcuttree<S, F, op, reversal, mapping, composition, id>;
vector<int> connected_vertices(int N, int r, const vector<unordered_set<int>> &to) {
vector<int> visited(N);
vector<int> ret, tmp{r};
while (tmp.size()) {
int now = tmp.back();
tmp.pop_back();
ret.push_back(now);
visited[now] = 1;
for (auto nxt : to[now]) {
if (!visited[nxt]) tmp.push_back(nxt);
}
}
return ret;
}
vector<int> get_rev_path(int s, int t, int prv, const vector<unordered_set<int>> &to) {
if (s == t) return {s};
for (auto nxt : to[s]) {
if (nxt == prv) continue;
auto v = get_rev_path(nxt, t, s, to);
if (v.size()) {
v.push_back(s);
return v;
}
}
return {};
}
S gen_rand_a() {
matrix<mint> ret(dim, dim);
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) ret[i][j] = rand_int() % md;
}
return {1, ret, ret};
}
int main() {
for (int ntry = 0; ntry < NTRY; ntry++) {
const int N = 2 + rand_int() % (VMAX - 1);
vector<S> A(N);
LCT tree;
vector<LCT::Node *> nodes;
for (int i = 0; i < N; i++) {
A[i] = gen_rand_a();
nodes.push_back(tree.make_node(A[i]));
}
vector<pair<int, int>> edges;
vector<unordered_set<int>> to(N);
auto try_to_add_edge = [&]() {
int a = rand_int() % N;
vector<int> is_cmp(N, 1);
for (auto i : connected_vertices(N, a, to)) is_cmp[i] = 0;
vector<int> cmp;
for (int i = 0; i < N; i++) {
if (is_cmp[i]) cmp.push_back(i);
}
if (cmp.empty()) return;
int b = cmp[rand_int() % cmp.size()];
edges.emplace_back(a, b);
to[a].insert(b), to[b].insert(a);
tree.link(nodes[a], nodes[b]);
};
for (int i = 0; i < N / 2; i++) try_to_add_edge();
for (int q = 0; q < QPERTRY; q++) {
const int tp = rand_int() % 6;
if (tp == 0) {
// cut() if possible
if (edges.empty()) continue;
int e = rand_int() % edges.size();
int a = edges[e].first, b = edges[e].second;
edges.erase(edges.begin() + e);
to[a].erase(b), to[b].erase(a);
tree.cut(nodes[a], nodes[b]);
} else if (tp == 1) {
// link() if possible
try_to_add_edge();
} else if (tp == 2) {
// apply()
const int u = rand_int() % N;
auto conn = connected_vertices(N, u, to);
int v = conn[rand_int() % conn.size()];
const auto a = gen_rand_a();
tree.apply(nodes[u], nodes[v], {true, get<1>(a)});
for (auto i : get_rev_path(u, v, -1, to)) A[i] = a;
} else if (tp == 3) {
// prod()
const int u = rand_int() % N;
auto conn = connected_vertices(N, u, to);
int v = conn[rand_int() % conn.size()];
S ret1 = tree.prod(nodes[u], nodes[v]);
auto ret2 = S(A[u]);
for (auto i : get_rev_path(v, u, -1, to)) {
if (i != u) ret2 = op(ret2, A[i]);
}
assert(ret1 == ret2);
} else if (tp == 4) {
// set()
const int u = rand_int() % N;
const auto a = gen_rand_a();
tree.set(nodes[u], a);
A[u] = a;
} else if (tp == 5) {
// get()
const int u = rand_int() % N;
const S a = tree.get(nodes[u]);
assert(a == A[u]);
} else {
exit(8);
}
}
}
puts("Hello World");
}