結果
問題 | No.621 3 x N グリッド上のドミノの置き方の数 |
ユーザー | pekempey |
提出日時 | 2017-12-21 00:45:58 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 13 ms / 3,000 ms |
コード長 | 4,389 bytes |
コンパイル時間 | 1,011 ms |
コンパイル使用メモリ | 83,252 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-12-16 07:30:06 |
合計ジャッジ時間 | 3,310 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 13 ms
5,248 KB |
testcase_01 | AC | 11 ms
5,248 KB |
testcase_02 | AC | 12 ms
5,248 KB |
testcase_03 | AC | 12 ms
5,248 KB |
testcase_04 | AC | 12 ms
5,248 KB |
testcase_05 | AC | 13 ms
5,248 KB |
testcase_06 | AC | 11 ms
5,248 KB |
testcase_07 | AC | 13 ms
5,248 KB |
testcase_08 | AC | 12 ms
5,248 KB |
testcase_09 | AC | 12 ms
5,248 KB |
testcase_10 | AC | 12 ms
5,248 KB |
testcase_11 | AC | 11 ms
5,248 KB |
testcase_12 | AC | 12 ms
5,248 KB |
testcase_13 | AC | 12 ms
5,248 KB |
testcase_14 | AC | 11 ms
5,248 KB |
testcase_15 | AC | 11 ms
5,248 KB |
testcase_16 | AC | 12 ms
5,248 KB |
testcase_17 | AC | 13 ms
5,248 KB |
testcase_18 | AC | 13 ms
5,248 KB |
testcase_19 | AC | 11 ms
5,248 KB |
testcase_20 | AC | 11 ms
5,248 KB |
testcase_21 | AC | 12 ms
5,248 KB |
testcase_22 | AC | 12 ms
5,248 KB |
testcase_23 | AC | 11 ms
5,248 KB |
testcase_24 | AC | 12 ms
5,248 KB |
testcase_25 | AC | 12 ms
5,248 KB |
testcase_26 | AC | 11 ms
5,248 KB |
testcase_27 | AC | 12 ms
5,248 KB |
testcase_28 | AC | 12 ms
5,248 KB |
testcase_29 | AC | 12 ms
5,248 KB |
testcase_30 | AC | 12 ms
5,248 KB |
testcase_31 | AC | 12 ms
5,248 KB |
testcase_32 | AC | 12 ms
5,248 KB |
testcase_33 | AC | 13 ms
5,248 KB |
testcase_34 | AC | 13 ms
5,248 KB |
testcase_35 | AC | 12 ms
5,248 KB |
testcase_36 | AC | 12 ms
5,248 KB |
testcase_37 | AC | 13 ms
5,248 KB |
testcase_38 | AC | 12 ms
5,248 KB |
testcase_39 | AC | 12 ms
5,248 KB |
testcase_40 | AC | 12 ms
5,248 KB |
testcase_41 | AC | 12 ms
5,248 KB |
testcase_42 | AC | 12 ms
5,248 KB |
testcase_43 | AC | 12 ms
5,248 KB |
testcase_44 | AC | 11 ms
5,248 KB |
testcase_45 | AC | 13 ms
5,248 KB |
testcase_46 | AC | 12 ms
5,248 KB |
testcase_47 | AC | 12 ms
5,248 KB |
testcase_48 | AC | 12 ms
5,248 KB |
testcase_49 | AC | 12 ms
5,248 KB |
testcase_50 | AC | 13 ms
5,248 KB |
testcase_51 | AC | 12 ms
5,248 KB |
testcase_52 | AC | 12 ms
5,248 KB |
testcase_53 | AC | 13 ms
5,248 KB |
testcase_54 | AC | 13 ms
5,248 KB |
testcase_55 | AC | 12 ms
5,248 KB |
testcase_56 | AC | 12 ms
5,248 KB |
testcase_57 | AC | 12 ms
5,248 KB |
testcase_58 | AC | 13 ms
5,248 KB |
testcase_59 | AC | 12 ms
5,248 KB |
testcase_60 | AC | 12 ms
5,248 KB |
testcase_61 | AC | 13 ms
5,248 KB |
testcase_62 | AC | 13 ms
5,248 KB |
testcase_63 | AC | 12 ms
5,248 KB |
testcase_64 | AC | 12 ms
5,248 KB |
testcase_65 | AC | 12 ms
5,248 KB |
testcase_66 | AC | 12 ms
5,248 KB |
testcase_67 | AC | 12 ms
5,248 KB |
ソースコード
#include <iostream> #include <vector> #include <algorithm> #include <cstring> using namespace std; const 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); } 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 a) { if (a.n == 1) return 1; return modinv(mod % a.n) * (mod - mod / a.n); } Modint operator/(Modint a, Modint b) { return a * modinv(b); } std::vector<Modint> berlekamp_massey(std::vector<Modint> s) { using K = Modint; const int N = s.size(); std::vector<K> C(N); std::vector<K> B(N); C[0] = 1; B[0] = 1; int L = 0; int m = 1; K b = 1; for (int n = 0; n < N; n++) { K 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(x) 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); } // a^b mod m(x) vector<Modint> poly_pow(vector<Modint> a, long long b, const vector<Modint> &m) { vector<Modint> ret(1); ret[0] = 1; while (b > 0) { if (b & 1) ret = poly_mul(ret, a, m); a = poly_mul(a, a, m); b >>= 1; } return poly_mod(ret, m); } Modint linear_recurrence_relation(vector<Modint> a, long long k) { auto m = berlekamp_massey(a); m.pop_back(); for (Modint &a : m) { a *= mod - 1; } auto p = poly_pow({0, 1}, k, m); Modint res = 0; for (int i = 0; i < p.size(); i++) { res += a[i] * p[i]; } return res; } bool g[3][50]; Modint memo[50][8 * 8]; bool vis[50][8 * 8]; Modint f(int n, int k) { if (k == n * 3) { bool any = false; for (int i = 0; i < 3; i++) { for (int j = 0; j < n; j++) { any |= i + 1 < 3 && !g[i][j] && !g[i + 1][j]; any |= j + 1 < n && !g[i][j] && !g[i][j + 1]; } } return !any; } int y = k % 3; int x = k / 3; if (y == 0 && x >= 2) { bool any = false; for (int i = 0; i < 3; i++) { for (int j = 0; j < x; j++) { any |= i + 1 < 3 && !g[i][j] && !g[i + 1][j]; any |= j + 1 < x && !g[i][j] && !g[i][j + 1]; } } if (any) return 0; int bit = 0; bit = bit << 1 | g[0][x - 1]; bit = bit << 1 | g[1][x - 1]; bit = bit << 1 | g[2][x - 1]; bit = bit << 1 | g[0][x]; bit = bit << 1 | g[1][x]; bit = bit << 1 | g[2][x]; if (vis[x][bit]) return memo[x][bit]; } Modint res = f(n, k + 1); if (y + 1 < 3 && !g[y][x] && !g[y + 1][x]) { g[y][x] = g[y + 1][x] = true; res += f(n, k + 1); g[y][x] = g[y + 1][x] = false; } if (x + 1 < n && !g[y][x] && !g[y][x + 1]) { g[y][x] = g[y][x + 1] = true; res += f(n, k + 1); g[y][x] = g[y][x + 1] = false; } if (y == 0 && x >= 2) { int bit = 0; bit = bit << 1 | g[0][x - 1]; bit = bit << 1 | g[1][x - 1]; bit = bit << 1 | g[2][x - 1]; bit = bit << 1 | g[0][x]; bit = bit << 1 | g[1][x]; bit = bit << 1 | g[2][x]; vis[x][bit] = true; memo[x][bit] = res; } return res; } int main() { vector<Modint> a; for (int i = 0; i < 40; i++) { memset(vis, false, sizeof(vis)); a.emplace_back(f(i, 0)); } long long n; cin >> n; cout << linear_recurrence_relation(a, n).n << endl; }