結果
問題 | No.569 3 x N グリッドのパスの数 |
ユーザー | pekempey |
提出日時 | 2017-09-17 16:17:41 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 72 ms / 2,000 ms |
コード長 | 4,170 bytes |
コンパイル時間 | 1,682 ms |
コンパイル使用メモリ | 116,124 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-08 01:55:12 |
合計ジャッジ時間 | 7,536 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 71 ms
5,248 KB |
testcase_01 | AC | 71 ms
5,248 KB |
testcase_02 | AC | 71 ms
5,248 KB |
testcase_03 | AC | 71 ms
5,248 KB |
testcase_04 | AC | 71 ms
5,248 KB |
testcase_05 | AC | 71 ms
5,248 KB |
testcase_06 | AC | 71 ms
5,248 KB |
testcase_07 | AC | 72 ms
5,248 KB |
testcase_08 | AC | 71 ms
5,248 KB |
testcase_09 | AC | 71 ms
5,248 KB |
testcase_10 | AC | 71 ms
5,248 KB |
testcase_11 | AC | 71 ms
5,248 KB |
testcase_12 | AC | 71 ms
5,248 KB |
testcase_13 | AC | 71 ms
5,248 KB |
testcase_14 | AC | 71 ms
5,248 KB |
testcase_15 | AC | 70 ms
5,248 KB |
testcase_16 | AC | 71 ms
5,248 KB |
testcase_17 | AC | 71 ms
5,248 KB |
testcase_18 | AC | 71 ms
5,248 KB |
testcase_19 | AC | 71 ms
5,248 KB |
testcase_20 | AC | 71 ms
5,248 KB |
testcase_21 | AC | 71 ms
5,248 KB |
testcase_22 | AC | 71 ms
5,248 KB |
testcase_23 | AC | 71 ms
5,248 KB |
testcase_24 | AC | 70 ms
5,248 KB |
testcase_25 | AC | 70 ms
5,248 KB |
testcase_26 | AC | 71 ms
5,248 KB |
testcase_27 | AC | 71 ms
5,248 KB |
testcase_28 | AC | 70 ms
5,248 KB |
testcase_29 | AC | 71 ms
5,248 KB |
testcase_30 | AC | 72 ms
5,248 KB |
testcase_31 | AC | 71 ms
5,248 KB |
testcase_32 | AC | 71 ms
5,248 KB |
testcase_33 | AC | 71 ms
5,248 KB |
testcase_34 | AC | 71 ms
5,248 KB |
testcase_35 | AC | 70 ms
5,248 KB |
testcase_36 | AC | 70 ms
5,248 KB |
testcase_37 | AC | 71 ms
5,248 KB |
testcase_38 | AC | 72 ms
5,248 KB |
testcase_39 | AC | 70 ms
5,248 KB |
testcase_40 | AC | 71 ms
5,248 KB |
testcase_41 | AC | 71 ms
5,248 KB |
testcase_42 | AC | 71 ms
5,248 KB |
testcase_43 | AC | 71 ms
5,248 KB |
testcase_44 | AC | 72 ms
5,248 KB |
testcase_45 | AC | 71 ms
5,248 KB |
testcase_46 | AC | 71 ms
5,248 KB |
testcase_47 | AC | 72 ms
5,248 KB |
testcase_48 | AC | 72 ms
5,248 KB |
testcase_49 | AC | 71 ms
5,248 KB |
testcase_50 | AC | 71 ms
5,248 KB |
testcase_51 | AC | 71 ms
5,248 KB |
testcase_52 | AC | 71 ms
5,248 KB |
testcase_53 | AC | 71 ms
5,248 KB |
testcase_54 | AC | 71 ms
5,248 KB |
testcase_55 | AC | 70 ms
5,248 KB |
testcase_56 | AC | 71 ms
5,248 KB |
testcase_57 | AC | 71 ms
5,248 KB |
testcase_58 | AC | 71 ms
5,248 KB |
testcase_59 | AC | 71 ms
5,248 KB |
ソースコード
#include <iostream> #include <algorithm> #include <vector> #include <map> #include <set> #include <cassert> using namespace std; constexpr int mod = 1e9 + 7; struct modint { int n; modint(int n = 0) : n(n) {} }; modint operator+(modint a, modint b) { return modint((a.n += b.n) >= mod ? a.n - mod : a.n); } modint operator-(modint a, modint b) { return modint((a.n -= b.n) < 0 ? a.n + mod : a.n); } modint operator*(modint a, modint b) { return modint(1LL * a.n * b.n % mod); } bool operator<(modint a, modint b) { return a.n < b.n; } modint &operator+=(modint &a, modint b) { return a = a + b; } modint &operator-=(modint &a, modint b) { return a = a - b; } modint &operator*=(modint &a, modint b) { return a = a * b; } modint modinv(modint n) { if (n.n == 1) return 1; return modinv(mod % n.n) * (mod - mod / n.n); } modint operator/(modint a, modint b) { return a * modinv(b); } modint simpath(int n) { constexpr int H = 4; vector<pair<int, int>> es; for (int x = 0; x < n; x++) { for (int y = 0; y < H; y++) { if (y + 1 < H) es.emplace_back(x * H + y, x * H + y + 1); if (x + 1 < n) es.emplace_back(x * H + y, x * H + y + H); } } vector<int> deg(n * H); for (auto e : es) { deg[e.first]++; deg[e.second]++; } map<map<int, int>, modint> dp0, dp1; dp0[{{0, 0}}] = 1; for (auto e : es) { const int u = e.first; const int v = e.second; deg[u]--; deg[v]--; dp1.clear(); for (auto kv : dp0) { map<int, int> mate1 = kv.first; if (!kv.first.count(v)) mate1[v] = v; const int mu = mate1[u]; const int mv = mate1[v]; const bool del = deg[u] == 0 && u != 0; if (del) mate1.erase(u); if (!del || mu == u || mu == -1) dp1[mate1] += kv.second; if (mu == v || mu == -1 || mv == -1) continue; mate1[u] = -1; mate1[v] = -1; mate1[mu] = mv; mate1[mv] = mu; if (mate1[0] == -1) continue; if (del) mate1.erase(u); if (!del || mu != u) dp1[mate1] += kv.second; } swap(dp0, dp1); } for (auto kv : dp0) { auto mate = kv.first; if (mate[H * n - 1] == 0) return kv.second; } return -1; } // ref: https://en.wikipedia.org/wiki/Berlekamp%E2%80%93Massey_algorithm vector<modint> berlekamp_massey(vector<modint> s) { const int N = s.size(); vector<modint> C(N); vector<modint> B(N); C[0] = 1; B[0] = 1; int L = 0; int m = 1; modint b = 1; for (int n = 0; n < N; n++) { modint d = s[n]; for (int i = 1; i <= L; i++) d += C[i] * s[n - i]; if (d.n == 0) { m++; } else if (2 * L <= n) { auto T = C; for (int i = 0; i + m < N; i++) C[i + m] -= B[i] * (d / b); L = n + 1 - L; B = T; b = d; m = 1; } else { for (int i = 0; i + m < N; i++) C[i + m] -= B[i] * (d / b); m++; } } C.resize(L + 1); reverse(C.begin(), C.end()); return C; } vector<modint> poly_mod(vector<modint> a, const vector<modint> &m) { const int n = m.size(); for (int i = a.size() - 1; i >= m.size(); i--) { for (int j = 0; j < m.size(); j++) { a[i - n + j] += a[i] * m[j]; } } a.resize(m.size()); return a; } // a*b mod m vector<modint> poly_mul(const vector<modint> &a, const vector<modint> &b, const vector<modint> &m) { vector<modint> ret(a.size() + b.size() - 1); for (int i = 0; i < a.size(); i++) { for (int j = 0; j < b.size(); j++) { ret[i + j] += a[i] * b[j]; } } return poly_mod(ret, m); } // x^n mod m vector<modint> nth_power(long long n, const vector<modint> &m) { vector<modint> ret(1); vector<modint> x(2); ret[0] = x[1] = 1; while (n > 0) { if (n & 1) ret = poly_mul(ret, x, m); x = poly_mul(x, x, m); n /= 2; } return poly_mod(ret, m); } int main() { vector<modint> a(30); for (int i = 0; i < a.size(); i++) { a[i] = simpath(i + 1); } vector<modint> m = berlekamp_massey(a); m.pop_back(); for (int i = 0; i < m.size(); i++) { m[i] *= mod - 1; } long long n; cin >> n; auto x = nth_power(n, m); modint ans; for (int i = 0; i < x.size(); i++) { ans += x[i] * a[i]; } cout << ans.n << endl; }