結果
問題 |
No.1561 connect x connect
|
ユーザー |
|
提出日時 | 2025-09-01 21:52:51 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 51,713 bytes |
コンパイル時間 | 11,136 ms |
コンパイル使用メモリ | 339,896 KB |
実行使用メモリ | 7,720 KB |
最終ジャッジ日時 | 2025-09-01 21:53:04 |
合計ジャッジ時間 | 12,036 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 RE * 1 |
other | AC * 14 RE * 21 |
ソースコード
#ifndef HIDDEN_IN_VS // 折りたたみ用 // 警告の抑制 #define _CRT_SECURE_NO_WARNINGS // ライブラリの読み込み #include <bits/stdc++.h> using namespace std; // 型名の短縮 using ll = long long; using ull = unsigned long long; // -2^63 ~ 2^63 = 9e18(int は -2^31 ~ 2^31 = 2e9) using pii = pair<int, int>; using pll = pair<ll, ll>; using pil = pair<int, ll>; using pli = pair<ll, int>; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vvvvi = vector<vvvi>; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vvvvl = vector<vvvl>; using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>; using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>; using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>; template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>; using Graph = vvi; // 定数の定義 const double PI = acos(-1); int DX[4] = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左) int DY[4] = { 0, 1, 0, -1 }; int INF = 1001001001; ll INFL = 4004004003094073385LL; // (int)INFL = INF, (int)(-INFL) = -INF; // 入出力高速化 struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp; // 汎用マクロの定義 #define all(a) (a).begin(), (a).end() #define sz(x) ((int)(x).size()) #define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), (x))) #define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), (x))) #define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");} #define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順 #define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順 #define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順 #define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能) #define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能) #define repb(set, d) for(int set = 0, set##_ub = 1 << int(d); set < set##_ub; ++set) // d ビット全探索(昇順) #define repis(i, set) for(int i = lsb(set), bset##i = set; i < 32; bset##i -= 1 << i, i = lsb(bset##i)) // set の全要素(昇順) #define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順) #define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去 #define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了 #define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) // 半開矩形内判定 // 汎用関数の定義 template <class T> inline ll powi(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; } template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す) template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す) template <class T> inline int getb(T set, int i) { return (set >> i) & T(1); } template <class T> inline T smod(T n, T m) { n %= m; if (n < 0) n += m; return n; } // 非負mod // 演算子オーバーロード template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; } template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; } template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; } template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; } #endif // 折りたたみ用 #if __has_include(<atcoder/all>) #include <atcoder/all> using namespace atcoder; #ifdef _MSC_VER #include "localACL.hpp" #endif //using mint = modint998244353; using mint = static_modint<(int)1e9+7>; //using mint = modint; // mint::set_mod(m); using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>; using pim = pair<int, mint>; #endif #ifdef _MSC_VER // 手元環境(Visual Studio) #include "local.hpp" #else // 提出用(gcc) int mute_dump = 0; int frac_print = 0; #if __has_include(<atcoder/all>) namespace atcoder { inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; } inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; } } #endif inline int popcount(int n) { return __builtin_popcount(n); } inline int popcount(ll n) { return __builtin_popcountll(n); } inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : 32; } inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : 64; } inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; } inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; } #define dump(...) #define dumpel(v) #define dump_math(v) #define input_from_file(f) #define output_to_file(f) #define Assert(b) { if (!(b)) { vc MLE(1<<30); EXIT(MLE.back()); } } // RE の代わりに MLE を出す #endif // 愚直 int H; mint naive(const string& s) { int w = sz(s); if (w == 0) return 1; vi a(H * w); rep(j, w) { int val = s[j] - '0'; rep(i, H) { int u = j * H + i; a[u] = getb(val, i); } } dsu d(H * w + 1); rep(i, H) rep(j, w - 1) { int u = j * H + i; int v = (j + 1) * H + i; if (a[u] && a[v]) d.merge(u, v); } rep(i, H - 1) rep(j, w) { int u = j * H + i; int v = j * H + (i + 1); if (a[u] && a[v]) d.merge(u, v); } rep(i, H) rep(j, w) { int u = j * H + i; if (!a[u]) d.merge(u, H * w); } return (int)(sz(d.groups()) <= 2); } //【行列】 /* * Matrix<T>(int n, int m) : O(n m) * n×m 零行列で初期化する. * * Matrix<T>(int n) : O(n^2) * n×n 単位行列で初期化する. * * Matrix<T>(vvT a) : O(n m) * 二次元配列 a[0..n)[0..m) の要素で初期化する. * * bool empty() : O(1) * 行列が空かを返す. * * A + B : O(n m) * n×m 行列 A, B の和を返す.+= も使用可. * * A - B : O(n m) * n×m 行列 A, B の差を返す.-= も使用可. * * c * A / A * c : O(n m) * n×m 行列 A とスカラー c のスカラー積を返す.*= も使用可. * * A * x : O(n m) * n×m 行列 A と n 次元列ベクトル x の積を返す. * * x * A : O(n m)(やや遅い) * m 次元行ベクトル x と n×m 行列 A の積を返す. * * A * B : O(n m l) * n×m 行列 A と m×l 行列 B の積を返す. * * Mat pow(ll d) : O(n^3 log d) * 自身を d 乗した行列を返す. */ template <class T> struct Matrix { int n, m; // 行列のサイズ(n 行 m 列) vector<vector<T>> v; // 行列の成分 // n×m 零行列で初期化する. Matrix(int n, int m) : n(n), m(m), v(n, vector<T>(m)) {} // n×n 単位行列で初期化する. Matrix(int n) : n(n), m(n), v(n, vector<T>(n)) { rep(i, n) v[i][i] = T(1); } // 二次元配列 a[0..n)[0..m) の要素で初期化する. Matrix(const vector<vector<T>>& a) : n(sz(a)), m(sz(a[0])), v(a) {} Matrix() : n(0), m(0) {} // 代入 Matrix(const Matrix&) = default; Matrix& operator=(const Matrix&) = default; // アクセス inline vector<T> const& operator[](int i) const { return v[i]; } inline vector<T>& operator[](int i) { // verify : https://judge.yosupo.jp/problem/matrix_product // inline を付けて [] でアクセスするとなぜか v[] への直接アクセスより速くなった. return v[i]; } // 入力 friend istream& operator>>(istream& is, Matrix& a) { rep(i, a.n) rep(j, a.m) is >> a.v[i][j]; return is; } // 行の追加 void push_back(const vector<T>& a) { Assert(sz(a) == m); v.push_back(a); n++; } // 行の削除 void pop_back() { Assert(n > 0); v.pop_back(); n--; } // サイズ変更 void resize(int n_) { v.resize(n_); n = n_; } void resize(int n_, int m_) { n = n_; m = m_; v.resize(n); rep(i, n) v[i].resize(m); } // 空か bool empty() const { return min(n, m) == 0; } // 比較 bool operator==(const Matrix& b) const { return n == b.n && m == b.m && v == b.v; } bool operator!=(const Matrix& b) const { return !(*this == b); } // 加算,減算,スカラー倍 Matrix& operator+=(const Matrix& b) { rep(i, n) rep(j, m) v[i][j] += b[i][j]; return *this; } Matrix& operator-=(const Matrix& b) { rep(i, n) rep(j, m) v[i][j] -= b[i][j]; return *this; } Matrix& operator*=(const T& c) { rep(i, n) rep(j, m) v[i][j] *= c; return *this; } Matrix operator+(const Matrix& b) const { return Matrix(*this) += b; } Matrix operator-(const Matrix& b) const { return Matrix(*this) -= b; } Matrix operator*(const T& c) const { return Matrix(*this) *= c; } friend Matrix operator*(const T& c, const Matrix<T>& a) { return a * c; } Matrix operator-() const { return Matrix(*this) *= T(-1); } // 行列ベクトル積 : O(m n) vector<T> operator*(const vector<T>& x) const { vector<T> y(n); rep(i, n) rep(j, m) y[i] += v[i][j] * x[j]; return y; } // ベクトル行列積 : O(m n) friend vector<T> operator*(const vector<T>& x, const Matrix& a) { vector<T> y(a.m); rep(i, a.n) rep(j, a.m) y[j] += x[i] * a[i][j]; return y; } // 積:O(n^3) Matrix operator*(const Matrix& b) const { // verify : https://judge.yosupo.jp/problem/matrix_product Matrix res(n, b.m); rep(i, res.n) rep(k, m) rep(j, res.m) res[i][j] += v[i][k] * b[k][j]; return res; } Matrix& operator*=(const Matrix& b) { *this = *this * b; return *this; } // 累乗:O(n^3 log d) Matrix pow(ll d) const { // verify : https://judge.yosupo.jp/problem/pow_of_matrix Matrix res(n), pow2 = *this; while (d > 0) { if (d & 1) res *= pow2; pow2 *= pow2; d >>= 1; } return res; } #ifdef _MSC_VER friend ostream& operator<<(ostream& os, const Matrix& a) { rep(i, a.n) { os << "["; rep(j, a.m) os << a[i][j] << " ]"[j == a.m - 1]; if (i < a.n - 1) os << "\n"; } return os; } #endif }; //【行簡約形(行交換なし)】O(n m min(n, m)) /* * 行基本変形(行交換なし)で n×m 行列 A を行簡約形に変形し,ピボット位置のリストを返す. */ template <class T> vector<pii> row_reduced_form(Matrix<T>& A) { int n = A.n, m = A.m; vector<pii> piv; piv.reserve(min(n, m)); // 未確定の列を記録しておくリスト list<int> rjs; rep(j, m) rjs.push_back(j); rep(i, n) { // 第 i 行の係数を左から走査し非 0 を見つける. auto it = rjs.begin(); for (; it != rjs.end(); it++) if (A[i][*it] != 0) break; // 第 i 行の全てが 0 なら無視する. if (it == rjs.end()) continue; // A[i][j] をピボットに選択する. int j = *it; rjs.erase(it); piv.emplace_back(i, j); // A[i][j] が 1 になるよう行全体を A[i][j] で割る. T Aij_inv = T(1) / A[i][j]; repi(j2, j, m - 1) A[i][j2] *= Aij_inv; // 第 i 行以外の第 j 列の成分が全て 0 になるよう第 i 行を定数倍して減じる. rep(i2, n) if (A[i2][j] != 0 && i2 != i) { T mul = A[i2][j]; repi(j2, j, m - 1) A[i2][j2] -= A[i][j2] * mul; } } return piv; } //【逆行列】O(n^3) /* * n 次正方行列 mat の逆行列を返す(存在しなければ空) */ template <class T> Matrix<T> inverse_matrix(const Matrix<T>& mat) { // verify : https://judge.yosupo.jp/problem/inverse_matrix int n = mat.n; // 元の行列 mat と単位行列を繋げた拡大行列 v を作る. vector<vector<T>> v(n, vector<T>(2 * n)); rep(i, n) rep(j, n) { v[i][j] = mat[i][j]; if (i == j) v[i][n + j] = 1; } int m = 2 * n; // 注目位置を (i, j)(i 行目かつ j 列目)とする. int i = 0, j = 0; // 拡大行列に対して行基本変形を行い,左側を単位行列にすることを目指す. while (i < n && j < m) { // 同じ列の下方の行から非 0 成分を見つける. int i2 = i; while (i2 < n && v[i2][j] == T(0)) i2++; // 見つからなかったら全て 0 の列があったので mat は非正則 if (i2 == n) return Matrix<T>(); // 見つかったら i 行目とその行を入れ替える. if (i != i2) swap(v[i], v[i2]); // v[i][j] が 1 になるよう行全体を v[i][j] で割る. T vij_inv = T(1) / v[i][j]; repi(j2, j, m - 1) v[i][j2] *= vij_inv; // v[i][j] と同じ列の成分が全て 0 になるよう i 行目を定数倍して減じる. rep(i2, n) { // i 行目だけは引かない. if (i2 == i) continue; T mul = v[i2][j]; repi(j2, j, m - 1) v[i2][j2] -= v[i][j2] * mul; } // 注目位置を右下に移す. i++; j++; } // 拡大行列の右半分が mat の逆行列なのでコピーする. Matrix<T> mat_inv(n, n); rep(i, n) rep(j, n) mat_inv[i][j] = v[i][n + j]; return mat_inv; } // 遷移行列の係数を計算し,埋め込み用のコードを出力する. void embed_coefs(int COL, int len_max, int L_max, int loop_cnt, const vector<string>& ssT_ini = { "" }, const vector<string>& ssB_ini = { "" }) { mt19937_64 mt((int)time(NULL)); uniform_int_distribution<int> rnd_len(1, len_max); uniform_int_distribution<int> rnd_col(0, COL - 1); uniform_int_distribution<int> rnd(7, 20); vector<string> ssT(ssT_ini), ssB(ssB_ini); rep(loop, loop_cnt) { if ((loop + 1) % 100 == 0) { dump("loop:", loop, "L:", sz(ssT)); // 途中再開用 sort(all(ssT)); sort(all(ssB)); string eb = "vvi ssT = {"; repe(s, ssT) { eb += "{"; repe(c, s) { eb += to_string((int)(c - '0')); eb += ","; } if (eb.back() == ',') eb.pop_back(); eb += "},"; } if (eb.back() == ',') eb.pop_back(); eb += "};\n"; eb += "vvi ssB = {"; repe(s, ssB) { eb += "{"; repe(c, s) { eb += to_string((int)(c - '0')); eb += ","; } if (eb.back() == ',') eb.pop_back(); eb += "},"; } if (eb.back() == ',') eb.pop_back(); eb += "};\n"; cerr << eb; } // 候補とする文字列をランダムに L_max 個追加する. rep(hoge, L_max) { int len = rnd_len(mt); string s; rep(fuga, len) s += '0' + rnd_col(mt); ssT.push_back(s); } rep(hoge, L_max) { int len = rnd_len(mt); string s; rep(fuga, len) s += '0' + rnd_col(mt); ssB.push_back(s); } uniq(ssT); uniq(ssB); // (i,j) 成分が naive(ss[i] + ss[j]) であるような行列 mat を得る. int LT = sz(ssT), LB = sz(ssB); Matrix<mint> mat(LT, LB); rep(i, LT) rep(j, LB) mat[i][j] = naive(ssT[i] + ssB[j]); // mat に対して行基本変形を行いピボット位置のリスト piv を得る. auto piv = row_reduced_form(mat); // ランク上昇に寄与した文字列だけ残す. vector<string> nssT, nssB; for (auto [i, j] : piv) { nssT.push_back(ssT[i]); nssB.push_back(ssB[j]); } ssT = move(nssT); ssB = move(nssB); } // (i,j) 成分が naive(ss[i] + ss[j]) であるような行列 mat を得る. int LT = sz(ssT), LB = sz(ssB); Matrix<mint> mat(LT, LB); rep(i, LT) rep(j, LB) mat[i][j] = naive(ssT[i] + ssB[j]); // mat に対して行基本変形を行いピボット位置のリスト piv を得る. auto piv = row_reduced_form(mat); int DIM = sz(piv); // 選択した行 is と列 js を並べ直して 0 始まりにする. vi is(DIM), js(DIM); rep(r, DIM) tie(is[r], js[r]) = piv[r]; repi(i, 1, DIM - 1) if (ssT[is[i]] == "") swap(is[i], is[0]); repi(j, 1, DIM - 1) if (ssB[js[j]] == "") swap(js[j], js[0]); assert(is[0] == 0); assert(js[0] == 0); // 基底の変換行列 P を得る. Matrix<mint> matP(DIM, DIM); rep(i, DIM) rep(j, DIM) matP[i][j] = naive(ssT[is[i]] + ssB[js[j]]); // P の逆行列 P_inv を得る. auto matP_inv = inverse_matrix(matP); // 各文字に対応する表現行列を得る. vector<Matrix<mint>> matAs(COL, Matrix<mint>(DIM, DIM)); rep(k, COL) { char c = '0' + k; rep(i, DIM) rep(j, DIM) matAs[k][i][j] = naive(ssT[is[i]] + c + ssB[js[j]]); matAs[k] = matAs[k] * matP_inv; } // 右端を閉じるためのベクトルを得る. vm vecP(DIM); rep(i, DIM) vecP[i] = matP[i][0]; // スパース埋め込み用の文字列を出力する. vector<tuple<int, int, mint>> elems; vi offsets{ 0 }; rep(c, COL) { rep(i, DIM) rep(j, DIM) { if (matAs[c][i][j] != 0) elems.emplace_back(i, j, matAs[c][i][j]); } offsets.push_back(sz(elems)); } auto to_signed_string = [](mint x) { int v = x.val(); int mod = mint::mod(); if (2 * v > mod) v -= mod; return to_string(v); }; string eb = "constexpr int DIM = "; eb += to_string(DIM); eb += ";\n"; eb += "constexpr int COL = "; eb += to_string(COL); eb += ";\n"; eb += "tuple<int, int, VTYPE> matAs[] = {"; for (auto [i, j, v] : elems) { eb += "{"; eb += to_string(i); eb += ","; eb += to_string(j); eb += ","; eb += to_signed_string(v); eb += "},"; } eb.pop_back(); eb += "};\n"; eb += "int offset[COL + 1] = {"; repi(c, 0, COL) eb += to_string(offsets[c]) + ","; eb.pop_back(); eb += "};\n"; eb += "VTYPE vecP[DIM] = {"; rep(i, DIM) eb += to_signed_string(vecP[i]) + ","; eb.pop_back(); eb += "};\n"; cout << eb; exit(0); } template <class VTYPE> vector<VTYPE> solve1(const string& s) { // --------------- embed_coefs() からの出力を貼る ---------------- constexpr int DIM = 3; constexpr int COL = 2; tuple<int, int, VTYPE> matAs[] = { {0,0,1},{1,2,1},{2,2,1},{0,1,1},{1,1,1} }; int offset[COL + 1] = { 0,3,5 }; VTYPE vecP[DIM] = { 1,1,1 }; // -------------------------------------------------------------- int n = sz(s); array<VTYPE, DIM> dp; dp.fill(0); dp[0] = 1; vector<VTYPE> res(n + 1); res[0] = vecP[0]; auto apply = [&](const array<VTYPE, DIM>& x, int col) { array<VTYPE, DIM> z; z.fill(0); repi(pt, offset[0], offset[COL] - 1) { // 重ね合わせ auto [i, j, v] = matAs[pt]; z[j] += x[i] * v; } return z; }; rep(p, n) { dp = apply(dp, s[p] - '0'); dump(dp); rep(i, DIM) res[p + 1] += dp[i] * vecP[i]; } return res; } template <class VTYPE> vector<VTYPE> solve2(const string& s) { // --------------- embed_coefs() からの出力を貼る ---------------- constexpr int DIM = 5; constexpr int COL = 4; tuple<int, int, VTYPE> matAs[] = { {0,0,1},{1,2,1},{2,2,1},{3,2,1},{4,2,1},{0,4,1},{3,4,1},{4,4,1},{0,1,1},{1,1,1},{3,1,1},{0,3,1},{1,3,1},{3,3,1},{4,3,1} }; int offset[COL + 1] = { 0,5,8,11,15 }; VTYPE vecP[DIM] = { 1,1,1,1,1 }; // -------------------------------------------------------------- int n = sz(s); array<VTYPE, DIM> dp; dp.fill(0); dp[0] = 1; vector<VTYPE> res(n + 1); res[0] = vecP[0]; auto apply = [&](const array<VTYPE, DIM>& x, int col) { array<VTYPE, DIM> z; z.fill(0); repi(pt, offset[0], offset[COL] - 1) { // 重ね合わせ auto [i, j, v] = matAs[pt]; z[j] += x[i] * v; } return z; }; rep(p, n) { dp = apply(dp, s[p] - '0'); rep(i, DIM) res[p + 1] += dp[i] * vecP[i]; } return res; } template <class VTYPE> vector<VTYPE> solve3(const string& s) { // --------------- embed_coefs() からの出力を貼る ---------------- constexpr int DIM = 10; constexpr int COL = 8; tuple<int, int, VTYPE> matAs[] = { {0,0,1},{1,6,1},{2,6,1},{3,6,1},{4,6,1},{5,6,1},{6,6,1},{7,6,1},{9,6,1},{0,3,1},{2,3,1},{3,3,1},{4,3,1},{9,3,1},{0,1,1},{1,1,1},{2,1,1},{4,1,1},{7,1,1},{0,4,1},{1,4,1},{2,4,1},{3,4,1},{4,4,1},{7,4,1},{9,4,1},{0,5,1},{2,5,1},{5,5,1},{7,5,1},{9,5,1},{0,8,1},{2,9,1},{3,8,1},{4,8,1},{5,8,1},{7,8,1},{8,8,1},{9,9,1},{0,7,1},{1,7,1},{2,7,1},{4,7,1},{5,7,1},{7,7,1},{9,7,1},{0,2,1},{1,2,1},{2,2,1},{3,2,1},{4,2,1},{5,2,1},{7,2,1},{8,2,1},{9,2,1} }; int offset[COL + 1] = { 0,9,14,19,26,31,39,46,55 }; VTYPE vecP[DIM] = { 1,1,1,1,1,1,1,1,0,1 }; // -------------------------------------------------------------- int n = sz(s); array<VTYPE, DIM> dp; dp.fill(0); dp[0] = 1; vector<VTYPE> res(n + 1); res[0] = vecP[0]; auto apply = [&](const array<VTYPE, DIM>& x, int col) { array<VTYPE, DIM> z; z.fill(0); repi(pt, offset[0], offset[COL] - 1) { // 重ね合わせ auto [i, j, v] = matAs[pt]; z[j] += x[i] * v; } return z; }; rep(p, n) { dp = apply(dp, s[p] - '0'); rep(i, DIM) res[p + 1] += dp[i] * vecP[i]; } return res; } template <class VTYPE> vector<VTYPE> solve4(const string& s) { // --------------- embed_coefs() からの出力を貼る ---------------- constexpr int DIM = 22; constexpr int COL = 16; tuple<int, int, VTYPE> matAs[] = { {0,0,1},{2,14,1},{3,14,1},{4,14,1},{5,14,1},{6,14,1},{7,14,1},{9,14,1},{10,14,1},{11,14,1},{13,14,1},{14,14,1},{16,14,1},{18,14,1},{19,14,1},{20,14,1},{21,14,1},{0,3,1},{3,3,1},{4,3,1},{7,3,1},{10,3,1},{11,3,1},{16,3,1},{18,3,1},{20,3,1},{0,13,1},{2,13,1},{4,13,1},{6,13,1},{7,13,1},{11,13,1},{13,13,1},{18,13,1},{21,13,1},{0,18,1},{2,18,1},{3,18,1},{4,18,1},{6,18,1},{7,18,1},{10,18,1},{11,18,1},{13,18,1},{16,18,1},{18,18,1},{20,18,1},{21,18,1},{0,5,1},{2,5,1},{5,5,1},{6,5,1},{7,5,1},{9,5,1},{10,5,1},{11,5,1},{16,5,1},{0,12,1},{1,12,1},{2,12,1},{3,12,1},{4,12,1},{5,12,1},{6,12,1},{7,10,1},{9,12,1},{10,10,1},{11,10,1},{12,12,1},{16,10,1},{18,12,1},{20,12,1},{0,6,1},{2,6,1},{4,6,1},{5,6,1},{6,6,1},{7,6,1},{9,6,1},{10,6,1},{11,6,1},{13,6,1},{16,6,1},{18,6,1},{21,6,1},{0,7,1},{1,7,1},{2,7,1},{3,7,1},{4,7,1},{5,7,1},{6,7,1},{7,7,1},{9,7,1},{10,7,1},{11,7,1},{12,7,1},{13,7,1},{16,7,1},{18,7,1},{20,7,1},{21,7,1},{0,19,1},{2,19,1},{4,19,1},{9,19,1},{11,19,1},{16,19,1},{19,19,1},{20,19,1},{21,19,1},{0,15,1},{1,15,1},{2,15,1},{3,15,1},{4,20,1},{7,15,1},{9,15,1},{10,15,1},{11,20,1},{15,15,1},{16,20,1},{17,15,1},{18,15,1},{19,15,1},{20,20,1},{21,15,1},{0,8,1},{2,21,1},{4,21,1},{6,8,1},{7,8,1},{8,8,1},{9,8,1},{11,21,1},{13,8,1},{16,8,1},{17,8,1},{18,8,1},{19,8,1},{20,8,1},{21,21,1},{0,17,1},{1,17,1},{2,4,1},{3,17,1},{4,4,1},{6,17,1},{7,17,1},{8,17,1},{9,17,1},{10,17,1},{11,4,1},{13,17,1},{15,17,1},{16,4,1},{17,17,1},{18,17,1},{19,17,1},{20,4,1},{21,4,1},{0,9,1},{2,9,1},{4,9,1},{5,9,1},{6,9,1},{7,9,1},{9,9,1},{10,9,1},{11,9,1},{16,9,1},{19,9,1},{20,9,1},{21,9,1},{0,1,1},{1,1,1},{2,1,1},{3,1,1},{4,16,1},{5,1,1},{6,1,1},{7,16,1},{9,1,1},{10,16,1},{11,16,1},{12,1,1},{15,1,1},{16,16,1},{17,1,1},{18,1,1},{19,1,1},{20,16,1},{21,1,1},{0,2,1},{2,2,1},{4,2,1},{5,2,1},{6,2,1},{7,2,1},{8,2,1},{9,2,1},{10,2,1},{11,2,1},{13,2,1},{16,2,1},{17,2,1},{18,2,1},{19,2,1},{20,2,1},{21,2,1},{0,11,1},{1,11,1},{2,11,1},{3,11,1},{4,11,1},{5,11,1},{6,11,1},{7,11,1},{8,11,1},{9,11,1},{10,11,1},{11,11,1},{12,11,1},{13,11,1},{15,11,1},{16,11,1},{17,11,1},{18,11,1},{19,11,1},{20,11,1},{21,11,1} }; int offset[COL + 1] = { 0,17,26,35,48,57,72,85,102,111,127,142,161,174,193,210,231 }; VTYPE vecP[DIM] = { 1,0,1,1,1,1,1,1,0,1,1,1,0,1,1,0,1,0,1,1,1,1 }; // -------------------------------------------------------------- int n = sz(s); array<VTYPE, DIM> dp; dp.fill(0); dp[0] = 1; vector<VTYPE> res(n + 1); res[0] = vecP[0]; auto apply = [&](const array<VTYPE, DIM>& x, int col) { array<VTYPE, DIM> z; z.fill(0); repi(pt, offset[0], offset[COL] - 1) { // 重ね合わせ auto [i, j, v] = matAs[pt]; z[j] += x[i] * v; } return z; }; rep(p, n) { dp = apply(dp, s[p] - '0'); rep(i, DIM) res[p + 1] += dp[i] * vecP[i]; } return res; } template <class VTYPE> vector<VTYPE> solve5(const string& s) { // --------------- embed_coefs() からの出力を貼る ---------------- constexpr int DIM = 52; constexpr int COL = 32; tuple<int, int, VTYPE> matAs[] = { {0,0,1},{2,12,1},{3,12,1},{4,12,1},{5,12,1},{9,12,1},{11,12,1},{12,12,1},{13,12,1},{19,12,1},{20,12,1},{21,12,1},{22,12,1},{23,12,1},{24,12,1},{25,12,1},{27,12,1},{31,12,1},{32,12,1},{34,12,1},{35,12,1},{36,12,1},{39,12,1},{40,12,1},{42,12,1},{43,12,1},{44,12,1},{45,12,1},{46,12,1},{47,12,1},{48,12,1},{50,12,1},{51,12,1},{0,11,1},{2,11,1},{11,11,1},{19,11,1},{22,11,1},{23,11,1},{32,11,1},{34,11,1},{35,11,1},{36,11,1},{39,11,1},{40,11,1},{43,11,1},{46,11,1},{48,11,1},{50,11,1},{51,11,1},{0,20,1},{4,20,1},{19,20,1},{20,20,1},{21,20,1},{22,20,1},{23,20,1},{25,20,1},{27,20,1},{32,20,1},{34,20,1},{36,20,1},{40,20,1},{44,20,1},{45,20,1},{46,20,1},{47,20,1},{0,22,1},{2,22,1},{4,22,1},{11,22,1},{19,22,1},{20,22,1},{21,22,1},{22,22,1},{23,22,1},{25,22,1},{27,22,1},{32,22,1},{34,22,1},{35,22,1},{36,22,1},{39,22,1},{40,22,1},{43,22,1},{44,22,1},{45,22,1},{46,22,1},{47,22,1},{48,22,1},{50,22,1},{51,22,1},{0,13,1},{2,13,1},{4,13,1},{5,13,1},{9,13,1},{13,13,1},{19,13,1},{21,13,1},{24,13,1},{25,13,1},{27,13,1},{32,13,1},{34,13,1},{35,13,1},{36,13,1},{43,13,1},{48,13,1},{0,30,1},{2,35,1},{4,30,1},{5,30,1},{9,30,1},{10,30,1},{11,30,1},{13,30,1},{16,30,1},{19,35,1},{21,30,1},{22,30,1},{23,30,1},{24,30,1},{25,30,1},{26,30,1},{27,30,1},{30,30,1},{32,35,1},{34,35,1},{35,35,1},{36,35,1},{39,30,1},{40,30,1},{43,35,1},{46,30,1},{48,35,1},{49,30,1},{50,30,1},{51,30,1},{0,25,1},{2,25,1},{4,25,1},{5,25,1},{9,25,1},{13,25,1},{19,25,1},{20,25,1},{21,25,1},{22,25,1},{23,25,1},{24,25,1},{25,25,1},{27,25,1},{32,25,1},{34,25,1},{35,25,1},{36,25,1},{40,25,1},{43,25,1},{44,25,1},{45,25,1},{46,25,1},{47,25,1},{48,25,1},{0,34,1},{2,34,1},{4,34,1},{5,34,1},{9,34,1},{10,34,1},{11,34,1},{13,34,1},{16,34,1},{19,34,1},{20,34,1},{21,34,1},{22,34,1},{23,34,1},{24,34,1},{25,34,1},{26,34,1},{27,34,1},{30,34,1},{32,34,1},{34,34,1},{35,34,1},{36,34,1},{39,34,1},{40,34,1},{43,34,1},{44,34,1},{45,34,1},{46,34,1},{47,34,1},{48,34,1},{49,34,1},{50,34,1},{51,34,1},{0,3,1},{2,3,1},{3,3,1},{4,3,1},{5,3,1},{9,3,1},{19,3,1},{21,3,1},{23,3,1},{31,3,1},{36,3,1},{39,3,1},{40,3,1},{43,3,1},{44,3,1},{47,3,1},{50,3,1},{0,29,1},{2,39,1},{3,29,1},{4,29,1},{5,29,1},{8,29,1},{9,29,1},{11,29,1},{14,29,1},{16,29,1},{18,29,1},{19,39,1},{21,29,1},{22,29,1},{23,39,1},{26,29,1},{29,29,1},{31,29,1},{32,29,1},{34,29,1},{35,29,1},{36,39,1},{39,39,1},{40,39,1},{43,39,1},{44,29,1},{46,29,1},{47,29,1},{48,29,1},{50,39,1},{51,29,1},{0,33,1},{2,33,1},{3,33,1},{4,44,1},{5,33,1},{8,33,1},{9,33,1},{14,33,1},{19,44,1},{20,33,1},{21,44,1},{22,33,1},{23,44,1},{25,33,1},{27,33,1},{28,33,1},{31,33,1},{32,33,1},{33,33,1},{34,33,1},{36,44,1},{39,33,1},{40,44,1},{43,33,1},{44,44,1},{45,33,1},{46,33,1},{47,44,1},{50,33,1},{0,14,1},{2,40,1},{3,14,1},{4,40,1},{5,14,1},{8,14,1},{9,14,1},{11,14,1},{14,14,1},{16,14,1},{18,14,1},{19,40,1},{20,14,1},{21,40,1},{22,14,1},{23,40,1},{25,14,1},{26,14,1},{27,14,1},{28,14,1},{29,14,1},{31,14,1},{32,14,1},{33,14,1},{34,14,1},{35,14,1},{36,40,1},{39,40,1},{40,40,1},{43,40,1},{44,40,1},{45,14,1},{46,14,1},{47,40,1},{48,14,1},{50,40,1},{51,14,1},{0,5,1},{2,5,1},{3,5,1},{4,5,1},{5,5,1},{9,5,1},{13,5,1},{19,5,1},{21,5,1},{23,5,1},{24,5,1},{25,5,1},{27,5,1},{31,5,1},{32,5,1},{34,5,1},{35,5,1},{36,5,1},{39,5,1},{40,5,1},{43,5,1},{44,5,1},{47,5,1},{48,5,1},{50,5,1},{0,26,1},{2,43,1},{3,26,1},{4,26,1},{5,26,1},{8,26,1},{9,26,1},{10,26,1},{11,26,1},{13,26,1},{14,26,1},{16,26,1},{18,26,1},{19,43,1},{21,26,1},{22,26,1},{23,43,1},{24,26,1},{25,26,1},{26,26,1},{27,26,1},{29,26,1},{30,26,1},{31,26,1},{32,43,1},{34,43,1},{35,43,1},{36,43,1},{39,43,1},{40,43,1},{43,43,1},{44,26,1},{46,26,1},{47,26,1},{48,43,1},{49,26,1},{50,43,1},{51,26,1},{0,4,1},{2,4,1},{3,4,1},{4,4,1},{5,4,1},{8,4,1},{9,4,1},{13,4,1},{14,4,1},{19,4,1},{20,4,1},{21,4,1},{22,4,1},{23,4,1},{24,4,1},{25,4,1},{27,4,1},{28,4,1},{31,4,1},{32,4,1},{33,4,1},{34,4,1},{35,4,1},{36,4,1},{39,4,1},{40,4,1},{43,4,1},{44,4,1},{45,4,1},{46,4,1},{47,4,1},{48,4,1},{50,4,1},{0,36,1},{2,36,1},{3,36,1},{4,36,1},{5,36,1},{8,36,1},{9,36,1},{10,36,1},{11,36,1},{13,36,1},{14,36,1},{16,36,1},{18,36,1},{19,36,1},{20,36,1},{21,36,1},{22,36,1},{23,36,1},{24,36,1},{25,36,1},{26,36,1},{27,36,1},{28,36,1},{29,36,1},{30,36,1},{31,36,1},{32,36,1},{33,36,1},{34,36,1},{35,36,1},{36,36,1},{39,36,1},{40,36,1},{43,36,1},{44,36,1},{45,36,1},{46,36,1},{47,36,1},{48,36,1},{49,36,1},{50,36,1},{51,36,1},{0,42,1},{2,42,1},{9,42,1},{19,42,1},{21,42,1},{23,42,1},{24,42,1},{27,42,1},{31,42,1},{32,42,1},{42,42,1},{45,42,1},{46,42,1},{47,42,1},{48,42,1},{50,42,1},{51,42,1},{0,38,1},{1,38,1},{2,51,1},{8,38,1},{9,38,1},{10,38,1},{11,38,1},{15,38,1},{16,38,1},{18,38,1},{19,51,1},{21,38,1},{22,38,1},{23,51,1},{24,38,1},{27,38,1},{31,38,1},{32,51,1},{34,38,1},{35,38,1},{36,38,1},{37,38,1},{38,38,1},{39,38,1},{40,38,1},{42,38,1},{43,38,1},{45,38,1},{46,51,1},{47,38,1},{48,51,1},{50,51,1},{51,51,1},{0,41,1},{1,41,1},{2,41,1},{4,41,1},{7,41,1},{8,41,1},{9,41,1},{15,41,1},{19,45,1},{20,41,1},{21,45,1},{22,41,1},{23,45,1},{24,41,1},{25,41,1},{27,45,1},{28,41,1},{31,41,1},{32,45,1},{34,41,1},{36,41,1},{40,41,1},{41,41,1},{42,41,1},{44,41,1},{45,45,1},{46,45,1},{47,45,1},{48,41,1},{50,41,1},{51,41,1},{0,15,1},{1,15,1},{2,46,1},{4,15,1},{7,15,1},{8,15,1},{9,15,1},{10,15,1},{11,15,1},{15,15,1},{16,15,1},{18,15,1},{19,46,1},{20,15,1},{21,46,1},{22,15,1},{23,46,1},{24,15,1},{25,15,1},{27,46,1},{28,15,1},{31,15,1},{32,46,1},{34,15,1},{35,15,1},{36,15,1},{37,15,1},{38,15,1},{39,15,1},{40,15,1},{41,15,1},{42,15,1},{43,15,1},{44,15,1},{45,46,1},{46,46,1},{47,46,1},{48,46,1},{50,46,1},{51,46,1},{0,6,1},{1,6,1},{2,24,1},{4,6,1},{5,6,1},{6,6,1},{7,6,1},{9,24,1},{13,6,1},{19,24,1},{21,24,1},{23,6,1},{24,24,1},{25,6,1},{27,24,1},{31,6,1},{32,24,1},{34,6,1},{35,6,1},{36,6,1},{37,6,1},{42,6,1},{43,6,1},{45,6,1},{46,6,1},{47,6,1},{48,24,1},{49,6,1},{50,6,1},{51,6,1},{0,17,1},{1,37,1},{2,48,1},{4,17,1},{5,17,1},{6,17,1},{7,17,1},{8,17,1},{9,10,1},{10,10,1},{11,17,1},{13,17,1},{15,17,1},{16,10,1},{17,17,1},{18,17,1},{19,48,1},{21,10,1},{22,17,1},{23,49,1},{24,10,1},{25,17,1},{26,17,1},{27,10,1},{30,17,1},{31,17,1},{32,48,1},{34,37,1},{35,37,1},{36,37,1},{37,37,1},{38,17,1},{39,17,1},{40,17,1},{42,17,1},{43,37,1},{45,17,1},{46,49,1},{47,17,1},{48,48,1},{49,49,1},{50,49,1},{51,49,1},{0,7,1},{1,7,1},{2,27,1},{4,7,1},{5,7,1},{6,7,1},{7,7,1},{8,7,1},{9,27,1},{13,7,1},{15,7,1},{19,27,1},{20,7,1},{21,27,1},{22,7,1},{23,27,1},{24,27,1},{25,7,1},{27,27,1},{28,7,1},{31,7,1},{32,27,1},{34,7,1},{35,7,1},{36,7,1},{37,7,1},{40,7,1},{41,7,1},{42,7,1},{43,7,1},{44,7,1},{45,27,1},{46,27,1},{47,27,1},{48,27,1},{49,7,1},{50,7,1},{51,7,1},{0,1,1},{1,1,1},{2,32,1},{4,1,1},{5,1,1},{6,1,1},{7,1,1},{8,1,1},{9,32,1},{10,32,1},{11,1,1},{13,1,1},{15,1,1},{16,32,1},{17,1,1},{18,1,1},{19,32,1},{20,1,1},{21,32,1},{22,1,1},{23,32,1},{24,32,1},{25,1,1},{26,1,1},{27,32,1},{28,1,1},{30,1,1},{31,1,1},{32,32,1},{34,1,1},{35,1,1},{36,1,1},{37,1,1},{38,1,1},{39,1,1},{40,1,1},{41,1,1},{42,1,1},{43,1,1},{44,1,1},{45,32,1},{46,32,1},{47,32,1},{48,32,1},{49,32,1},{50,32,1},{51,32,1},{0,31,1},{2,31,1},{3,31,1},{4,31,1},{5,31,1},{9,31,1},{19,31,1},{21,31,1},{23,31,1},{24,31,1},{27,31,1},{31,31,1},{32,31,1},{36,31,1},{39,31,1},{40,31,1},{42,31,1},{43,31,1},{44,31,1},{45,31,1},{46,31,1},{47,31,1},{48,31,1},{50,31,1},{51,31,1},{0,18,1},{1,18,1},{2,50,1},{3,18,1},{4,18,1},{5,18,1},{8,18,1},{9,18,1},{10,18,1},{11,18,1},{14,18,1},{15,18,1},{16,18,1},{18,18,1},{19,50,1},{21,18,1},{22,18,1},{23,50,1},{24,18,1},{26,18,1},{27,18,1},{29,18,1},{31,18,1},{32,50,1},{34,18,1},{35,18,1},{36,50,1},{37,18,1},{38,18,1},{39,50,1},{40,50,1},{42,18,1},{43,50,1},{44,18,1},{45,18,1},{46,50,1},{47,18,1},{48,50,1},{50,50,1},{51,50,1},{0,28,1},{1,28,1},{2,28,1},{3,28,1},{4,47,1},{5,28,1},{7,28,1},{8,28,1},{9,28,1},{14,28,1},{15,28,1},{19,47,1},{20,28,1},{21,47,1},{22,28,1},{23,47,1},{24,28,1},{25,28,1},{27,47,1},{28,28,1},{31,28,1},{32,47,1},{33,28,1},{34,28,1},{36,47,1},{39,28,1},{40,47,1},{41,28,1},{42,28,1},{43,28,1},{44,47,1},{45,47,1},{46,47,1},{47,47,1},{48,28,1},{50,28,1},{51,28,1},{0,8,1},{1,8,1},{2,23,1},{3,8,1},{4,23,1},{5,8,1},{7,8,1},{8,8,1},{9,8,1},{10,8,1},{11,8,1},{14,8,1},{15,8,1},{16,8,1},{18,8,1},{19,23,1},{20,8,1},{21,23,1},{22,8,1},{23,23,1},{24,8,1},{25,8,1},{26,8,1},{27,23,1},{28,8,1},{29,8,1},{31,8,1},{32,23,1},{33,8,1},{34,8,1},{35,8,1},{36,23,1},{37,8,1},{38,8,1},{39,23,1},{40,23,1},{41,8,1},{42,8,1},{43,23,1},{44,23,1},{45,23,1},{46,23,1},{47,23,1},{48,23,1},{50,23,1},{51,23,1},{0,9,1},{1,9,1},{2,9,1},{3,9,1},{4,9,1},{5,9,1},{6,9,1},{7,9,1},{9,9,1},{13,9,1},{19,9,1},{21,9,1},{23,9,1},{24,9,1},{25,9,1},{27,9,1},{31,9,1},{32,9,1},{34,9,1},{35,9,1},{36,9,1},{37,9,1},{39,9,1},{40,9,1},{42,9,1},{43,9,1},{44,9,1},{45,9,1},{46,9,1},{47,9,1},{48,9,1},{49,9,1},{50,9,1},{51,9,1},{0,16,1},{1,2,1},{2,2,1},{3,16,1},{4,16,1},{5,16,1},{6,16,1},{7,16,1},{8,16,1},{9,16,1},{10,16,1},{11,16,1},{13,16,1},{14,16,1},{15,16,1},{16,16,1},{17,16,1},{18,16,1},{19,2,1},{21,16,1},{22,16,1},{23,2,1},{24,16,1},{25,16,1},{26,16,1},{27,16,1},{29,16,1},{30,16,1},{31,16,1},{32,2,1},{34,2,1},{35,2,1},{36,2,1},{37,2,1},{38,16,1},{39,2,1},{40,2,1},{42,16,1},{43,2,1},{44,16,1},{45,16,1},{46,2,1},{47,16,1},{48,2,1},{49,2,1},{50,2,1},{51,2,1},{0,21,1},{1,21,1},{2,21,1},{3,21,1},{4,21,1},{5,21,1},{6,21,1},{7,21,1},{8,21,1},{9,21,1},{13,21,1},{14,21,1},{15,21,1},{19,21,1},{20,21,1},{21,21,1},{22,21,1},{23,21,1},{24,21,1},{25,21,1},{27,21,1},{28,21,1},{31,21,1},{32,21,1},{33,21,1},{34,21,1},{35,21,1},{36,21,1},{37,21,1},{39,21,1},{40,21,1},{41,21,1},{42,21,1},{43,21,1},{44,21,1},{45,21,1},{46,21,1},{47,21,1},{48,21,1},{49,21,1},{50,21,1},{51,21,1},{0,19,1},{1,19,1},{2,19,1},{3,19,1},{4,19,1},{5,19,1},{6,19,1},{7,19,1},{8,19,1},{9,19,1},{10,19,1},{11,19,1},{13,19,1},{14,19,1},{15,19,1},{16,19,1},{17,19,1},{18,19,1},{19,19,1},{20,19,1},{21,19,1},{22,19,1},{23,19,1},{24,19,1},{25,19,1},{26,19,1},{27,19,1},{28,19,1},{29,19,1},{30,19,1},{31,19,1},{32,19,1},{33,19,1},{34,19,1},{35,19,1},{36,19,1},{37,19,1},{38,19,1},{39,19,1},{40,19,1},{41,19,1},{42,19,1},{43,19,1},{44,19,1},{45,19,1},{46,19,1},{47,19,1},{48,19,1},{49,19,1},{50,19,1},{51,19,1} }; int offset[COL + 1] = { 0,33,50,67,92,109,139,164,198,215,246,275,312,337,375,408,450,467,500,531,571,601,644,682,729,754,794,831,877,911,958,1000,1051 }; VTYPE vecP[DIM] = { 1,0,1,1,1,1,0,0,0,1,0,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,1,0,0,0,1,1,0,1,1,1,0,0,1,1,0,1,1,1,1,1,1,1,0,1,1 }; // -------------------------------------------------------------- int n = sz(s); array<VTYPE, DIM> dp; dp.fill(0); dp[0] = 1; vector<VTYPE> res(n + 1); res[0] = vecP[0]; auto apply = [&](const array<VTYPE, DIM>& x, int col) { array<VTYPE, DIM> z; z.fill(0); repi(pt, offset[0], offset[COL] - 1) { // 重ね合わせ auto [i, j, v] = matAs[pt]; z[j] += x[i] * v; } return z; }; rep(p, n) { dp = apply(dp, s[p] - '0'); rep(i, DIM) res[p + 1] += dp[i] * vecP[i]; } return res; } //【線形漸化式の発見】O(n^2) /* * 与えられた数列 a[0..n) に対し,以下の等式を満たす c[0..m) で m を最小とするものを返す: * a[i] = Σj∈[0..m) c[j] a[i-1-j] (∀i∈[m..n)) * * 制約 : mint::mod は大きい素数 */ vm berlekamp_massey(const vm& a) { // 参考 : https://en.wikipedia.org/wiki/Berlekamp%E2%80%93Massey_algorithm // verify : https://judge.yosupo.jp/problem/find_linear_recurrence vm S(a), C{ 1 }, B{ 1 }; int N = sz(a), m = 1; mint b = 1; rep(n, N) { mint d = 0; rep(i, sz(C)) d += C[i] * S[n - i]; if (d == 0) { m++; } else if (2 * (sz(C) - 1) <= n) { vm T(C); mint coef = d * b.inv(); C.resize(max(sz(C), sz(B) + m)); rep(j, sz(B)) C[j + m] -= coef * B[j]; B = T; b = d; m = 1; } else { mint coef = d * b.inv(); C.resize(max(sz(C), sz(B) + m)); rep(j, sz(B)) C[j + m] -= coef * B[j]; m++; } } C.erase(C.begin()); rep(i, sz(C)) C[i] *= -1; return C; } //【畳込み(素朴)】O(n m) /* * a[0..n) と b[0..m) を畳み込んだ数列 c[0..n+m-1) を返す. * すなわち c[k] = Σ_(i+j=k) a[i] b[j] である. */ template <class T> vector<T> naive_convolution(const vector<T>& a, const vector<T>& b) { // verify : https://atcoder.jp/contests/abc214/tasks/abc214_g int n = sz(a), m = sz(b); if (n == 0 || m == 0) return vector<T>(); // c[k] = Σ_(i+j=k) a[i] b[j] vector<T> c(n + m - 1); if (n < m) { rep(i, n) rep(j, m) c[i + j] += a[i] * b[j]; } else { rep(j, m) rep(i, n) c[i + j] += a[i] * b[j]; } return c; } //【形式的冪級数】 /* * MFPS() : O(1) * 零多項式 f = 0 で初期化する. * * MFPS(mint c0) : O(1) * 定数多項式 f = c0 で初期化する. * * MFPS(mint c0, int n) : O(n) * n 次未満の項をもつ定数多項式 f = c0 で初期化する. * * MFPS(vm c) : O(n) * f(z) = c[0] + c[1] z + ... + c[n - 1] z^(n-1) で初期化する. * * set_conv(vm(*CONV)(const vm&, const vm&)) : O(1) * 畳込み用の関数を CONV に設定する. * * c + f, f + c : O(1) f + g : O(n) * f - c : O(1) c - f, f - g, -f : O(n) * c * f, f * c : O(n) f * g : O(n log n) f * g_sp : O(n |g|) * f / c : O(n) f / g : O(n log n) f / g_sp : O(n |g|) * 形式的冪級数としての和,差,積,商の結果を返す. * g_sp はスパース多項式であり,{次数, 係数} の次数昇順の組の vector で表す. * 制約 : 商では g(0) != 0 * * MFPS f.inv(int d) : O(n log n) * 1 / f mod z^d を返す. * 制約 : f(0) != 0 * * MFPS f.quotient(MFPS g) : O(n log n) * MFPS f.reminder(MFPS g) : O(n log n) * pair<MFPS, MFPS> f.quotient_remainder(MFPS g) : O(n log n) * 多項式としての f を g で割った商,余り,商と余りの組を返す. * 制約 : g の最高次の係数は 0 でない * * int f.deg(), int f.size() : O(1) * 多項式 f の次数[項数]を返す. * * MFPS::monomial(int d, mint c = 1) : O(d) * 単項式 c z^d を返す. * * mint f.assign(mint c) : O(n) * 多項式 f の不定元 z に c を代入した値を返す. * * f.resize(int d) : O(1) * mod z^d をとる. * * f.resize() : O(n) * 不要な高次の項を削る. * * f >> d, f << d : O(n) * 係数列を d だけ右[左]シフトした多項式を返す. * (右シフトは z^d の乗算,左シフトは z^d で割った商と等価) * * f.push_back(c) : O(1) * 最高次の係数として c を追加する. */ struct MFPS { using SMFPS = vector<pim>; int n; // 係数の個数(次数 + 1) vm c; // 係数列 inline static vm(*CONV)(const vm&, const vm&) = convolution; // 畳込み用の関数 // コンストラクタ(0,定数,係数列で初期化) MFPS() : n(0) {} MFPS(mint c0) : n(1), c({ c0 }) {} MFPS(int c0) : n(1), c({ mint(c0) }) {} MFPS(mint c0, int d) : n(d), c(n) { if (n > 0) c[0] = c0; } MFPS(int c0, int d) : n(d), c(n) { if (n > 0) c[0] = c0; } MFPS(const vm& c_) : n(sz(c_)), c(c_) {} MFPS(const vi& c_) : n(sz(c_)), c(n) { rep(i, n) c[i] = c_[i]; } // 代入 MFPS(const MFPS& f) = default; MFPS& operator=(const MFPS& f) = default; MFPS& operator=(const mint& c0) { n = 1; c = { c0 }; return *this; } void push_back(mint cn) { c.emplace_back(cn); ++n; } void pop_back() { c.pop_back(); --n; } [[nodiscard]] mint back() { return c.back(); } // 比較 [[nodiscard]] bool operator==(const MFPS& g) const { return c == g.c; } [[nodiscard]] bool operator!=(const MFPS& g) const { return c != g.c; } // アクセス inline mint const& operator[](int i) const { return c[i]; } inline mint& operator[](int i) { return c[i]; } // 次数 [[nodiscard]] int deg() const { return n - 1; } [[nodiscard]] int size() const { return n; } static void set_conv(vm(*CONV_)(const vm&, const vm&)) { // verify : https://atcoder.jp/contests/tdpc/tasks/tdpc_fibonacci CONV = CONV_; } // 加算 MFPS& operator+=(const MFPS& g) { if (n >= g.n) rep(i, g.n) c[i] += g.c[i]; else { rep(i, n) c[i] += g.c[i]; repi(i, n, g.n - 1) c.push_back(g.c[i]); n = g.n; } return *this; } [[nodiscard]] MFPS operator+(const MFPS& g) const { return MFPS(*this) += g; } // 定数加算 MFPS& operator+=(const mint& sc) { if (n == 0) { n = 1; c = { sc }; } else { c[0] += sc; } return *this; } [[nodiscard]] MFPS operator+(const mint& sc) const { return MFPS(*this) += sc; } [[nodiscard]] friend MFPS operator+(const mint& sc, const MFPS& f) { return f + sc; } MFPS& operator+=(const int& sc) { *this += mint(sc); return *this; } [[nodiscard]] MFPS operator+(const int& sc) const { return MFPS(*this) += sc; } [[nodiscard]] friend MFPS operator+(const int& sc, const MFPS& f) { return f + sc; } // 減算 MFPS& operator-=(const MFPS& g) { if (n >= g.n) rep(i, g.n) c[i] -= g.c[i]; else { rep(i, n) c[i] -= g.c[i]; repi(i, n, g.n - 1) c.push_back(-g.c[i]); n = g.n; } return *this; } [[nodiscard]] MFPS operator-(const MFPS& g) const { return MFPS(*this) -= g; } // 定数減算 MFPS& operator-=(const mint& sc) { *this += -sc; return *this; } [[nodiscard]] MFPS operator-(const mint& sc) const { return MFPS(*this) -= sc; } [[nodiscard]] friend MFPS operator-(const mint& sc, const MFPS& f) { return -(f - sc); } MFPS& operator-=(const int& sc) { *this += -sc; return *this; } [[nodiscard]] MFPS operator-(const int& sc) const { return MFPS(*this) -= sc; } [[nodiscard]] friend MFPS operator-(const int& sc, const MFPS& f) { return -(f - sc); } // 加法逆元 [[nodiscard]] MFPS operator-() const { return MFPS(*this) *= -1; } // 定数倍 MFPS& operator*=(const mint& sc) { rep(i, n) c[i] *= sc; return *this; } [[nodiscard]] MFPS operator*(const mint& sc) const { return MFPS(*this) *= sc; } [[nodiscard]] friend MFPS operator*(const mint& sc, const MFPS& f) { return f * sc; } MFPS& operator*=(const int& sc) { *this *= mint(sc); return *this; } [[nodiscard]] MFPS operator*(const int& sc) const { return MFPS(*this) *= sc; } [[nodiscard]] friend MFPS operator*(const int& sc, const MFPS& f) { return f * sc; } // 右からの定数除算 MFPS& operator/=(const mint& sc) { *this *= sc.inv(); return *this; } [[nodiscard]] MFPS operator/(const mint& sc) const { return MFPS(*this) /= sc; } MFPS& operator/=(const int& sc) { *this /= mint(sc); return *this; } [[nodiscard]] MFPS operator/(const int& sc) const { return MFPS(*this) /= sc; } // 積 MFPS& operator*=(const MFPS& g) { c = CONV(c, g.c); n = sz(c); return *this; } [[nodiscard]] MFPS operator*(const MFPS& g) const { return MFPS(*this) *= g; } // 除算 [[nodiscard]] MFPS inv(int d) const { // 参考:https://nyaannyaan.github.io/library/fps/formal-power-series.hpp // verify : https://judge.yosupo.jp/problem/inv_of_formal_power_series //【方法】 // 1 / f mod z^d を求めることは, // f g = 1 (mod z^d) // なる g を求めることである. // この d の部分を 1, 2, 4, ..., 2^i と倍々にして求めていく. // // d = 1 のときについては // g = 1 / f[0] (mod z^1) // である. // // 次に, // g = h (mod z^k) // が求まっているとして // g mod z^(2 k) // を求める.最初の式を変形していくことで // g - h = 0 (mod z^k) // ⇒ (g - h)^2 = 0 (mod z^(2 k)) // ⇔ g^2 - 2 g h + h^2 = 0 (mod z^(2 k)) // ⇒ f g^2 - 2 f g h + f h^2 = 0 (mod z^(2 k)) // ⇔ g - 2 h + f h^2 = 0 (mod z^(2 k)) (f g = 1 (mod z^d) より) // ⇔ g = (2 - f h) h (mod z^(2 k)) // を得る. // // この手順を d ≦ 2^i となる i まで繰り返し,d 次以上の項を削除すればよい. Assert(!c.empty()); Assert(c[0] != 0); MFPS g(c[0].inv()); for (int k = 1; k < d; k <<= 1) { int len = max(min(2 * k, d), 1); MFPS tmp(0, len); rep(i, min(len, n)) tmp[i] = -c[i]; // -f tmp *= g; // -f h tmp.resize(len); tmp[0] += 2; // 2 - f h g *= tmp; // (2 - f h) h g.resize(len); } return g; } MFPS& operator/=(const MFPS& g) { return *this *= g.inv(max(n, g.n)); } [[nodiscard]] MFPS operator/(const MFPS& g) const { return MFPS(*this) /= g; } // 余り付き除算 [[nodiscard]] MFPS quotient(const MFPS& g) const { // 参考 : https://nyaannyaan.github.io/library/fps/formal-power-series.hpp // verify : https://judge.yosupo.jp/problem/division_of_polynomials //【方法】 // f(x) = g(x) q(x) + r(x) となる q(x) を求める. // f の次数は n-1, g の次数は m-1 とする.(n ≧ m) // 従って q の次数は n-m,r の次数は m-2 となる. // // f^R で f の係数列を逆順にした多項式を表す.すなわち // f^R(x) := f(1/x) x^(n-1) // である.他の多項式も同様とする. // // 最初の式で x → 1/x と置き換えると, // f(1/x) = g(1/x) q(1/x) + r(1/x) // ⇔ f(1/x) x^(n-1) = g(1/x) q(1/x) x^(n-1) + r(1/x) x^(n-1) // ⇔ f(1/x) x^(n-1) = g(1/x) x^(m-1) q(1/x) x^(n-m) + r(1/x) x^(m-2) x^(n-m+1) // ⇔ f^R(x) = g^R(x) q^R(x) + r^R(x) x^(n-m+1) // ⇒ f^R(x) = g^R(x) q^R(x) (mod x^(n-m+1)) // ⇒ q^R(x) = f^R(x) / g^R(x) (mod x^(n-m+1)) // を得る. // // これで q を mod x^(n-m+1) で正しく求めることができることになるが, // q の次数は n-m であったから,q 自身を正しく求めることができた. if (n < g.n) return MFPS(); return ((this->rev() / g.rev()).resize(n - g.n + 1)).rev(); } [[nodiscard]] MFPS reminder(const MFPS& g) const { // verify : https://judge.yosupo.jp/problem/division_of_polynomials return (*this - this->quotient(g) * g).resize(); } [[nodiscard]] pair<MFPS, MFPS> quotient_remainder(const MFPS& g) const { // verify : https://judge.yosupo.jp/problem/division_of_polynomials pair<MFPS, MFPS> res; res.first = this->quotient(g); res.second = (*this - res.first * g).resize(); return res; } // スパース積 MFPS& operator*=(const SMFPS& g) { // g の定数項だけ例外処理 auto it0 = g.begin(); mint g0 = 0; if (it0->first == 0) { g0 = it0->second; it0++; } // 後ろからインライン配る DP repir(i, n - 1, 0) { // 上位項に係数倍して配っていく. for (auto it = it0; it != g.end(); it++) { auto [j, gj] = *it; if (i + j >= n) break; c[i + j] += c[i] * gj; } // 定数項は最後に配るか消去しないといけない. c[i] *= g0; } return *this; } [[nodiscard]] MFPS operator*(const SMFPS& g) const { return MFPS(*this) *= g; } // スパース商 MFPS& operator/=(const SMFPS& g) { // g の定数項だけ例外処理 auto it0 = g.begin(); Assert(it0->first == 0 && it0->second != 0); mint g0_inv = it0->second.inv(); it0++; // 前からインライン配る DP(後ろに累積効果あり) rep(i, n) { // 定数項は最初に配らないといけない. c[i] *= g0_inv; // 上位項に係数倍して配っていく. for (auto it = it0; it != g.end(); it++) { auto [j, gj] = *it; if (i + j >= n) break; c[i + j] -= c[i] * gj; } } return *this; } [[nodiscard]] MFPS operator/(const SMFPS& g) const { return MFPS(*this) /= g; } // 係数反転 [[nodiscard]] MFPS rev() const { MFPS h = *this; reverse(all(h.c)); return h; } // 単項式 [[nodiscard]] static MFPS monomial(int d, mint coef = 1) { MFPS mono(0, d + 1); mono[d] = coef; return mono; } // 不要な高次項の除去 MFPS& resize() { // 最高次の係数が非 0 になるまで削る. while (n > 0 && c[n - 1] == 0) { c.pop_back(); n--; } return *this; } // x^d 以上の項を除去する. MFPS& resize(int d) { n = d; c.resize(d); return *this; } // 不定元への代入 [[nodiscard]] mint assign(const mint& x) const { mint val = 0; repir(i, n - 1, 0) val = val * x + c[i]; return val; } // 係数のシフト MFPS& operator>>=(int d) { n += d; c.insert(c.begin(), d, 0); return *this; } MFPS& operator<<=(int d) { n -= d; if (n <= 0) { c.clear(); n = 0; } else c.erase(c.begin(), c.begin() + d); return *this; } [[nodiscard]] MFPS operator>>(int d) const { return MFPS(*this) >>= d; } [[nodiscard]] MFPS operator<<(int d) const { return MFPS(*this) <<= d; } #ifdef _MSC_VER friend ostream& operator<<(ostream& os, const MFPS& f) { if (f.n == 0) os << 0; else { rep(i, f.n) { os << f[i] << "z^" << i; if (i < f.n - 1) os << " + "; } } return os; } #endif }; //【展開係数】O(n log n log N) /* * [z^N] f(z)/g(z) を返す. * * 制約 : deg f < deg g, g[0] ≠ 0 */ mint bostan_mori(MFPS f, MFPS g, ll N) { // 参考 : http://q.c.titech.ac.jp/docs/progs/polynomial_division.html // verify : https://judge.yosupo.jp/problem/kth_term_of_linearly_recurrent_sequence //【方法】 // 分母分子に g(-z) を掛けることにより // f(z) / g(z) = f(z) g(-z) / g(z) g(-z) // を得る.ここで g(z) g(-z) は偶多項式なので // g(z) g(-z) = e(z^2) // と表すことができる. // // 分子について // f(z) g(-z) = E(z^2) + z O(z^2) // というように偶多項式部分と奇多項式部分に分けると,N が偶数のときは // [z^N] f(z) g(-z) / g(z) g(-z) // = [z^N] E(z^2) / e(z^2) // = [z^(N/2)] E(z) / e(z) // となり,N が奇数のときは // [z^N] f(z) g(-z) / g(z) g(-z) // = [z^N] z O(z^2) / e(z^2) // = [z^((N-1)/2)] O(z) / e(z) // となる. // // これを繰り返せば N を半分ずつに減らしていくことができる. Assert(g.n >= 1 && g[0] != 0); // f(z) = 0 のときは 0 を返す. if (f.n == 0) return 0; while (N > 0) { // f2(z) = f(z) g(-z), g2(z) = g(z) g(-z) を求める. MFPS f2, g2 = g; rep(i, g2.n) if (i & 1) g2[i] *= -1; f2 = f * g2; g2 *= g; // f3(z) = E(z) or O(z), g3(z) = e(z) を求める. f.c.clear(); g.c.clear(); if (N & 1) rep(i, min<ll>(f2.n / 2, N / 2 + 1)) f.c.push_back(f2[2 * i + 1]); else rep(i, min<ll>((f2.n + 1) / 2, N / 2 + 1)) f.c.push_back(f2[2 * i]); f.n = sz(f.c); rep(i, min<ll>((g2.n + 1) / 2, N / 2 + 1)) g.c.push_back(g2[2 * i]); g.n = sz(g.c); // N を半分にして次のステップに進む. N /= 2; } // N = 0 になったら定数項を返す. return f[0] / g[0]; } //【線形漸化式】O(n log n log N) /* * 初項 a[0..n) と漸化式 a[i] = Σj∈[0..n) c[j] a[i-1-j] で定義される * 数列 a について,a[N] の値を返す. * * 利用:【展開係数】 */ mint linearly_recurrent_sequence(const vm& a, const vm& c, ll N) { // verify : https://judge.yosupo.jp/problem/kth_term_of_linearly_recurrent_sequence int n = sz(c); if (n == 0) return 0; MFPS A(a), C(c); MFPS Dnm = 1 - (C >> 1); MFPS Num = (Dnm * A).resize(n); return bostan_mori(Num, Dnm, N); } int main() { // input_from_file("input.txt"); // output_to_file("output.txt"); //【方法】 // 愚直を書いて集めたデータをもとに遷移行列を復元する. //【使い方】 // 1. mint naive(文字列) を実装する. // 2. embed_coefs(文字の種類数); を実行する. // 3. 出力を solve() 内に貼る. // 4. auto dp = solve<答えの型>(文字列) で勝手に DP してくれる. MFPS::set_conv(naive_convolution); H = 6; vector<string> ssT_ini{ "" }, ssB_ini{ "" }; // 途中から再開 //repe(a, ssT) { // string s; // repe(x, a) s += '0' + x; // ssT_ini.push_back(s); //} //repe(a, ssB) { // string s; // repe(x, a) s += '0' + x; // ssB_ini.push_back(s); //} //dump("naive:", naive("00")); //dump("naive:", naive("01")); //dump("naive:", naive("10")); //dump("naive:", naive("11")); //embed_coefs(1 << H, 7, 300, 1000, ssT_ini, ssB_ini); int n; ll m; cin >> n >> m; string s; rep(i, 300) s += '?'; vm seq; if (n == 1) seq = solve1<mint>(s); else if (n == 2) seq = solve2<mint>(s); else if (n == 3) seq = solve3<mint>(s); else if (n == 4) seq = solve4<mint>(s); else if (n == 5) seq = solve5<mint>(s); else exit(-1); // ムリ dump(seq); auto coef = berlekamp_massey(seq); dump(coef); EXIT(linearly_recurrent_sequence(seq, coef, m) - 1); }