Submission #781786

# Submission time Handle Problem Language Result Execution time Memory
781786 2023-07-13T10:54:03 Z flappybird None (JOI16_solitaire) C++17
100 / 100
223 ms 234092 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define MAX 2020
#define MAXS 22
#define INF 1000000000000000001
#define bb ' '
#define ln '\n'
#define Ln '\n'
#define MOD 1000000007
ll C[MAX * 3][MAX * 3];
int arr[3][MAX];
ll fact[MAX * 3];
ll dp[2][MAX][MAX * 3];
int num[MAX];
void no() {
	cout << 0;
	exit(0);
}
ll mpow(ll x, ll y = MOD - 2) {
	if (!y) return 1;
	ll res = mpow(x, y / 2);
	res *= res;
	res %= MOD;
	if (y & 1) res *= x, res %= MOD;
	return res;
}
ll solve(int l, int r) {
	if (l > r) return 1;
	int i, j;
	if (num[l] == 0) {
		dp[0][l][1] = 1;
		dp[1][l][1] = 1;
	}
	if (num[l] == 1) {
		dp[1][l][2] = 1;
		dp[0][l][1] = 1;
		dp[0][l][2] = 1;
	}
	if (num[l] == 2) {
		dp[1][l][3] = 2;
		dp[0][l][1] = 2;
		dp[0][l][2] = 2;
		dp[0][l][3] = 2;
	}
	ll sum = num[l] + 1;
	for (i = l + 1; i <= r; i++) {
		ll psum = 0;
		for (j = sum - 1; j >= 0; j--) {
			psum += dp[0][i - 1][j + 1];
			psum %= MOD;
			dp[0][i][j + num[i] + 1] += (((C[j + num[i]][num[i]] * fact[num[i]]) % MOD) * psum) % MOD;
			dp[1][i][j + num[i] + 1] += (((C[j + num[i]][num[i]] * fact[num[i]]) % MOD) * psum) % MOD;
			dp[0][i][j + num[i] + 1] %= MOD;
			dp[1][i][j + num[i] + 1] %= MOD;
		}
		psum = 0;
		for (j = 1; j <= sum; j++) {
			psum += dp[1][i - 1][j];
			psum %= MOD;
			dp[0][i][j + num[i] + 1] += (((C[j + num[i]][num[i]] * fact[num[i]]) % MOD) * psum) % MOD; //both down
			dp[1][i][j + num[i] + 1] += (((C[j + num[i]][num[i]] * fact[num[i]]) % MOD) * psum) % MOD;
			dp[0][i][j + num[i] + 1] %= MOD;
			dp[1][i][j + num[i] + 1] %= MOD;
			if (num[i] == 2) {
				dp[0][i][j + 1] += (C[sum - j + 2][2] * 2 * psum) % MOD; //both up
				dp[0][i][j + 1] %= MOD;
				dp[0][i][j + 2] += ((((sum - j + 1ll) * (j + 1ll)) % MOD) * 2 * psum) % MOD; //up down
				dp[0][i][j + 2] %= MOD;
			}
			if (num[i] == 1) {
				dp[0][i][j + 1] += ((sum - j + 1) * psum) % MOD; //both up
				dp[0][i][j + 1] %= MOD;
			}
		}
		sum += num[i] + 1;
	}
	ll res = 0;
	for (i = 1; i <= sum; i++) res += dp[0][r][i], res %= MOD;
	return res;
}
signed main() {
	ios::sync_with_stdio(false), cin.tie(0);
	int N;
	cin >> N;
	int i, j;
	C[0][0] = 1;
	fact[0] = 1;
	for (i = 1; i <= N * 3 + 10; i++) fact[i] = (fact[i - 1] * i) % MOD;
	for (i = 1; i <= N * 3 + 10; i++) {
		C[i][0] = C[i][i] = 1;
		for (j = 1; j < i; j++) {
			C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
			C[i][j] %= MOD;
		}
	}
	string s[3];
	cin >> s[0] >> s[1] >> s[2];
	for (i = 1; i <= N; i++) arr[0][i] = s[0][i - 1] == 'o';
	for (i = 1; i <= N; i++) arr[1][i] = s[1][i - 1] == 'o';
	for (i = 1; i <= N; i++) arr[2][i] = s[2][i - 1] == 'o';
	for (i = 1; i <= N; i++) {
		for (auto c : { 0, 2 }) {
			if (!arr[c][i]) {
				if (!arr[c][i - 1]) no();
				if (!arr[c][i + 1]) no();
			}
		}
	}
	int cnt = 0;
	vector<int> v;
	v.push_back(0);
	for (i = 1; i <= N; i++) {
		num[i] = 2 - arr[0][i] - arr[2][i];
		if (arr[1][i]) {
			cnt += num[i];
			v.push_back(i);
		}
	}
	v.push_back(N + 1);
	ll ans = 1;
	int all = 0;
	for (i = 1; i < v.size(); i++) {
		if (v[i - 1] + 1 > v[i] - 1) continue;
		ans *= solve(v[i - 1] + 1, v[i] - 1);
		ans %= MOD;
		int ccnt = 0;
		for (j = v[i - 1] + 1; j < v[i]; j++) ccnt += 1 + num[j];
		ans *= mpow(fact[ccnt]);
		ans %= MOD;
		all += ccnt;
	}
	all += cnt;
	ans *= fact[all];
	ans %= MOD;
	cout << ans << ln;
}

