結果
問題 | No.569 3 x N グリッドのパスの数 |
ユーザー | pekempey |
提出日時 | 2017-09-16 18:27:38 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 11 ms / 2,000 ms |
コード長 | 5,393 bytes |
コンパイル時間 | 1,794 ms |
コンパイル使用メモリ | 107,732 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-08 01:36:18 |
合計ジャッジ時間 | 3,950 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 10 ms
5,248 KB |
testcase_01 | AC | 11 ms
5,248 KB |
testcase_02 | AC | 10 ms
5,248 KB |
testcase_03 | AC | 10 ms
5,248 KB |
testcase_04 | AC | 10 ms
5,248 KB |
testcase_05 | AC | 10 ms
5,248 KB |
testcase_06 | AC | 10 ms
5,248 KB |
testcase_07 | AC | 10 ms
5,248 KB |
testcase_08 | AC | 10 ms
5,248 KB |
testcase_09 | AC | 10 ms
5,248 KB |
testcase_10 | AC | 10 ms
5,248 KB |
testcase_11 | AC | 10 ms
5,248 KB |
testcase_12 | AC | 10 ms
5,248 KB |
testcase_13 | AC | 11 ms
5,248 KB |
testcase_14 | AC | 10 ms
5,248 KB |
testcase_15 | AC | 10 ms
5,248 KB |
testcase_16 | AC | 11 ms
5,248 KB |
testcase_17 | AC | 10 ms
5,248 KB |
testcase_18 | AC | 10 ms
5,248 KB |
testcase_19 | AC | 10 ms
5,248 KB |
testcase_20 | AC | 10 ms
5,248 KB |
testcase_21 | AC | 10 ms
5,248 KB |
testcase_22 | AC | 10 ms
5,248 KB |
testcase_23 | AC | 10 ms
5,248 KB |
testcase_24 | AC | 10 ms
5,248 KB |
testcase_25 | AC | 11 ms
5,248 KB |
testcase_26 | AC | 10 ms
5,248 KB |
testcase_27 | AC | 11 ms
5,248 KB |
testcase_28 | AC | 10 ms
5,248 KB |
testcase_29 | AC | 10 ms
5,248 KB |
testcase_30 | AC | 10 ms
5,248 KB |
testcase_31 | AC | 10 ms
5,248 KB |
testcase_32 | AC | 10 ms
5,248 KB |
testcase_33 | AC | 11 ms
5,248 KB |
testcase_34 | AC | 10 ms
5,248 KB |
testcase_35 | AC | 10 ms
5,248 KB |
testcase_36 | AC | 11 ms
5,248 KB |
testcase_37 | AC | 10 ms
5,248 KB |
testcase_38 | AC | 10 ms
5,248 KB |
testcase_39 | AC | 11 ms
5,248 KB |
testcase_40 | AC | 10 ms
5,248 KB |
testcase_41 | AC | 10 ms
5,248 KB |
testcase_42 | AC | 10 ms
5,248 KB |
testcase_43 | AC | 10 ms
5,248 KB |
testcase_44 | AC | 10 ms
5,248 KB |
testcase_45 | AC | 10 ms
5,248 KB |
testcase_46 | AC | 11 ms
5,248 KB |
testcase_47 | AC | 11 ms
5,248 KB |
testcase_48 | AC | 10 ms
5,248 KB |
testcase_49 | AC | 10 ms
5,248 KB |
testcase_50 | AC | 10 ms
5,248 KB |
testcase_51 | AC | 11 ms
5,248 KB |
testcase_52 | AC | 10 ms
5,248 KB |
testcase_53 | AC | 11 ms
5,248 KB |
testcase_54 | AC | 11 ms
5,248 KB |
testcase_55 | AC | 10 ms
5,248 KB |
testcase_56 | AC | 10 ms
5,248 KB |
testcase_57 | AC | 10 ms
5,248 KB |
testcase_58 | AC | 10 ms
5,248 KB |
testcase_59 | AC | 10 ms
5,248 KB |
ソースコード
#include <iostream> #include <algorithm> #include <vector> #include <cmath> #include <array> 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; auto setval = [&](vector<int> &d, array<int, H> &a, int k, int v) { for (int i = 0; i < d.size(); i++) { if (d[i] == k) a[i] = v; } }; auto getval = [&](vector<int> &d, array<int, H> &a, int k) -> int { for (int i = 0; i < d.size(); i++) { if (d[i] == k) return a[i]; } return k; }; 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]++; } vector<pair<array<int, H>, modint>> dp, val; vector<int> fr = { 0 }; dp.push_back(make_pair(array<int, H> {}, 1)); vector<int> fr1; for (auto e : es) { deg[e.first]--; deg[e.second]--; fr1.clear(); for (int u : fr) if (deg[u] > 0) fr1.push_back(u); fr1.push_back(e.second); sort(fr1.begin(), fr1.end()); fr1.erase(unique(fr1.begin(), fr1.end()), fr1.end()); val.clear(); for (auto kv : dp) { auto mate = kv.first; array<int, H> mate1 = {}; for (int i = 0; i < fr1.size(); i++) mate1[i] = fr1[i]; for (int i = 0; i < fr.size(); i++) setval(fr1, mate1, fr[i], mate[i]); val.emplace_back(mate1, kv.second); int mateu = getval(fr, mate, e.first); int matev = getval(fr, mate, e.second); if (mateu == e.second && matev == e.first) continue; if (mateu == -1 || matev == -1) continue; setval(fr1, mate1, e.first, -1); setval(fr1, mate1, e.second, -1); setval(fr1, mate1, mateu, matev); setval(fr1, mate1, matev, mateu); val.emplace_back(mate1, kv.second); } sort(val.begin(), val.end()); int i = 0; dp.clear(); while (i < val.size()) { int j = i; modint sum = 0; while (i < val.size() && val[i].first == val[j].first) { sum += val[i].second; i++; } bool ok = true; bool has0 = false; for (int u : fr1) { int v = getval(fr1, val[j].first, u); if (v == 0) has0 = true; if (v != 0 && v != -1 && u != v && deg[v] == 0) ok = false; } if (ok && has0) dp.emplace_back(val[j].first, sum); } swap(fr, fr1); } for (auto kv : dp) { auto mate = kv.first; if (getval(fr, mate, H * n - 1) == 0) { return kv.second; } } // don't come here abort(); } // 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; }