1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| const int N = 1e4 + 1, MOD = 1e9 + 7; int f[N][7][7]; bool valid[7][7]; class Solution { public: int distinctSequences(int n) { fill(valid[0], valid[6] + 7, 1); valid[2][4] = valid[2][6] = valid[3][6] = valid[4][2] = valid[4][6] = valid[6][2] = valid[6][3] = valid[6][4] = false; function<int(int, int, int)> dfs = [&](int n, int p1, int p2) { if (n == 0) return 1; if (f[n][p1][p2]) return f[n][p1][p2]; int res = 0; for (int x = 1; x <= 6; ++x) if (x != p1 && x != p2 && valid[p2][x]) res = (res + dfs(n - 1, p2, x)) % MOD; f[n][p1][p2] = res; return res; }; return dfs(n, 0, 0); } };
|