結果
問題 | No.2152 [Cherry Anniversary 2] 19 Petals of Cherry |
ユーザー | hitonanode |
提出日時 | 2022-12-09 00:13:49 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 223 ms / 1,000 ms |
コード長 | 28,898 bytes |
コンパイル時間 | 2,299 ms |
コンパイル使用メモリ | 187,616 KB |
実行使用メモリ | 11,500 KB |
最終ジャッジ日時 | 2024-10-14 18:17:41 |
合計ジャッジ時間 | 9,428 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 35 ms
11,400 KB |
testcase_01 | AC | 82 ms
11,364 KB |
testcase_02 | AC | 22 ms
11,488 KB |
testcase_03 | AC | 158 ms
11,432 KB |
testcase_04 | AC | 130 ms
11,492 KB |
testcase_05 | AC | 126 ms
11,444 KB |
testcase_06 | AC | 130 ms
11,416 KB |
testcase_07 | AC | 121 ms
11,484 KB |
testcase_08 | AC | 122 ms
11,480 KB |
testcase_09 | AC | 104 ms
11,372 KB |
testcase_10 | AC | 126 ms
11,488 KB |
testcase_11 | AC | 124 ms
11,372 KB |
testcase_12 | AC | 98 ms
11,356 KB |
testcase_13 | AC | 103 ms
11,364 KB |
testcase_14 | AC | 125 ms
11,372 KB |
testcase_15 | AC | 129 ms
11,492 KB |
testcase_16 | AC | 107 ms
11,472 KB |
testcase_17 | AC | 120 ms
11,364 KB |
testcase_18 | AC | 135 ms
11,484 KB |
testcase_19 | AC | 130 ms
11,492 KB |
testcase_20 | AC | 86 ms
11,428 KB |
testcase_21 | AC | 114 ms
11,372 KB |
testcase_22 | AC | 120 ms
11,288 KB |
testcase_23 | AC | 107 ms
11,488 KB |
testcase_24 | AC | 155 ms
11,496 KB |
testcase_25 | AC | 116 ms
11,496 KB |
testcase_26 | AC | 138 ms
11,496 KB |
testcase_27 | AC | 118 ms
11,372 KB |
testcase_28 | AC | 32 ms
11,440 KB |
testcase_29 | AC | 44 ms
11,364 KB |
testcase_30 | AC | 57 ms
11,372 KB |
testcase_31 | AC | 66 ms
11,364 KB |
testcase_32 | AC | 77 ms
11,488 KB |
testcase_33 | AC | 85 ms
11,364 KB |
testcase_34 | AC | 95 ms
11,476 KB |
testcase_35 | AC | 105 ms
11,372 KB |
testcase_36 | AC | 115 ms
11,500 KB |
testcase_37 | AC | 129 ms
11,448 KB |
testcase_38 | AC | 137 ms
11,368 KB |
testcase_39 | AC | 149 ms
11,372 KB |
testcase_40 | AC | 162 ms
11,372 KB |
testcase_41 | AC | 167 ms
11,376 KB |
testcase_42 | AC | 180 ms
11,492 KB |
testcase_43 | AC | 190 ms
11,300 KB |
testcase_44 | AC | 202 ms
11,444 KB |
testcase_45 | AC | 223 ms
11,452 KB |
testcase_46 | AC | 210 ms
11,372 KB |
testcase_47 | AC | 21 ms
11,364 KB |
testcase_48 | AC | 146 ms
11,368 KB |
ソースコード
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <complex> #include <deque> #include <forward_list> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <type_traits> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; using lint = long long; using pint = pair<int, int>; using plint = pair<lint, lint>; struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++) #define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template <typename T, typename V> void ndarray(vector<T>& vec, const V& val, int len) { vec.assign(len, val); } template <typename T, typename V, typename... Args> void ndarray(vector<T>& vec, const V& val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); } template <typename T> bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; } template <typename T> bool chmin(T &m, const T q) { return m > q ? (m = q, true) : false; } const std::vector<std::pair<int, int>> grid_dxs{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; int floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); } template <class T1, class T2> T1 floor_div(T1 num, T2 den) { return (num > 0 ? num / den : -((-num + den - 1) / den)); } template <class T1, class T2> std::pair<T1, T2> operator+(const std::pair<T1, T2> &l, const std::pair<T1, T2> &r) { return std::make_pair(l.first + r.first, l.second + r.second); } template <class T1, class T2> std::pair<T1, T2> operator-(const std::pair<T1, T2> &l, const std::pair<T1, T2> &r) { return std::make_pair(l.first - r.first, l.second - r.second); } template <class T> std::vector<T> sort_unique(std::vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } template <class T> int arglb(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); } template <class T> int argub(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::upper_bound(v.begin(), v.end(), x)); } template <class IStream, class T> IStream &operator>>(IStream &is, std::vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec); template <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr); template <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec); template <class OStream, class T, class U> OStream &operator<<(OStream &os, const pair<T, U> &pa); template <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec); template <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec); template <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec); template <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec); template <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa); template <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp); template <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp); template <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl); template <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } template <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; } template <class... T> std::istream &operator>>(std::istream &is, std::tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; } template <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; } template <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; } template <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; } template <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } template <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } #ifdef HITONANODE_LOCAL const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m"; #define dbg(x) std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl #define dbgif(cond, x) ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl : std::cerr) #else #define dbg(x) ((void)0) #define dbgif(cond, x) ((void)0) #endif // Subset sum (fast zeta transform) // Complexity: O(N 2^N) for array of size 2^N template <typename T> void subset_sum(std::vector<T> &f) { const int sz = f.size(), n = __builtin_ctz(sz); assert(__builtin_popcount(sz) == 1); for (int d = 0; d < n; d++) { for (int S = 0; S < 1 << n; S++) if (S & (1 << d)) f[S] += f[S ^ (1 << d)]; } } // Inverse of subset sum (fast moebius transform) // Complexity: O(N 2^N) for array of size 2^N template <typename T> void subset_sum_inv(std::vector<T> &g) { const int sz = g.size(), n = __builtin_ctz(sz); assert(__builtin_popcount(sz) == 1); for (int d = 0; d < n; d++) { for (int S = 0; S < 1 << n; S++) if (S & (1 << d)) g[S] -= g[S ^ (1 << d)]; } } // Superset sum / its inverse (fast zeta/moebius transform) // Complexity: O(N 2^N) for array of size 2^N template <typename T> void superset_sum(std::vector<T> &f) { const int sz = f.size(), n = __builtin_ctz(sz); assert(__builtin_popcount(sz) == 1); for (int d = 0; d < n; d++) { for (int S = 0; S < 1 << n; S++) if (!(S & (1 << d))) f[S] += f[S | (1 << d)]; } } template <typename T> void superset_sum_inv(std::vector<T> &g) { const int sz = g.size(), n = __builtin_ctz(sz); assert(__builtin_popcount(sz) == 1); for (int d = 0; d < n; d++) { for (int S = 0; S < 1 << n; S++) if (!(S & (1 << d))) g[S] -= g[S | (1 << d)]; } } template <typename T> std::vector<std::vector<T>> build_zeta_(int D, const std::vector<T> &f) { int n = f.size(); std::vector<std::vector<T>> ret(D, std::vector<T>(n)); for (int i = 0; i < n; i++) ret[__builtin_popcount(i)][i] += f[i]; for (auto &vec : ret) subset_sum(vec); return ret; } template <typename T> std::vector<T> get_moebius_of_prod_(const std::vector<std::vector<T>> &mat1, const std::vector<std::vector<T>> &mat2) { int D = mat1.size(), n = mat1[0].size(); std::vector<std::vector<int>> pc2i(D); for (int i = 0; i < n; i++) pc2i[__builtin_popcount(i)].push_back(i); std::vector<T> tmp, ret(mat1[0].size()); for (int d = 0; d < D; d++) { tmp.assign(mat1[d].size(), 0); for (int e = 0; e <= d; e++) { for (int i = 0; i < int(tmp.size()); i++) tmp[i] += mat1[e][i] * mat2[d - e][i]; } subset_sum_inv(tmp); for (auto i : pc2i[d]) ret[i] = tmp[i]; } return ret; }; // Subset convolution // h[S] = \sum_T f[T] * g[S - T] // Complexity: O(N^2 2^N) for arrays of size 2^N template <typename T> std::vector<T> subset_convolution(std::vector<T> f, std::vector<T> g) { const int sz = f.size(), m = __builtin_ctz(sz) + 1; assert(__builtin_popcount(sz) == 1 and f.size() == g.size()); auto ff = build_zeta_(m, f), fg = build_zeta_(m, g); return get_moebius_of_prod_(ff, fg); } // https://hos-lyric.hatenablog.com/entry/2021/01/14/201231 template <class T, class Function> void subset_func(std::vector<T> &f, const Function &func) { const int sz = f.size(), m = __builtin_ctz(sz) + 1; assert(__builtin_popcount(sz) == 1); auto ff = build_zeta_(m, f); std::vector<T> p(m); for (int i = 0; i < sz; i++) { for (int d = 0; d < m; d++) p[d] = ff[d][i]; func(p); for (int d = 0; d < m; d++) ff[d][i] = p[d]; } for (auto &vec : ff) subset_sum_inv(vec); for (int i = 0; i < sz; i++) f[i] = ff[__builtin_popcount(i)][i]; } // log(f(x)) for f(x), f(0) == 1 // Requires inv() template <class T> void poly_log(std::vector<T> &f) { assert(f.at(0) == T(1)); static std::vector<T> invs{0}; const int m = f.size(); std::vector<T> finv(m); for (int d = 0; d < m; d++) { finv[d] = (d == 0); if (int(invs.size()) <= d) invs.push_back(T(d).inv()); for (int e = 0; e < d; e++) finv[d] -= finv[e] * f[d - e]; } std::vector<T> ret(m); for (int d = 1; d < m; d++) { for (int e = 0; d + e < m; e++) ret[d + e] += f[d] * d * finv[e] * invs[d + e]; } f = ret; } // log(f(S)) for set function f(S), f(0) == 1 // Requires inv() // Complexity: O(n^2 2^n) // https://atcoder.jp/contests/abc213/tasks/abc213_g template <class T> void subset_log(std::vector<T> &f) { subset_func(f, poly_log<T>); } // exp(f(S)) for set function f(S), f(0) == 0 // Complexity: O(n^2 2^n) // https://codeforces.com/blog/entry/92183 template <class T> void subset_exp(std::vector<T> &f) { const int sz = f.size(), m = __builtin_ctz(sz); assert(sz == 1 << m); assert(f.at(0) == 0); std::vector<T> ret{T(1)}; ret.reserve(sz); for (int d = 0; d < m; d++) { auto c = subset_convolution({f.begin() + (1 << d), f.begin() + (1 << (d + 1))}, ret); ret.insert(ret.end(), c.begin(), c.end()); } f = ret; } // sqrt(f(x)), f(x) == 1 // Requires inv of 2 // Compelxity: O(n^2) template <class T> void poly_sqrt(std::vector<T> &f) { assert(f.at(0) == T(1)); const int m = f.size(); static const auto inv2 = T(2).inv(); for (int d = 1; d < m; d++) { if (~(d & 1)) f[d] -= f[d / 2] * f[d / 2]; f[d] *= inv2; for (int e = 1; e < d - e; e++) f[d] -= f[e] * f[d - e]; } } // sqrt(f(S)) for set function f(S), f(0) == 1 // Requires inv() // https://atcoder.jp/contests/xmascon20/tasks/xmascon20_h template <class T> void subset_sqrt(std::vector<T> &f) { subset_func(f, poly_sqrt<T>); } // exp(f(S)) for set function f(S), f(0) == 0 template <class T> void poly_exp(std::vector<T> &P) { const int m = P.size(); assert(m and P[0] == 0); std::vector<T> Q(m), logQ(m), Qinv(m); Q[0] = Qinv[0] = T(1); static std::vector<T> invs{0}; auto set_invlog = [&](int d) { Qinv[d] = 0; for (int e = 0; e < d; e++) Qinv[d] -= Qinv[e] * Q[d - e]; while (d >= int(invs.size())) { int sz = invs.size(); invs.push_back(T(sz).inv()); } logQ[d] = 0; for (int e = 1; e <= d; e++) logQ[d] += Q[e] * e * Qinv[d - e]; logQ[d] *= invs[d]; }; for (int d = 1; d < m; d++) { Q[d] += P[d] - logQ[d]; set_invlog(d); assert(logQ[d] == P[d]); if (d + 1 < m) set_invlog(d + 1); } P = Q; } // f(S)^k for set function f(S) // Requires inv() template <class T> void subset_pow(std::vector<T> &f, long long k) { auto poly_pow = [&](std::vector<T> &f) { const int m = f.size(); if (k == 0) f[0] = 1, std::fill(f.begin() + 1, f.end(), T(0)); if (k <= 1) return; int nzero = 0; while (nzero < int(f.size()) and f[nzero] == T(0)) nzero++; int rem = std::max<long long>((long long)f.size() - nzero * k, 0LL); if (rem == 0) { std::fill(f.begin(), f.end(), 0); return; } f.erase(f.begin(), f.begin() + nzero); f.resize(rem); const T f0 = f.at(0), f0inv = f0.inv(), f0pow = f0.pow(k); for (auto &x : f) x *= f0inv; poly_log(f); for (auto &x : f) x *= k; poly_exp(f); for (auto &x : f) x *= f0pow; f.resize(rem, 0); f.insert(f.begin(), m - int(f.size()), T(0)); }; subset_func(f, poly_pow); } // Count perfect matchings of undirected graph (Hafnian of the matrix) // Assumption: mat[i][j] == mat[j][i], mat[i][i] == 0 // Complexity: O(n^2 2^n) // [1] A. Björklund, "Counting Perfect Matchings as Fast as Ryser, // Proc. of 23rd ACM-SIAM symposium on Discrete Algorithms, pp.914-921, 2012. template <class T> T hafnian(const std::vector<std::vector<T>> &mat) { const int N = mat.size(); if (N % 2) return 0; std::vector<std::vector<std::vector<T>>> B(N, std::vector<std::vector<T>>(N)); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) B[i][j] = std::vector<T>{mat[i][j]}; } std::vector<int> pc(1 << (N / 2)); std::vector<std::vector<int>> pc2i(N / 2 + 1); for (int i = 0; i < int(pc.size()); i++) { pc[i] = __builtin_popcount(i); pc2i[pc[i]].push_back(i); } std::vector<T> h{1}; for (int i = 1; i < N / 2; i++) { int r1 = N - i * 2, r2 = r1 + 1; auto h_add = subset_convolution(h, B[r2][r1]); h.insert(h.end(), h_add.begin(), h_add.end()); std::vector<std::vector<std::vector<T>>> B1(r1), B2(r1); for (int j = 0; j < r1; j++) { B1[j] = build_zeta_(i, B[r1][j]); B2[j] = build_zeta_(i, B[r2][j]); } for (int j = 0; j < r1; j++) { for (int k = 0; k < j; k++) { auto Sijk1 = get_moebius_of_prod_(B1[j], B2[k]); auto Sijk2 = get_moebius_of_prod_(B1[k], B2[j]); for (int s = 0; s < int(Sijk2.size()); s++) Sijk1[s] += Sijk2[s]; B[j][k].insert(B[j][k].end(), Sijk1.begin(), Sijk1.end()); } } } T ret = 0; for (int i = 0; i < int(h.size()); i++) ret += h[i] * B[1][0][h.size() - 1 - i]; return ret; } #include <cassert> #include <chrono> #include <random> // F_p, p = 2^61 - 1 // https://qiita.com/keymoon/items/11fac5627672a6d6a9f6 class ModIntMersenne61 { static const long long md = (1LL << 61) - 1; long long _v; inline unsigned hi() const noexcept { return _v >> 31; } inline unsigned lo() const noexcept { return _v & ((1LL << 31) - 1); } public: static long long mod() { return md; } ModIntMersenne61() : _v(0) {} // 0 <= x < md * 2 explicit ModIntMersenne61(long long x) : _v(x >= md ? x - md : x) {} long long val() const noexcept { return _v; } ModIntMersenne61 operator+(const ModIntMersenne61 &x) const { return ModIntMersenne61(_v + x._v); } ModIntMersenne61 operator-(const ModIntMersenne61 &x) const { return ModIntMersenne61(_v + md - x._v); } ModIntMersenne61 operator*(const ModIntMersenne61 &x) const { using ull = unsigned long long; ull uu = (ull)hi() * x.hi() * 2; ull ll = (ull)lo() * x.lo(); ull lu = (ull)hi() * x.lo() + (ull)lo() * x.hi(); ull sum = uu + ll + ((lu & ((1ULL << 30) - 1)) << 31) + (lu >> 30); ull reduced = (sum >> 61) + (sum & ull(md)); return ModIntMersenne61(reduced); } ModIntMersenne61 pow(long long n) const { assert(n >= 0); ModIntMersenne61 ans(1), tmp = *this; while (n) { if (n & 1) ans *= tmp; tmp *= tmp, n >>= 1; } return ans; } ModIntMersenne61 inv() const { return pow(md - 2); } ModIntMersenne61 operator/(const ModIntMersenne61 &x) const { return *this * x.inv(); } ModIntMersenne61 operator-() const { return ModIntMersenne61(md - _v); } ModIntMersenne61 &operator+=(const ModIntMersenne61 &x) { return *this = *this + x; } ModIntMersenne61 &operator-=(const ModIntMersenne61 &x) { return *this = *this - x; } ModIntMersenne61 &operator*=(const ModIntMersenne61 &x) { return *this = *this * x; } ModIntMersenne61 &operator/=(const ModIntMersenne61 &x) { return *this = *this / x; } ModIntMersenne61 operator+(unsigned x) const { return ModIntMersenne61(this->_v + x); } bool operator==(const ModIntMersenne61 &x) const { return _v == x._v; } bool operator!=(const ModIntMersenne61 &x) const { return _v != x._v; } bool operator<(const ModIntMersenne61 &x) const { return _v < x._v; } // To use std::map template <class OStream> friend OStream &operator<<(OStream &os, const ModIntMersenne61 &x) { return os << x._v; } static ModIntMersenne61 randgen(bool force_update = false) { static ModIntMersenne61 b(0); if (b == ModIntMersenne61(0) or force_update) { std::mt19937 mt(std::chrono::steady_clock::now().time_since_epoch().count()); std::uniform_int_distribution<long long> d(1, ModIntMersenne61::mod()); b = ModIntMersenne61(d(mt)); } return b; } }; #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] == 0) 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; } }; using mint = ModIntMersenne61; int main() { constexpr int D = 19; vector<vector<int>> vs(D); matrix<mint> mat(D, D); vector<lint> dp(1 << D); dp.front() = 1; REP(d, D) { vector<lint> dpnxt = dp; auto &v = vs.at(d); int l; cin >> l; v.resize(l); for (auto &x : v) { cin >> x; --x; REP(s, dp.size()) { if ((s & (1 << x)) == 0) { dpnxt.at(s + (1 << x)) += dp.at(s); } } mat[d][x] = mint(1); } dp = dpnxt; } // dbg(vs); // dbg(mat); auto de = mat.gauss_jordan().determinant_of_upper_triangle(); lint det = de.val(); if (det > 1LL << 60) det = -(-de).val(); dbg(det); dbg(dp.back()); dbg(dp.back() / 2); cout << (dp.back() + det) / 2 << ' ' << (dp.back() - det) / 2 << endl; }