結果
問題 |
No.3202 Periodic Alternating Subsequence
|
ユーザー |
|
提出日時 | 2025-07-19 18:46:10 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 145 ms / 2,000 ms |
コード長 | 22,722 bytes |
コンパイル時間 | 4,303 ms |
コンパイル使用メモリ | 273,804 KB |
実行使用メモリ | 7,716 KB |
最終ジャッジ日時 | 2025-07-19 18:46:18 |
合計ジャッジ時間 | 8,429 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 24 |
ソースコード
#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 T 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 // 愚直を書く. ll naive(string s) { int n = sz(s); ll res = 0; string seq; // 作成途中の列 function<void(int)> rf = [&](int i) { // 完成していれば記録する. if (i == n) { res += sz(seq) * sz(seq); return; } // 採用 if (seq.empty() || seq.back() != s[i]) { seq.push_back(s[i]); rf(i + 1); seq.pop_back(); } // 不採用 rf(i + 1); }; rf(0); return res; } //【任意列の列挙】O(m^n) /* * 集合 a[0..m) の要素からなる長さ n の列全てを格納したリストを返す. */ vector<string> enumerate_all_sequences(int n, const string& a) { // verify : https://yukicoder.me/problems/no/2329 vector<string> seqs; string seq; // 作成途中の列 seq.resize(n); // seq[i] を決める. function<void(int)> rf = [&](int i) { // seq が完成していれば記録する. if (i == n) { seqs.push_back(seq); return; } repe(x, a) { seq[i] = x; rf(i + 1); } }; rf(0); return seqs; } // 具体例を集める. void zikken() { // 整形して出力 string out; out += "data=Association["; repi(len, 1, 8) { auto seqs = enumerate_all_sequences(len, string("01")); repe(s, seqs) { ll res = naive(s); out += "{"; repe(d, s) { out += d; out += ","; } out.pop_back(); out += "}->" + to_string(res) + ","; } } out.pop_back(); out += "];"; cout << out << endl; exit(0); } /* data=Association[{}->0,{0}->1,{1}->1,{0,0}->2,{0,1}->6,{1,0}->6,{1,1}->2,{0,0,0}->3,{0,0,1}->11,{0,1,0}->20,{0,1,1}->11,{1,0,0}->11,{1,0,1}->20,{1,1,0}->11,{1,1,1}->3,{0,0,0,0}->4,{0,0,0,1}->16,{0,0,1,0}->34,{0,0,1,1}->20,{0,1,0,0}->34,{0,1,0,1}->54,{0,1,1,0}->38,{0,1,1,1}->16,{1,0,0,0}->16,{1,0,0,1}->38,{1,0,1,0}->54,{1,0,1,1}->34,{1,1,0,0}->20,{1,1,0,1}->34,{1,1,1,0}->16,{1,1,1,1}->4,{0,0,0,0,0}->5,{0,0,0,0,1}->21,{0,0,0,1,0}->48,{0,0,0,1,1}->29,{0,0,1,0,0}->57,{0,0,1,0,1}->88,{0,0,1,1,0}->65,{0,0,1,1,1}->29,{0,1,0,0,0}->48,{0,1,0,0,1}->97,{0,1,0,1,0}->131,{0,1,0,1,1}->88,{0,1,1,0,0}->65,{0,1,1,0,1}->97,{0,1,1,1,0}->56,{0,1,1,1,1}->21,{1,0,0,0,0}->21,{1,0,0,0,1}->56,{1,0,0,1,0}->97,{1,0,0,1,1}->65,{1,0,1,0,0}->88,{1,0,1,0,1}->131,{1,0,1,1,0}->97,{1,0,1,1,1}->48,{1,1,0,0,0}->29,{1,1,0,0,1}->65,{1,1,0,1,0}->88,{1,1,0,1,1}->57,{1,1,1,0,0}->29,{1,1,1,0,1}->48,{1,1,1,1,0}->21,{1,1,1,1,1}->5,{0,0,0,0,0,0}->6,{0,0,0,0,0,1}->26,{0,0,0,0,1,0}->62,{0,0,0,0,1,1}->38,{0,0,0,1,0,0}->80,{0,0,0,1,0,1}->122,{0,0,0,1,1,0}->92,{0,0,0,1,1,1}->42,{0,0,1,0,0,0}->80,{0,0,1,0,0,1}->156,{0,0,1,0,1,0}->208,{0,0,1,0,1,1}->142,{0,0,1,1,0,0}->110,{0,0,1,1,0,1}->160,{0,0,1,1,1,0}->96,{0,0,1,1,1,1}->38,{0,1,0,0,0,0}->62,{0,1,0,0,0,1}->140,{0,1,0,0,1,0}->224,{0,1,0,0,1,1}->160,{0,1,0,1,0,0}->208,{0,1,0,1,0,1}->296,{0,1,0,1,1,0}->228,{0,1,0,1,1,1}->122,{0,1,1,0,0,0}->92,{0,1,1,0,0,1}->178,{0,1,1,0,1,0}->228,{0,1,1,0,1,1}->156,{0,1,1,1,0,0}->96,{0,1,1,1,0,1}->140,{0,1,1,1,1,0}->74,{0,1,1,1,1,1}->26,{1,0,0,0,0,0}->26,{1,0,0,0,0,1}->74,{1,0,0,0,1,0}->140,{1,0,0,0,1,1}->96,{1,0,0,1,0,0}->156,{1,0,0,1,0,1}->228,{1,0,0,1,1,0}->178,{1,0,0,1,1,1}->92,{1,0,1,0,0,0}->122,{1,0,1,0,0,1}->228,{1,0,1,0,1,0}->296,{1,0,1,0,1,1}->208,{1,0,1,1,0,0}->160,{1,0,1,1,0,1}->224,{1,0,1,1,1,0}->140,{1,0,1,1,1,1}->62,{1,1,0,0,0,0}->38,{1,1,0,0,0,1}->96,{1,1,0,0,1,0}->160,{1,1,0,0,1,1}->110,{1,1,0,1,0,0}->142,{1,1,0,1,0,1}->208,{1,1,0,1,1,0}->156,{1,1,0,1,1,1}->80,{1,1,1,0,0,0}->42,{1,1,1,0,0,1}->92,{1,1,1,0,1,0}->122,{1,1,1,0,1,1}->80,{1,1,1,1,0,0}->38,{1,1,1,1,0,1}->62,{1,1,1,1,1,0}->26,{1,1,1,1,1,1}->6,{0,0,0,0,0,0,0}->7,{0,0,0,0,0,0,1}->31,{0,0,0,0,0,1,0}->76,{0,0,0,0,0,1,1}->47,{0,0,0,0,1,0,0}->103,{0,0,0,0,1,0,1}->156,{0,0,0,0,1,1,0}->119,{0,0,0,0,1,1,1}->55,{0,0,0,1,0,0,0}->112,{0,0,0,1,0,0,1}->215,{0,0,0,1,0,1,0}->285,{0,0,0,1,0,1,1}->196,{0,0,0,1,1,0,0}->155,{0,0,0,1,1,0,1}->223,{0,0,0,1,1,1,0}->136,{0,0,0,1,1,1,1}->55,{0,0,1,0,0,0,0}->103,{0,0,1,0,0,0,1}->224,{0,0,1,0,0,1,0}->351,{0,0,1,0,0,1,1}->255,{0,0,1,0,1,0,0}->328,{0,0,1,0,1,0,1}->461,{0,0,1,0,1,1,0}->359,{0,0,1,0,1,1,1}->196,{0,0,1,1,0,0,0}->155,{0,0,1,1,0,0,1}->291,{0,0,1,1,0,1,0}->368,{0,0,1,1,0,1,1}->255,{0,0,1,1,1,0,0}->163,{0,0,1,1,1,0,1}->232,{0,0,1,1,1,1,0}->127,{0,0,1,1,1,1,1}->47,{0,1,0,0,0,0,0}->76,{0,1,0,0,0,0,1}->183,{0,1,0,0,0,1,0}->317,{0,1,0,0,0,1,1}->232,{0,1,0,0,1,0,0}->351,{0,1,0,0,1,0,1}->495,{0,1,0,0,1,1,0}->400,{0,1,0,0,1,1,1}->223,{0,1,0,1,0,0,0}->285,{0,1,0,1,0,0,1}->504,{0,1,0,1,0,1,0}->637,{0,1,0,1,0,1,1}->461,{0,1,0,1,1,0,0}->368,{0,1,0,1,1,0,1}->495,{0,1,0,1,1,1,0}->325,{0,1,0,1,1,1,1}->156,{0,1,1,0,0,0,0}->119,{0,1,1,0,0,0,1}->259,{0,1,1,0,0,1,0}->400,{0,1,1,0,0,1,1}->291,{0,1,1,0,1,0,0}->359,{0,1,1,0,1,0,1}->504,{0,1,1,0,1,1,0}->391,{0,1,1,0,1,1,1}->215,{0,1,1,1,0,0,0}->136,{0,1,1,1,0,0,1}->259,{0,1,1,1,0,1,0}->325,{0,1,1,1,0,1,1}->224,{0,1,1,1,1,0,0}->127,{0,1,1,1,1,0,1}->183,{0,1,1,1,1,1,0}->92,{0,1,1,1,1,1,1}->31,{1,0,0,0,0,0,0}->31,{1,0,0,0,0,0,1}->92,{1,0,0,0,0,1,0}->183,{1,0,0,0,0,1,1}->127,{1,0,0,0,1,0,0}->224,{1,0,0,0,1,0,1}->325,{1,0,0,0,1,1,0}->259,{1,0,0,0,1,1,1}->136,{1,0,0,1,0,0,0}->215,{1,0,0,1,0,0,1}->391,{1,0,0,1,0,1,0}->504,{1,0,0,1,0,1,1}->359,{1,0,0,1,1,0,0}->291,{1,0,0,1,1,0,1}->400,{1,0,0,1,1,1,0}->259,{1,0,0,1,1,1,1}->119,{1,0,1,0,0,0,0}->156,{1,0,1,0,0,0,1}->325,{1,0,1,0,0,1,0}->495,{1,0,1,0,0,1,1}->368,{1,0,1,0,1,0,0}->461,{1,0,1,0,1,0,1}->637,{1,0,1,0,1,1,0}->504,{1,0,1,0,1,1,1}->285,{1,0,1,1,0,0,0}->223,{1,0,1,1,0,0,1}->400,{1,0,1,1,0,1,0}->495,{1,0,1,1,0,1,1}->351,{1,0,1,1,1,0,0}->232,{1,0,1,1,1,0,1}->317,{1,0,1,1,1,1,0}->183,{1,0,1,1,1,1,1}->76,{1,1,0,0,0,0,0}->47,{1,1,0,0,0,0,1}->127,{1,1,0,0,0,1,0}->232,{1,1,0,0,0,1,1}->163,{1,1,0,0,1,0,0}->255,{1,1,0,0,1,0,1}->368,{1,1,0,0,1,1,0}->291,{1,1,0,0,1,1,1}->155,{1,1,0,1,0,0,0}->196,{1,1,0,1,0,0,1}->359,{1,1,0,1,0,1,0}->461,{1,1,0,1,0,1,1}->328,{1,1,0,1,1,0,0}->255,{1,1,0,1,1,0,1}->351,{1,1,0,1,1,1,0}->224,{1,1,0,1,1,1,1}->103,{1,1,1,0,0,0,0}->55,{1,1,1,0,0,0,1}->136,{1,1,1,0,0,1,0}->223,{1,1,1,0,0,1,1}->155,{1,1,1,0,1,0,0}->196,{1,1,1,0,1,0,1}->285,{1,1,1,0,1,1,0}->215,{1,1,1,0,1,1,1}->112,{1,1,1,1,0,0,0}->55,{1,1,1,1,0,0,1}->119,{1,1,1,1,0,1,0}->156,{1,1,1,1,0,1,1}->103,{1,1,1,1,1,0,0}->47,{1,1,1,1,1,0,1}->76,{1,1,1,1,1,1,0}->31,{1,1,1,1,1,1,1}->7,{0,0,0,0,0,0,0,0}->8,{0,0,0,0,0,0,0,1}->36,{0,0,0,0,0,0,1,0}->90,{0,0,0,0,0,0,1,1}->56,{0,0,0,0,0,1,0,0}->126,{0,0,0,0,0,1,0,1}->190,{0,0,0,0,0,1,1,0}->146,{0,0,0,0,0,1,1,1}->68,{0,0,0,0,1,0,0,0}->144,{0,0,0,0,1,0,0,1}->274,{0,0,0,0,1,0,1,0}->362,{0,0,0,0,1,0,1,1}->250,{0,0,0,0,1,1,0,0}->200,{0,0,0,0,1,1,0,1}->286,{0,0,0,0,1,1,1,0}->176,{0,0,0,0,1,1,1,1}->72,{0,0,0,1,0,0,0,0}->144,{0,0,0,1,0,0,0,1}->308,{0,0,0,1,0,0,1,0}->478,{0,0,0,1,0,0,1,1}->350,{0,0,0,1,0,1,0,0}->448,{0,0,0,1,0,1,0,1}->626,{0,0,0,1,0,1,1,0}->490,{0,0,0,1,0,1,1,1}->270,{0,0,0,1,1,0,0,0}->218,{0,0,0,1,1,0,0,1}->404,{0,0,0,1,1,0,1,0}->508,{0,0,0,1,1,0,1,1}->354,{0,0,0,1,1,1,0,0}->230,{0,0,0,1,1,1,0,1}->324,{0,0,0,1,1,1,1,0}->180,{0,0,0,1,1,1,1,1}->68,{0,0,1,0,0,0,0,0}->126,{0,0,1,0,0,0,0,1}->292,{0,0,1,0,0,0,1,0}->494,{0,0,1,0,0,0,1,1}->368,{0,0,1,0,0,1,0,0}->546,{0,0,1,0,0,1,0,1}->762,{0,0,1,0,0,1,1,0}->622,{0,0,1,0,0,1,1,1}->354,{0,0,1,0,1,0,0,0}->448,{0,0,1,0,1,0,0,1}->780,{0,0,1,0,1,0,1,0}->978,{0,0,1,0,1,0,1,1}->714,{0,0,1,0,1,1,0,0}->576,{0,0,1,0,1,1,0,1}->766,{0,0,1,0,1,1,1,0}->510,{0,0,1,0,1,1,1,1}->250,{0,0,1,1,0,0,0,0}->200,{0,0,1,1,0,0,0,1}->422,{0,0,1,1,0,0,1,0}->640,{0,0,1,1,0,0,1,1}->472,{0,0,1,1,0,1,0,0}->576,{0,0,1,1,0,1,0,1}->800,{0,0,1,1,0,1,1,0}->626,{0,0,1,1,0,1,1,1}->350,{0,0,1,1,1,0,0,0}->230,{0,0,1,1,1,0,0,1}->426,{0,0,1,1,1,0,1,0}->528,{0,0,1,1,1,0,1,1}->368,{0,0,1,1,1,1,0,0}->216,{0,0,1,1,1,1,0,1}->304,{0,0,1,1,1,1,1,0}->158,{0,0,1,1,1,1,1,1}->56,{0,1,0,0,0,0,0,0}->90,{0,1,0,0,0,0,0,1}->226,{0,1,0,0,0,0,1,0}->410,{0,1,0,0,0,0,1,1}->304,{0,1,0,0,0,1,0,0}->494,{0,1,0,0,0,1,0,1}->694,{0,1,0,0,0,1,1,0}->572,{0,1,0,0,0,1,1,1}->324,{0,1,0,0,1,0,0,0}->478,{0,1,0,0,1,0,0,1}->830,{0,1,0,0,1,0,1,0}->1046,{0,1,0,0,1,0,1,1}->766,{0,1,0,0,1,1,0,0}->640,{0,1,0,0,1,1,0,1}->850,{0,1,0,0,1,1,1,0}->576,{0,1,0,0,1,1,1,1}->286,{0,1,0,1,0,0,0,0}->362,{0,1,0,1,0,0,0,1}->712,{0,1,0,1,0,0,1,0}->1046,{0,1,0,1,0,0,1,1}->800,{0,1,0,1,0,1,0,0}->978,{0,1,0,1,0,1,0,1}->1322,{0,1,0,1,0,1,1,0}->1066,{0,1,0,1,0,1,1,1}->626,{0,1,0,1,1,0,0,0}->508,{0,1,0,1,1,0,0,1}->868,{0,1,0,1,1,0,1,0}->1050,{0,1,0,1,1,0,1,1}->762,{0,1,0,1,1,1,0,0}->528,{0,1,0,1,1,1,0,1}->694,{0,1,0,1,1,1,1,0}->422,{0,1,0,1,1,1,1,1}->190,{0,1,1,0,0,0,0,0}->146,{0,1,1,0,0,0,0,1}->340,{0,1,1,0,0,0,1,0}->572,{0,1,1,0,0,0,1,1}->426,{0,1,1,0,0,1,0,0}->622,{0,1,1,0,0,1,0,1}->868,{0,1,1,0,0,1,1,0}->708,{0,1,1,0,0,1,1,1}->404,{0,1,1,0,1,0,0,0}->490,{0,1,1,0,1,0,0,1}->852,{0,1,1,0,1,0,1,0}->1066,{0,1,1,0,1,0,1,1}->780,{0,1,1,0,1,1,0,0}->626,{0,1,1,0,1,1,0,1}->830,{0,1,1,0,1,1,1,0}->554,{0,1,1,0,1,1,1,1}->274,{0,1,1,1,0,0,0,0}->176,{0,1,1,1,0,0,0,1}->378,{0,1,1,1,0,0,1,0}->576,{0,1,1,1,0,0,1,1}->422,{0,1,1,1,0,1,0,0}->510,{0,1,1,1,0,1,0,1}->712,{0,1,1,1,0,1,1,0}->554,{0,1,1,1,0,1,1,1}->308,{0,1,1,1,1,0,0,0}->180,{0,1,1,1,1,0,0,1}->340,{0,1,1,1,1,0,1,0}->422,{0,1,1,1,1,0,1,1}->292,{0,1,1,1,1,1,0,0}->158,{0,1,1,1,1,1,0,1}->226,{0,1,1,1,1,1,1,0}->110,{0,1,1,1,1,1,1,1}->36,{1,0,0,0,0,0,0,0}->36,{1,0,0,0,0,0,0,1}->110,{1,0,0,0,0,0,1,0}->226,{1,0,0,0,0,0,1,1}->158,{1,0,0,0,0,1,0,0}->292,{1,0,0,0,0,1,0,1}->422,{1,0,0,0,0,1,1,0}->340,{1,0,0,0,0,1,1,1}->180,{1,0,0,0,1,0,0,0}->308,{1,0,0,0,1,0,0,1}->554,{1,0,0,0,1,0,1,0}->712,{1,0,0,0,1,0,1,1}->510,{1,0,0,0,1,1,0,0}->422,{1,0,0,0,1,1,0,1}->576,{1,0,0,0,1,1,1,0}->378,{1,0,0,0,1,1,1,1}->176,{1,0,0,1,0,0,0,0}->274,{1,0,0,1,0,0,0,1}->554,{1,0,0,1,0,0,1,0}->830,{1,0,0,1,0,0,1,1}->626,{1,0,0,1,0,1,0,0}->780,{1,0,0,1,0,1,0,1}->1066,{1,0,0,1,0,1,1,0}->852,{1,0,0,1,0,1,1,1}->490,{1,0,0,1,1,0,0,0}->404,{1,0,0,1,1,0,0,1}->708,{1,0,0,1,1,0,1,0}->868,{1,0,0,1,1,0,1,1}->622,{1,0,0,1,1,1,0,0}->426,{1,0,0,1,1,1,0,1}->572,{1,0,0,1,1,1,1,0}->340,{1,0,0,1,1,1,1,1}->146,{1,0,1,0,0,0,0,0}->190,{1,0,1,0,0,0,0,1}->422,{1,0,1,0,0,0,1,0}->694,{1,0,1,0,0,0,1,1}->528,{1,0,1,0,0,1,0,0}->762,{1,0,1,0,0,1,0,1}->1050,{1,0,1,0,0,1,1,0}->868,{1,0,1,0,0,1,1,1}->508,{1,0,1,0,1,0,0,0}->626,{1,0,1,0,1,0,0,1}->1066,{1,0,1,0,1,0,1,0}->1322,{1,0,1,0,1,0,1,1}->978,{1,0,1,0,1,1,0,0}->800,{1,0,1,0,1,1,0,1}->1046,{1,0,1,0,1,1,1,0}->712,{1,0,1,0,1,1,1,1}->362,{1,0,1,1,0,0,0,0}->286,{1,0,1,1,0,0,0,1}->576,{1,0,1,1,0,0,1,0}->850,{1,0,1,1,0,0,1,1}->640,{1,0,1,1,0,1,0,0}->766,{1,0,1,1,0,1,0,1}->1046,{1,0,1,1,0,1,1,0}->830,{1,0,1,1,0,1,1,1}->478,{1,0,1,1,1,0,0,0}->324,{1,0,1,1,1,0,0,1}->572,{1,0,1,1,1,0,1,0}->694,{1,0,1,1,1,0,1,1}->494,{1,0,1,1,1,1,0,0}->304,{1,0,1,1,1,1,0,1}->410,{1,0,1,1,1,1,1,0}->226,{1,0,1,1,1,1,1,1}->90,{1,1,0,0,0,0,0,0}->56,{1,1,0,0,0,0,0,1}->158,{1,1,0,0,0,0,1,0}->304,{1,1,0,0,0,0,1,1}->216,{1,1,0,0,0,1,0,0}->368,{1,1,0,0,0,1,0,1}->528,{1,1,0,0,0,1,1,0}->426,{1,1,0,0,0,1,1,1}->230,{1,1,0,0,1,0,0,0}->350,{1,1,0,0,1,0,0,1}->626,{1,1,0,0,1,0,1,0}->800,{1,1,0,0,1,0,1,1}->576,{1,1,0,0,1,1,0,0}->472,{1,1,0,0,1,1,0,1}->640,{1,1,0,0,1,1,1,0}->422,{1,1,0,0,1,1,1,1}->200,{1,1,0,1,0,0,0,0}->250,{1,1,0,1,0,0,0,1}->510,{1,1,0,1,0,0,1,0}->766,{1,1,0,1,0,0,1,1}->576,{1,1,0,1,0,1,0,0}->714,{1,1,0,1,0,1,0,1}->978,{1,1,0,1,0,1,1,0}->780,{1,1,0,1,0,1,1,1}->448,{1,1,0,1,1,0,0,0}->354,{1,1,0,1,1,0,0,1}->622,{1,1,0,1,1,0,1,0}->762,{1,1,0,1,1,0,1,1}->546,{1,1,0,1,1,1,0,0}->368,{1,1,0,1,1,1,0,1}->494,{1,1,0,1,1,1,1,0}->292,{1,1,0,1,1,1,1,1}->126,{1,1,1,0,0,0,0,0}->68,{1,1,1,0,0,0,0,1}->180,{1,1,1,0,0,0,1,0}->324,{1,1,1,0,0,0,1,1}->230,{1,1,1,0,0,1,0,0}->354,{1,1,1,0,0,1,0,1}->508,{1,1,1,0,0,1,1,0}->404,{1,1,1,0,0,1,1,1}->218,{1,1,1,0,1,0,0,0}->270,{1,1,1,0,1,0,0,1}->490,{1,1,1,0,1,0,1,0}->626,{1,1,1,0,1,0,1,1}->448,{1,1,1,0,1,1,0,0}->350,{1,1,1,0,1,1,0,1}->478,{1,1,1,0,1,1,1,0}->308,{1,1,1,0,1,1,1,1}->144,{1,1,1,1,0,0,0,0}->72,{1,1,1,1,0,0,0,1}->176,{1,1,1,1,0,0,1,0}->286,{1,1,1,1,0,0,1,1}->200,{1,1,1,1,0,1,0,0}->250,{1,1,1,1,0,1,0,1}->362,{1,1,1,1,0,1,1,0}->274,{1,1,1,1,0,1,1,1}->144,{1,1,1,1,1,0,0,0}->68,{1,1,1,1,1,0,0,1}->146,{1,1,1,1,1,0,1,0}->190,{1,1,1,1,1,0,1,1}->126,{1,1,1,1,1,1,0,0}->56,{1,1,1,1,1,1,0,1}->90,{1,1,1,1,1,1,1,0}->36,{1,1,1,1,1,1,1,1}->8]; これを全自動遷移行列学習器にぶち込んで遷移行列を自動生成する. */ //【行列】 /* * 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 }; int main() { // input_from_file("input.txt"); // output_to_file("output.txt"); string s; ll K; cin >> s >> K; // 生成された遷移行列を貼る. vvm umeC = { {0, 1, 1, 6, 6, 20}, {1, 2, 6, 11, 20, 34}, {1, 6, 2, 20, 11, 54}, {6, 20, 11, 54, 38, 131}, {6, 11, 20, 38, 54, 97}, {20, 34, 54, 97, 131, 224} }; vvvm umes = { {{0, 1, 0, 0, 0, 0}, {1000000006, 2, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 0, 1}, {0, 0, 1000000006, 0, 2, 0}, {0, 0, 0, 1000000006, 0, 2}}, {{0, 0, 1, 0, 0, 0}, {0, 0, 0, 1, 0, 0}, {1000000006, 0, 2, 0, 0, 0}, {0, 1000000006, 0, 2, 0, 0}, {13, 999999997, 999999997, 5, 5, 1000000006}, {49, 999999972, 999999970, 15, 16, 1000000004}} }; Matrix<mint> matC(umeC); Matrix<mint> mat0(umes[0]); Matrix<mint> mat1(umes[1]); Matrix<mint> mat(sz(umeC)); repe(c, s) { if (c == '0') mat = mat * mat0; else if (c == '1') mat = mat * mat1; } mat = mat.pow(K); mat = mat * umeC; EXIT(mat[0][0]); }