# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
49811 | 2018-06-03T09:38:40 Z | 강태규(#1279, imeimi2000) | None (JOI16_solitaire) | C++11 | 98 ms | 35072 KB |
#include <iostream> #include <algorithm> #include <vector> #include <queue> #include <deque> #include <set> #include <map> #include <unordered_map> #include <functional> #include <cstring> #include <cmath> #include <ctime> #include <cstdlib> using namespace std; typedef long long llong; typedef long double ld; typedef pair<int, int> pii; typedef pair<llong, llong> pll; const int mod = 1e9 + 7; int n; char grid[3][2001]; int dp[2001][6001][2]; int fact[6001]; void fail() { printf("0\n"); exit(0); } int pow(int x, int p) { if (p == 0) return 1; if (p == 1) return x; int ret = pow(x, p >> 1); ret = (llong)ret * ret % mod; if (p & 1) ret = (llong)ret * x % mod; return ret; } int nP2(int x) { return (llong)x * (x - 1) % mod; } pii getCount(int s, int e) { if (e <= s) return pii(1, 0); int n = e - s; vector<int> cnt(n + 1, 0); vector<int> sum(n + 1, 0); for (int i = s; i < e; ++i) { cnt[i - s + 1] = (grid[0][i] == 'x') + (grid[2][i] == 'x') + 1; sum[i - s + 1] = sum[i - s] + cnt[i - s + 1]; } dp[0][0][0] = 0; dp[0][0][1] = 1; for (int i = 0; i < n; ++i) { for (int j = 1; j < sum[i + 1]; ++j) { dp[i][j][0] = 0; dp[i][j][1] = 0; } } for (int i = 1; i <= n; ++i) { if (cnt[i] == 1) { for (int j = 1; j <= sum[i]; ++j) { dp[i][j][0] = 0; dp[i][j][1] = dp[i - 1][sum[i - 1]][1]; dp[i][j][1] += dp[i - 1][sum[i - 1]][0]; dp[i][j][1] %= mod; dp[i][j][1] += mod - dp[i - 1][j - 1][0]; dp[i][j][1] %= mod; } } else if (cnt[i] == 2) { for (int j = 1; j <= sum[i]; ++j) { dp[i][j][0] = (llong)dp[i - 1][j - 1][1] * (sum[i] - j) % mod; dp[i][j][1] = (llong)dp[i - 1][sum[i - 1]][1] * (j - 1) % mod; if (j > 1) dp[i][j][1] += (llong)(dp[i - 1][sum[i - 1]][0] - dp[i - 1][j - 2][0] + mod) * (j - 1) % mod; dp[i][j][1] %= mod; } } else { for (int j = 1; j <= sum[i]; ++j) { dp[i][j][0] = (llong)dp[i - 1][j - 1][1] * nP2(sum[i] - j) % mod; if (j > 1) dp[i][j][0] += (llong)dp[i - 1][j - 2][1] * (sum[i] - j) % mod * (j - 1) * 2 % mod; dp[i][j][0] %= mod; dp[i][j][1] = (llong)dp[i - 1][sum[i - 1]][1] * nP2(j - 1) % mod; dp[i][j][1] += (llong)(dp[i - 1][sum[i - 1]][0] - dp[i - 1][j - 1][0] + mod) * nP2(j - 1) % mod; dp[i][j][1] %= mod; } } for (int j = 1; j <= sum[i]; ++j) { dp[i][j][0] += dp[i][j - 1][0]; dp[i][j][0] %= mod; dp[i][j][1] += dp[i][j - 1][1]; dp[i][j][1] %= mod; } } return pii((dp[n][sum[n]][0] + dp[n][sum[n]][1]) % mod, sum[n]); } int main() { scanf("%d", &n); scanf("%s", grid[0]); scanf("%s", grid[1]); scanf("%s", grid[2]); if (grid[0][0] == 'x') fail(); if (grid[2][0] == 'x') fail(); if (grid[0][n - 1] == 'x') fail(); if (grid[2][n - 1] == 'x') fail(); for (int i = 1; i < n - 2; ++i) { if (grid[0][i] == 'x' && grid[0][i + 1] == 'x') fail(); if (grid[2][i] == 'x' && grid[2][i + 1] == 'x') fail(); } fact[0] = 1; for (int i = 1; i <= 3 * n; ++i) { fact[i] = (llong)fact[i - 1] * i % mod; } vector<pii> ans; int p = 0; for (int i = 0; i < n; ++i) { if (grid[1][i] == 'o') { ans.push_back(getCount(p, i)); int c = (grid[0][i] == 'x') + (grid[2][i] == 'x'); if (c) ans.emplace_back(c, c); p = i + 1; } } ans.push_back(getCount(p, n)); int sum = 0; for (pii i : ans) { sum += i.second; } int x = fact[sum], y = 1; for (pii i : ans) { x = (llong)x * i.first % mod; y = (llong)y * fact[i.second] % mod; } x = (llong)x * pow(y, mod - 2) % mod; printf("%d\n", x); return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 376 KB | Output is correct |
2 | Correct | 2 ms | 376 KB | Output is correct |
3 | Correct | 2 ms | 432 KB | Output is correct |
4 | Correct | 3 ms | 596 KB | Output is correct |
5 | Correct | 2 ms | 596 KB | Output is correct |
6 | Correct | 2 ms | 596 KB | Output is correct |
7 | Correct | 2 ms | 596 KB | Output is correct |
8 | Correct | 2 ms | 596 KB | Output is correct |
9 | Correct | 3 ms | 596 KB | Output is correct |
10 | Correct | 2 ms | 620 KB | Output is correct |
11 | Correct | 2 ms | 620 KB | Output is correct |
12 | Correct | 2 ms | 620 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 620 KB | Output is correct |
2 | Correct | 2 ms | 620 KB | Output is correct |
3 | Correct | 2 ms | 676 KB | Output is correct |
4 | Correct | 2 ms | 676 KB | Output is correct |
5 | Correct | 3 ms | 676 KB | Output is correct |
6 | Correct | 2 ms | 676 KB | Output is correct |
7 | Correct | 2 ms | 676 KB | Output is correct |
8 | Correct | 3 ms | 676 KB | Output is correct |
9 | Correct | 3 ms | 676 KB | Output is correct |
10 | Correct | 2 ms | 676 KB | Output is correct |
11 | Correct | 2 ms | 676 KB | Output is correct |
12 | Correct | 2 ms | 676 KB | Output is correct |
13 | Correct | 6 ms | 2284 KB | Output is correct |
14 | Correct | 7 ms | 2924 KB | Output is correct |
15 | Correct | 9 ms | 4972 KB | Output is correct |
16 | Correct | 8 ms | 4972 KB | Output is correct |
17 | Correct | 10 ms | 4972 KB | Output is correct |
18 | Correct | 8 ms | 4972 KB | Output is correct |
19 | Correct | 8 ms | 4972 KB | Output is correct |
20 | Correct | 10 ms | 4972 KB | Output is correct |
21 | Correct | 9 ms | 4972 KB | Output is correct |
22 | Correct | 11 ms | 5372 KB | Output is correct |
23 | Correct | 9 ms | 5372 KB | Output is correct |
24 | Correct | 11 ms | 5372 KB | Output is correct |
25 | Correct | 10 ms | 5372 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 3 ms | 5372 KB | Output is correct |
2 | Correct | 2 ms | 5372 KB | Output is correct |
3 | Correct | 2 ms | 5372 KB | Output is correct |
4 | Correct | 3 ms | 5372 KB | Output is correct |
5 | Correct | 2 ms | 5372 KB | Output is correct |
6 | Correct | 2 ms | 5372 KB | Output is correct |
7 | Correct | 8 ms | 5372 KB | Output is correct |
8 | Correct | 2 ms | 5372 KB | Output is correct |
9 | Correct | 3 ms | 5372 KB | Output is correct |
10 | Correct | 2 ms | 5372 KB | Output is correct |
11 | Correct | 2 ms | 5372 KB | Output is correct |
12 | Correct | 3 ms | 5372 KB | Output is correct |
13 | Correct | 3 ms | 5372 KB | Output is correct |
14 | Correct | 2 ms | 5372 KB | Output is correct |
15 | Correct | 2 ms | 5372 KB | Output is correct |
16 | Correct | 2 ms | 5372 KB | Output is correct |
17 | Correct | 2 ms | 5372 KB | Output is correct |
18 | Correct | 2 ms | 5372 KB | Output is correct |
19 | Correct | 3 ms | 5372 KB | Output is correct |
20 | Correct | 2 ms | 5372 KB | Output is correct |
21 | Correct | 2 ms | 5372 KB | Output is correct |
22 | Correct | 2 ms | 5372 KB | Output is correct |
23 | Correct | 2 ms | 5372 KB | Output is correct |
24 | Correct | 2 ms | 5372 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 376 KB | Output is correct |
2 | Correct | 2 ms | 376 KB | Output is correct |
3 | Correct | 2 ms | 432 KB | Output is correct |
4 | Correct | 3 ms | 596 KB | Output is correct |
5 | Correct | 2 ms | 596 KB | Output is correct |
6 | Correct | 2 ms | 596 KB | Output is correct |
7 | Correct | 2 ms | 596 KB | Output is correct |
8 | Correct | 2 ms | 596 KB | Output is correct |
9 | Correct | 3 ms | 596 KB | Output is correct |
10 | Correct | 2 ms | 620 KB | Output is correct |
11 | Correct | 2 ms | 620 KB | Output is correct |
12 | Correct | 2 ms | 620 KB | Output is correct |
13 | Correct | 3 ms | 5372 KB | Output is correct |
14 | Correct | 2 ms | 5372 KB | Output is correct |
15 | Correct | 2 ms | 5372 KB | Output is correct |
16 | Correct | 3 ms | 5372 KB | Output is correct |
17 | Correct | 2 ms | 5372 KB | Output is correct |
18 | Correct | 2 ms | 5372 KB | Output is correct |
19 | Correct | 8 ms | 5372 KB | Output is correct |
20 | Correct | 2 ms | 5372 KB | Output is correct |
21 | Correct | 3 ms | 5372 KB | Output is correct |
22 | Correct | 2 ms | 5372 KB | Output is correct |
23 | Correct | 2 ms | 5372 KB | Output is correct |
24 | Correct | 3 ms | 5372 KB | Output is correct |
25 | Correct | 3 ms | 5372 KB | Output is correct |
26 | Correct | 2 ms | 5372 KB | Output is correct |
27 | Correct | 2 ms | 5372 KB | Output is correct |
28 | Correct | 2 ms | 5372 KB | Output is correct |
29 | Correct | 2 ms | 5372 KB | Output is correct |
30 | Correct | 2 ms | 5372 KB | Output is correct |
31 | Correct | 3 ms | 5372 KB | Output is correct |
32 | Correct | 2 ms | 5372 KB | Output is correct |
33 | Correct | 2 ms | 5372 KB | Output is correct |
34 | Correct | 2 ms | 5372 KB | Output is correct |
35 | Correct | 2 ms | 5372 KB | Output is correct |
36 | Correct | 2 ms | 5372 KB | Output is correct |
37 | Correct | 2 ms | 5372 KB | Output is correct |
38 | Correct | 7 ms | 5372 KB | Output is correct |
39 | Correct | 2 ms | 5372 KB | Output is correct |
40 | Correct | 2 ms | 5372 KB | Output is correct |
41 | Correct | 2 ms | 5372 KB | Output is correct |
42 | Correct | 2 ms | 5372 KB | Output is correct |
43 | Correct | 2 ms | 5372 KB | Output is correct |
44 | Correct | 2 ms | 5372 KB | Output is correct |
45 | Correct | 2 ms | 5372 KB | Output is correct |
46 | Correct | 3 ms | 5372 KB | Output is correct |
47 | Correct | 5 ms | 5372 KB | Output is correct |
48 | Correct | 2 ms | 5372 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 376 KB | Output is correct |
2 | Correct | 2 ms | 376 KB | Output is correct |
3 | Correct | 2 ms | 432 KB | Output is correct |
4 | Correct | 3 ms | 596 KB | Output is correct |
5 | Correct | 2 ms | 596 KB | Output is correct |
6 | Correct | 2 ms | 596 KB | Output is correct |
7 | Correct | 2 ms | 596 KB | Output is correct |
8 | Correct | 2 ms | 596 KB | Output is correct |
9 | Correct | 3 ms | 596 KB | Output is correct |
10 | Correct | 2 ms | 620 KB | Output is correct |
11 | Correct | 2 ms | 620 KB | Output is correct |
12 | Correct | 2 ms | 620 KB | Output is correct |
13 | Correct | 2 ms | 620 KB | Output is correct |
14 | Correct | 2 ms | 620 KB | Output is correct |
15 | Correct | 2 ms | 676 KB | Output is correct |
16 | Correct | 2 ms | 676 KB | Output is correct |
17 | Correct | 3 ms | 676 KB | Output is correct |
18 | Correct | 2 ms | 676 KB | Output is correct |
19 | Correct | 2 ms | 676 KB | Output is correct |
20 | Correct | 3 ms | 676 KB | Output is correct |
21 | Correct | 3 ms | 676 KB | Output is correct |
22 | Correct | 2 ms | 676 KB | Output is correct |
23 | Correct | 2 ms | 676 KB | Output is correct |
24 | Correct | 2 ms | 676 KB | Output is correct |
25 | Correct | 6 ms | 2284 KB | Output is correct |
26 | Correct | 7 ms | 2924 KB | Output is correct |
27 | Correct | 9 ms | 4972 KB | Output is correct |
28 | Correct | 8 ms | 4972 KB | Output is correct |
29 | Correct | 10 ms | 4972 KB | Output is correct |
30 | Correct | 8 ms | 4972 KB | Output is correct |
31 | Correct | 8 ms | 4972 KB | Output is correct |
32 | Correct | 10 ms | 4972 KB | Output is correct |
33 | Correct | 9 ms | 4972 KB | Output is correct |
34 | Correct | 11 ms | 5372 KB | Output is correct |
35 | Correct | 9 ms | 5372 KB | Output is correct |
36 | Correct | 11 ms | 5372 KB | Output is correct |
37 | Correct | 10 ms | 5372 KB | Output is correct |
38 | Correct | 3 ms | 5372 KB | Output is correct |
39 | Correct | 2 ms | 5372 KB | Output is correct |
40 | Correct | 2 ms | 5372 KB | Output is correct |
41 | Correct | 3 ms | 5372 KB | Output is correct |
42 | Correct | 2 ms | 5372 KB | Output is correct |
43 | Correct | 2 ms | 5372 KB | Output is correct |
44 | Correct | 8 ms | 5372 KB | Output is correct |
45 | Correct | 2 ms | 5372 KB | Output is correct |
46 | Correct | 3 ms | 5372 KB | Output is correct |
47 | Correct | 2 ms | 5372 KB | Output is correct |
48 | Correct | 2 ms | 5372 KB | Output is correct |
49 | Correct | 3 ms | 5372 KB | Output is correct |
50 | Correct | 3 ms | 5372 KB | Output is correct |
51 | Correct | 2 ms | 5372 KB | Output is correct |
52 | Correct | 2 ms | 5372 KB | Output is correct |
53 | Correct | 2 ms | 5372 KB | Output is correct |
54 | Correct | 2 ms | 5372 KB | Output is correct |
55 | Correct | 2 ms | 5372 KB | Output is correct |
56 | Correct | 3 ms | 5372 KB | Output is correct |
57 | Correct | 2 ms | 5372 KB | Output is correct |
58 | Correct | 2 ms | 5372 KB | Output is correct |
59 | Correct | 2 ms | 5372 KB | Output is correct |
60 | Correct | 2 ms | 5372 KB | Output is correct |
61 | Correct | 2 ms | 5372 KB | Output is correct |
62 | Correct | 2 ms | 5372 KB | Output is correct |
63 | Correct | 7 ms | 5372 KB | Output is correct |
64 | Correct | 2 ms | 5372 KB | Output is correct |
65 | Correct | 2 ms | 5372 KB | Output is correct |
66 | Correct | 2 ms | 5372 KB | Output is correct |
67 | Correct | 2 ms | 5372 KB | Output is correct |
68 | Correct | 2 ms | 5372 KB | Output is correct |
69 | Correct | 2 ms | 5372 KB | Output is correct |
70 | Correct | 2 ms | 5372 KB | Output is correct |
71 | Correct | 3 ms | 5372 KB | Output is correct |
72 | Correct | 5 ms | 5372 KB | Output is correct |
73 | Correct | 2 ms | 5372 KB | Output is correct |
74 | Correct | 2 ms | 5372 KB | Output is correct |
75 | Correct | 2 ms | 5372 KB | Output is correct |
76 | Correct | 2 ms | 5372 KB | Output is correct |
77 | Correct | 2 ms | 5372 KB | Output is correct |
78 | Correct | 2 ms | 5372 KB | Output is correct |
79 | Correct | 3 ms | 5372 KB | Output is correct |
80 | Correct | 3 ms | 5372 KB | Output is correct |
81 | Correct | 2 ms | 5372 KB | Output is correct |
82 | Correct | 2 ms | 5372 KB | Output is correct |
83 | Correct | 2 ms | 5372 KB | Output is correct |
84 | Correct | 2 ms | 5372 KB | Output is correct |
85 | Correct | 2 ms | 5372 KB | Output is correct |
86 | Correct | 87 ms | 34944 KB | Output is correct |
87 | Correct | 78 ms | 34944 KB | Output is correct |
88 | Correct | 88 ms | 34944 KB | Output is correct |
89 | Correct | 79 ms | 34944 KB | Output is correct |
90 | Correct | 80 ms | 34944 KB | Output is correct |
91 | Correct | 87 ms | 35068 KB | Output is correct |
92 | Correct | 98 ms | 35068 KB | Output is correct |
93 | Correct | 76 ms | 35068 KB | Output is correct |
94 | Correct | 78 ms | 35072 KB | Output is correct |
95 | Correct | 76 ms | 35072 KB | Output is correct |
96 | Correct | 76 ms | 35072 KB | Output is correct |
97 | Correct | 74 ms | 35072 KB | Output is correct |