답안 #781785

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
781785 2023-07-13T10:53:08 Z flappybird Solitaire (JOI16_solitaire) C++17
80 / 100
214 ms 217952 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];
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++) {
      |              ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 712 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 596 KB Output is correct
7 Correct 1 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 480 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 1 ms 588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 42836 KB Output is correct
2 Correct 78 ms 152616 KB Output is correct
3 Correct 18 ms 36820 KB Output is correct
4 Correct 76 ms 175964 KB Output is correct
5 Correct 76 ms 174736 KB Output is correct
6 Correct 84 ms 174028 KB Output is correct
7 Correct 77 ms 170544 KB Output is correct
8 Correct 84 ms 176180 KB Output is correct
9 Correct 80 ms 176348 KB Output is correct
10 Correct 76 ms 170324 KB Output is correct
11 Correct 91 ms 172620 KB Output is correct
12 Correct 96 ms 176228 KB Output is correct
13 Correct 86 ms 184560 KB Output is correct
14 Correct 88 ms 186044 KB Output is correct
15 Correct 89 ms 186568 KB Output is correct
16 Correct 104 ms 184680 KB Output is correct
17 Correct 90 ms 186956 KB Output is correct
18 Correct 89 ms 185660 KB Output is correct
19 Correct 91 ms 186188 KB Output is correct
20 Correct 95 ms 186532 KB Output is correct
21 Correct 91 ms 186736 KB Output is correct
22 Correct 93 ms 188028 KB Output is correct
23 Correct 89 ms 186920 KB Output is correct
24 Correct 96 ms 187452 KB Output is correct
25 Correct 93 ms 187368 KB Output is correct
# 결과 실행 시간 메모리 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 840 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 844 KB Output is correct
13 Correct 1 ms 968 KB Output is correct
14 Correct 1 ms 968 KB Output is correct
15 Correct 1 ms 980 KB Output is correct
16 Correct 1 ms 968 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 972 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 972 KB Output is correct
23 Correct 1 ms 980 KB Output is correct
24 Correct 1 ms 980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 712 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 596 KB Output is correct
7 Correct 1 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 480 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 1 ms 588 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 840 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 844 KB Output is correct
25 Correct 1 ms 968 KB Output is correct
26 Correct 1 ms 968 KB Output is correct
27 Correct 1 ms 980 KB Output is correct
28 Correct 1 ms 968 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 972 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 972 KB Output is correct
35 Correct 1 ms 980 KB Output is correct
36 Correct 1 ms 980 KB Output is correct
37 Correct 1 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 4 ms 8532 KB Output is correct
44 Correct 4 ms 8148 KB Output is correct
45 Correct 4 ms 8660 KB Output is correct
46 Correct 4 ms 8404 KB Output is correct
47 Correct 4 ms 8532 KB Output is correct
48 Correct 4 ms 8808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 712 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 596 KB Output is correct
7 Correct 1 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 480 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 1 ms 588 KB Output is correct
13 Correct 21 ms 42836 KB Output is correct
14 Correct 78 ms 152616 KB Output is correct
15 Correct 18 ms 36820 KB Output is correct
16 Correct 76 ms 175964 KB Output is correct
17 Correct 76 ms 174736 KB Output is correct
18 Correct 84 ms 174028 KB Output is correct
19 Correct 77 ms 170544 KB Output is correct
20 Correct 84 ms 176180 KB Output is correct
21 Correct 80 ms 176348 KB Output is correct
22 Correct 76 ms 170324 KB Output is correct
23 Correct 91 ms 172620 KB Output is correct
24 Correct 96 ms 176228 KB Output is correct
25 Correct 86 ms 184560 KB Output is correct
26 Correct 88 ms 186044 KB Output is correct
27 Correct 89 ms 186568 KB Output is correct
28 Correct 104 ms 184680 KB Output is correct
29 Correct 90 ms 186956 KB Output is correct
30 Correct 89 ms 185660 KB Output is correct
31 Correct 91 ms 186188 KB Output is correct
32 Correct 95 ms 186532 KB Output is correct
33 Correct 91 ms 186736 KB Output is correct
34 Correct 93 ms 188028 KB Output is correct
35 Correct 89 ms 186920 KB Output is correct
36 Correct 96 ms 187452 KB Output is correct
37 Correct 93 ms 187368 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 840 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 844 KB Output is correct
50 Correct 1 ms 968 KB Output is correct
51 Correct 1 ms 968 KB Output is correct
52 Correct 1 ms 980 KB Output is correct
53 Correct 1 ms 968 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 972 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 972 KB Output is correct
60 Correct 1 ms 980 KB Output is correct
61 Correct 1 ms 980 KB Output is correct
62 Correct 1 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 4 ms 8532 KB Output is correct
69 Correct 4 ms 8148 KB Output is correct
70 Correct 4 ms 8660 KB Output is correct
71 Correct 4 ms 8404 KB Output is correct
72 Correct 4 ms 8532 KB Output is correct
73 Correct 4 ms 8808 KB Output is correct
74 Correct 51 ms 112072 KB Output is correct
75 Correct 12 ms 25696 KB Output is correct
76 Correct 30 ms 64648 KB Output is correct
77 Correct 77 ms 175384 KB Output is correct
78 Correct 77 ms 175688 KB Output is correct
79 Correct 76 ms 171772 KB Output is correct
80 Correct 78 ms 175320 KB Output is correct
81 Correct 78 ms 173876 KB Output is correct
82 Correct 77 ms 175324 KB Output is correct
83 Correct 76 ms 172096 KB Output is correct
84 Correct 75 ms 171220 KB Output is correct
85 Correct 77 ms 174184 KB Output is correct
86 Incorrect 214 ms 217952 KB Output isn't correct
87 Halted 0 ms 0 KB -