Compilation message

solitaire.cpp: In function 'int main()':
solitaire.cpp:129:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |  for (i = 1; i < v.size(); i++) {
      |              ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 3 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 42964 KB Output is correct
2 Correct 67 ms 152812 KB Output is correct
3 Correct 20 ms 36960 KB Output is correct
4 Correct 82 ms 176292 KB Output is correct
5 Correct 77 ms 175036 KB Output is correct
6 Correct 77 ms 174260 KB Output is correct
7 Correct 77 ms 170720 KB Output is correct
8 Correct 78 ms 176276 KB Output is correct
9 Correct 78 ms 176468 KB Output is correct
10 Correct 76 ms 170608 KB Output is correct
11 Correct 77 ms 172816 KB Output is correct
12 Correct 78 ms 176520 KB Output is correct
13 Correct 86 ms 184772 KB Output is correct
14 Correct 88 ms 186240 KB Output is correct
15 Correct 102 ms 186744 KB Output is correct
16 Correct 104 ms 184908 KB Output is correct
17 Correct 92 ms 187264 KB Output is correct
18 Correct 93 ms 185948 KB Output is correct
19 Correct 88 ms 186516 KB Output is correct
20 Correct 90 ms 186884 KB Output is correct
21 Correct 92 ms 186892 KB Output is correct
22 Correct 107 ms 188272 KB Output is correct
23 Correct 91 ms 187148 KB Output is correct
24 Correct 91 ms 187656 KB Output is correct
25 Correct 90 ms 187568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 852 KB Output is correct
2 Correct 1 ms 852 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 852 KB Output is correct
5 Correct 1 ms 852 KB Output is correct
6 Correct 1 ms 852 KB Output is correct
7 Correct 1 ms 852 KB Output is correct
8 Correct 1 ms 852 KB Output is correct
9 Correct 1 ms 852 KB Output is correct
10 Correct 1 ms 852 KB Output is correct
11 Correct 1 ms 852 KB Output is correct
12 Correct 1 ms 852 KB Output is correct
13 Correct 1 ms 980 KB Output is correct
14 Correct 1 ms 980 KB Output is correct
15 Correct 1 ms 980 KB Output is correct
16 Correct 1 ms 980 KB Output is correct
17 Correct 1 ms 980 KB Output is correct
18 Correct 1 ms 980 KB Output is correct
19 Correct 1 ms 980 KB Output is correct
20 Correct 1 ms 980 KB Output is correct
21 Correct 1 ms 980 KB Output is correct
22 Correct 1 ms 980 KB Output is correct
23 Correct 1 ms 980 KB Output is correct
24 Correct 1 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 3 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 852 KB Output is correct
14 Correct 1 ms 852 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 852 KB Output is correct
17 Correct 1 ms 852 KB Output is correct
18 Correct 1 ms 852 KB Output is correct
19 Correct 1 ms 852 KB Output is correct
20 Correct 1 ms 852 KB Output is correct
21 Correct 1 ms 852 KB Output is correct
22 Correct 1 ms 852 KB Output is correct
23 Correct 1 ms 852 KB Output is correct
24 Correct 1 ms 852 KB Output is correct
25 Correct 1 ms 980 KB Output is correct
26 Correct 1 ms 980 KB Output is correct
27 Correct 1 ms 980 KB Output is correct
28 Correct 1 ms 980 KB Output is correct
29 Correct 1 ms 980 KB Output is correct
30 Correct 1 ms 980 KB Output is correct
31 Correct 1 ms 980 KB Output is correct
32 Correct 1 ms 980 KB Output is correct
33 Correct 1 ms 980 KB Output is correct
34 Correct 1 ms 980 KB Output is correct
35 Correct 1 ms 980 KB Output is correct
36 Correct 1 ms 980 KB Output is correct
37 Correct 2 ms 2004 KB Output is correct
38 Correct 2 ms 3924 KB Output is correct
39 Correct 1 ms 1364 KB Output is correct
40 Correct 4 ms 8788 KB Output is correct
41 Correct 4 ms 8148 KB Output is correct
42 Correct 4 ms 8276 KB Output is correct
43 Correct 6 ms 8660 KB Output is correct
44 Correct 5 ms 8148 KB Output is correct
45 Correct 4 ms 8716 KB Output is correct
46 Correct 4 ms 8404 KB Output is correct
47 Correct 5 ms 8536 KB Output is correct
48 Correct 4 ms 8916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 3 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 19 ms 42964 KB Output is correct
14 Correct 67 ms 152812 KB Output is correct
15 Correct 20 ms 36960 KB Output is correct
16 Correct 82 ms 176292 KB Output is correct
17 Correct 77 ms 175036 KB Output is correct
18 Correct 77 ms 174260 KB Output is correct
19 Correct 77 ms 170720 KB Output is correct
20 Correct 78 ms 176276 KB Output is correct
21 Correct 78 ms 176468 KB Output is correct
22 Correct 76 ms 170608 KB Output is correct
23 Correct 77 ms 172816 KB Output is correct
24 Correct 78 ms 176520 KB Output is correct
25 Correct 86 ms 184772 KB Output is correct
26 Correct 88 ms 186240 KB Output is correct
27 Correct 102 ms 186744 KB Output is correct
28 Correct 104 ms 184908 KB Output is correct
29 Correct 92 ms 187264 KB Output is correct
30 Correct 93 ms 185948 KB Output is correct
31 Correct 88 ms 186516 KB Output is correct
32 Correct 90 ms 186884 KB Output is correct
33 Correct 92 ms 186892 KB Output is correct
34 Correct 107 ms 188272 KB Output is correct
35 Correct 91 ms 187148 KB Output is correct
36 Correct 91 ms 187656 KB Output is correct
37 Correct 90 ms 187568 KB Output is correct
38 Correct 1 ms 852 KB Output is correct
39 Correct 1 ms 852 KB Output is correct
40 Correct 1 ms 724 KB Output is correct
41 Correct 1 ms 852 KB Output is correct
42 Correct 1 ms 852 KB Output is correct
43 Correct 1 ms 852 KB Output is correct
44 Correct 1 ms 852 KB Output is correct
45 Correct 1 ms 852 KB Output is correct
46 Correct 1 ms 852 KB Output is correct
47 Correct 1 ms 852 KB Output is correct
48 Correct 1 ms 852 KB Output is correct
49 Correct 1 ms 852 KB Output is correct
50 Correct 1 ms 980 KB Output is correct
51 Correct 1 ms 980 KB Output is correct
52 Correct 1 ms 980 KB Output is correct
53 Correct 1 ms 980 KB Output is correct
54 Correct 1 ms 980 KB Output is correct
55 Correct 1 ms 980 KB Output is correct
56 Correct 1 ms 980 KB Output is correct
57 Correct 1 ms 980 KB Output is correct
58 Correct 1 ms 980 KB Output is correct
59 Correct 1 ms 980 KB Output is correct
60 Correct 1 ms 980 KB Output is correct
61 Correct 1 ms 980 KB Output is correct
62 Correct 2 ms 2004 KB Output is correct
63 Correct 2 ms 3924 KB Output is correct
64 Correct 1 ms 1364 KB Output is correct
65 Correct 4 ms 8788 KB Output is correct
66 Correct 4 ms 8148 KB Output is correct
67 Correct 4 ms 8276 KB Output is correct
68 Correct 6 ms 8660 KB Output is correct
69 Correct 5 ms 8148 KB Output is correct
70 Correct 4 ms 8716 KB Output is correct
71 Correct 4 ms 8404 KB Output is correct
72 Correct 5 ms 8536 KB Output is correct
73 Correct 4 ms 8916 KB Output is correct
74 Correct 50 ms 112376 KB Output is correct
75 Correct 12 ms 25684 KB Output is correct
76 Correct 30 ms 64756 KB Output is correct
77 Correct 77 ms 175580 KB Output is correct
78 Correct 81 ms 175948 KB Output is correct
79 Correct 77 ms 171988 KB Output is correct
80 Correct 86 ms 175612 KB Output is correct
81 Correct 78 ms 174128 KB Output is correct
82 Correct 90 ms 175628 KB Output is correct
83 Correct 77 ms 172224 KB Output is correct
84 Correct 75 ms 171408 KB Output is correct
85 Correct 78 ms 174412 KB Output is correct
86 Correct 223 ms 233884 KB Output is correct
87 Correct 216 ms 233776 KB Output is correct
88 Correct 217 ms 233184 KB Output is correct
89 Correct 218 ms 233560 KB Output is correct
90 Correct 210 ms 233172 KB Output is correct
91 Correct 217 ms 234092 KB Output is correct
92 Correct 217 ms 233952 KB Output is correct
93 Correct 216 ms 233540 KB Output is correct
94 Correct 223 ms 233820 KB Output is correct
95 Correct 221 ms 233548 KB Output is correct
96 Correct 215 ms 233468 KB Output is correct
97 Correct 216 ms 233112 KB Output is correct