Submission #49104

# Submission time Handle Problem Language Result Execution time Memory
49104 2018-05-22T09:58:18 Z aome Bowling (BOI15_bow) C++17
100 / 100
113 ms 956 KB
#include <bits/stdc++.h>

using namespace std;

int n;
int score[10];
string s;
long long f[10][305][4];

void solve() {
	cin >> n >> s;
	for (int i = 0; i < n; ++i) cin >> score[i];
	memset(f, 0, sizeof f);

	// 0 -> i - 1 ko duoc 10
	// 1 -> i - 1 duoc 10
	// 2 -> i - 1 duoc strike
 	// 3 -> i - 1 duoc strike va i - 2 duoc strike

	f[0][0][0] = 1;

	for (int i = 0; i < (n - 1); ++i) {
		for (int j = 0; j <= 300; ++j) {
			for (int k = 0; k < 4; ++k) {
				if (!f[i][j][k]) continue;

				// cout << i << ' ' << j << ' ' << k << '\n';

				char c1 = s[i * 2], c2 = s[i * 2 + 1];

				for (int m1 = 0; m1 <= 10; ++m1) {
					if (c1 == 'x' || m1 == 10) {
						if (m1 != 10 || c1 != '?' && c1 != 'x') continue;
					}
					else if (c1 != '?') {
						if (m1 != c1 - '0' || c1 != '?' && (c1 < '0' || c1 > '9')) continue;
					}

					for (int m2 = 0; m2 + m1 <= 10; ++m2) {
						if (c2 == '-' || m1 == 10) {
							if (m1 != 10 || c2 != '?' && c2 != '-') continue;
						}
						else if (c2 == '/' || m1 + m2 == 10) {
							if (m1 + m2 != 10 || c2 != '?' && c2 != '/') continue;
						}
						else if (c2 != '?') {
							if (m2 != c2 - '0' || c2 != '?' && (c2 < '0' || c2 > '9')) continue;
						}

						if (k == 0) {
							if (m1 == 10) {
								f[i + 1][j + 10][2] += f[i][j][k];
							}
							else if (m1 + m2 == 10) {
								f[i + 1][j + 10][1] += f[i][j][k];
							}
							else {
								if (score[i] != -1 && score[i] != j + m1 + m2) continue;
								f[i + 1][j + m1 + m2][0] += f[i][j][k];
							}
						}

						if (k == 1) {
							if (m1 == 10) {
								if (score[i - 1] != -1 && score[i - 1] != j + 10) continue;
								f[i + 1][j + 10 + 10][2] += f[i][j][k];
							}
							else if (m1 + m2 == 10) {
								if (score[i - 1] != -1 && score[i - 1] != j + m1) continue;
								f[i + 1][j + 10 + m1][1] += f[i][j][k];
							}
							else {
								if (score[i - 1] != -1 && score[i - 1] != j + m1) continue;
								if (score[i] != -1 && score[i] != j + m1 + m2 + m1) continue;
								f[i + 1][j + m1 + m2 + m1][0] += f[i][j][k];
							}
						}

						if (k == 2) {
							if (m1 == 10) {
								f[i + 1][j + 10][3] += f[i][j][k];
							}
							else if (m1 + m2 == 10) {
								if (score[i - 1] != -1 && score[i - 1] != j + 10) continue;
								f[i + 1][j + 10 + 10][1] += f[i][j][k];
							}
							else {
								if (score[i - 1] != -1 && score[i - 1] != j + m1 + m2) continue;
								if (score[i] != -1 && score[i] != j + m1 + m2 + m1 + m2) continue;
								f[i + 1][j + m1 + m2 + m1 + m2][0] += f[i][j][k];
							}
						}

						if (k == 3) {
							if (m1 == 10) {
								if (score[i - 2] != -1 && score[i - 2] != (j - 10) + 10 + 10) continue;
								f[i + 1][j + 10 + 10 + 10][3] += f[i][j][k];
							}
							else if (m1 + m2 == 10) {
								if (score[i - 2] != -1 && score[i - 2] != (j - 10) + 10 + m1) continue;
								if (score[i - 1] != -1 && score[i - 1] != j + 10 + m1 + 10) continue;
								f[i + 1][j + 10 + m1 + 10 + 10][1] += f[i][j][k]; 
							}
							else {
								if (score[i - 2] != -1 && score[i - 2] != (j - 10) + 10 + m1) continue;
								if (score[i - 1] != -1 && score[i - 1] != j + 10 + m1 + m1 + m2) continue;
								if (score[i] != -1 && score[i] != j + 10 + m1 + m1 + m2 + m1 + m2) continue;
								f[i + 1][j + 10 + m1 + m1 + m2 + m1 + m2][0] += f[i][j][k];						
							}
						}
					}
				}
			}
		}
	}

	long long res = 0;

	for (int i = 0; i <= 300; ++i) {
		for (int j = 0; j < 4; ++j) {
			if (!f[n - 1][i][j]) continue;
			char c1 = s[(n - 1) * 2], c2 = s[(n - 1) * 2 + 1], c3 = s[(n - 1) * 2 + 2];

			// cout << i << ' ' << j << ' ' << f[n - 1][i][j] << '\n';

			for (int m1 = 0; m1 <= 10; ++m1) {
				if (c1 == 'x' || m1 == 10) {
					if (m1 != 10 || c1 != '?' && c1 != 'x') continue;
				}
				else if (c1 != '?') {
					if (m1 != c1 - '0' || c1 != '?' && (c1 < '0' || c1 > '9')) continue;
				}

				// cout << m1 << '\n';

				int lim1 = 0;
				if (m1 == 10) lim1 = 10;
				else lim1 = 10 - m1;

				for (int m2 = 0; m2 <= lim1; ++m2) {
					if (c2 == 'x' || m1 == 10 && m2 == 10) {
						if (m1 != 10 || m2 != 10 || c2 != '?' && c2 != 'x') continue;
					}
					else if (c2 == '/' || m1 != 10 && m1 + m2 == 10) {
						if (m1 == 10 || m1 + m2 != 10 || c2 != '?' && c2 != '/') continue;
					}
					else if (c2 != '?') {
						if (m2 != c2 - '0' || c2 != '?' && (c2 < '0' || c2 > '9')) continue;
					}

					// cout << m1 << ' ' << m2 << '\n';

					int lim2 = 0;
					if (m1 == 10 && m2 == 10) lim2 = 10;
					else if (m1 == 10) lim2 = 10 - m2;
					else if (m1 + m2 == 10) lim2 = 10;

					for (int m3 = 0; m3 <= lim2; ++m3) {
						if (c3 == 'x' || ((m1 + m2 == 10 && m1 != 10) || m1 + m2 == 20) && m3 == 10) {
							if ((m1 + m2 != 10 || m1 == 10) && m1 + m2 != 20 || m3 != 10 || c3 != '?' && c3 != 'x') continue;
						}
						else if (c3 == '/' || m1 == 10 && m2 != 10 && m2 + m3 == 10) {
							if (m1 != 10 || m2 == 10 || m2 + m3 != 10 || c3 != '?' && c3 != '/') continue;
						}
						else if (c3 == '-' || m1 + m2 < 10) {
							if (m1 + m2 >= 10 || c3 != '?' && c3 != '-') continue;
						}
						else if (c3 != '?') {
							if (m3 != c3 - '0' || c3 != '?' && (c3 < '0' || c3 > '9')) continue;
						}

						// cout << m1 << ' ' << m2 << ' ' << m3 << '\n';

						if (j == 0) {
							if (score[n - 1] != -1 && i + m1 + m2 + m3 != score[n - 1]) continue;
							res += f[n - 1][i][j];
						}

						if (j == 1) {
							if (score[n - 2] != -1 && i + m1 != score[n - 2]) continue;
							if (score[n - 1] != -1 && i + m1 + m1 + m2 + m3 != score[n - 1]) continue;
							res += f[n - 1][i][j];
						}

						if (j == 2) {
							if (score[n - 2] != -1 && i + m1 + m2 != score[n - 2]) continue;
							if (score[n - 1] != -1 && i + m1 + m2 + m1 + m2 + m3 != score[n - 1]) continue;
							res += f[n - 1][i][j];
						}

						if (j == 3) {
							if (score[n - 3] != -1 && (i - 10) + 10 + m1 != score[n - 3]) continue;
							if (score[n - 2] != -1 && i + 10 + m1 + m1 + m2 != score[n - 2]) continue;
							if (score[n - 1] != -1 && i + 10 + m1 + m1 + m2 + m1 + m2 + m3 != score[n - 1]) continue;
							res += f[n - 1][i][j];
						}
					}
				}
			}
		}
	}

	cout << res << '\n';
}

int main() {
	ios::sync_with_stdio(false);
	int t; cin >> t; while (t--) solve();
}

Compilation message

bow.cpp: In function 'void solve()':
bow.cpp:33:33: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
       if (m1 != 10 || c1 != '?' && c1 != 'x') continue;
                       ~~~~~~~~~~^~~~~~~~~~~~
bow.cpp:36:39: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
       if (m1 != c1 - '0' || c1 != '?' && (c1 < '0' || c1 > '9')) continue;
                             ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
bow.cpp:41:34: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
        if (m1 != 10 || c2 != '?' && c2 != '-') continue;
                        ~~~~~~~~~~^~~~~~~~~~~~
bow.cpp:44:39: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
        if (m1 + m2 != 10 || c2 != '?' && c2 != '/') continue;
                             ~~~~~~~~~~^~~~~~~~~~~~
bow.cpp:47:40: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
        if (m2 != c2 - '0' || c2 != '?' && (c2 < '0' || c2 > '9')) continue;
                              ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
bow.cpp:128:32: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
      if (m1 != 10 || c1 != '?' && c1 != 'x') continue;
                      ~~~~~~~~~~^~~~~~~~~~~~
bow.cpp:131:38: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
      if (m1 != c1 - '0' || c1 != '?' && (c1 < '0' || c1 > '9')) continue;
                            ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
bow.cpp:141:32: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
      if (c2 == 'x' || m1 == 10 && m2 == 10) {
                       ~~~~~~~~~^~~~~~~~~~~
bow.cpp:142:45: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
       if (m1 != 10 || m2 != 10 || c2 != '?' && c2 != 'x') continue;
                                   ~~~~~~~~~~^~~~~~~~~~~~
bow.cpp:144:37: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
      else if (c2 == '/' || m1 != 10 && m1 + m2 == 10) {
                            ~~~~~~~~~^~~~~~~~~~~~~~~~
bow.cpp:145:50: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
       if (m1 == 10 || m1 + m2 != 10 || c2 != '?' && c2 != '/') continue;
                                        ~~~~~~~~~~^~~~~~~~~~~~
bow.cpp:148:39: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
       if (m2 != c2 - '0' || c2 != '?' && (c2 < '0' || c2 > '9')) continue;
                             ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
bow.cpp:159:71: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
       if (c3 == 'x' || ((m1 + m2 == 10 && m1 != 10) || m1 + m2 == 20) && m3 == 10) {
                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
bow.cpp:160:40: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
        if ((m1 + m2 != 10 || m1 == 10) && m1 + m2 != 20 || m3 != 10 || c3 != '?' && c3 != 'x') continue;
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
bow.cpp:160:82: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
        if ((m1 + m2 != 10 || m1 == 10) && m1 + m2 != 20 || m3 != 10 || c3 != '?' && c3 != 'x') continue;
                                                                        ~~~~~~~~~~^~~~~~~~~~~~
bow.cpp:162:50: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
       else if (c3 == '/' || m1 == 10 && m2 != 10 && m2 + m3 == 10) {
                             ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
bow.cpp:163:63: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
        if (m1 != 10 || m2 == 10 || m2 + m3 != 10 || c3 != '?' && c3 != '/') continue;
                                                     ~~~~~~~~~~^~~~~~~~~~~~
bow.cpp:166:39: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
        if (m1 + m2 >= 10 || c3 != '?' && c3 != '-') continue;
                             ~~~~~~~~~~^~~~~~~~~~~~
bow.cpp:169:40: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
        if (m3 != c3 - '0' || c3 != '?' && (c3 < '0' || c3 > '9')) continue;
                              ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 488 KB Output is correct
3 Correct 3 ms 560 KB Output is correct
4 Correct 3 ms 672 KB Output is correct
5 Correct 2 ms 768 KB Output is correct
6 Correct 3 ms 768 KB Output is correct
7 Correct 3 ms 768 KB Output is correct
8 Correct 3 ms 868 KB Output is correct
9 Correct 3 ms 868 KB Output is correct
10 Correct 3 ms 888 KB Output is correct
11 Correct 3 ms 888 KB Output is correct
12 Correct 3 ms 888 KB Output is correct
13 Correct 3 ms 888 KB Output is correct
14 Correct 4 ms 888 KB Output is correct
15 Correct 2 ms 888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 888 KB Output is correct
2 Correct 5 ms 888 KB Output is correct
3 Correct 4 ms 888 KB Output is correct
4 Correct 3 ms 888 KB Output is correct
5 Correct 5 ms 888 KB Output is correct
6 Correct 4 ms 888 KB Output is correct
7 Correct 5 ms 888 KB Output is correct
8 Correct 4 ms 888 KB Output is correct
9 Correct 3 ms 888 KB Output is correct
10 Correct 10 ms 888 KB Output is correct
11 Correct 10 ms 888 KB Output is correct
12 Correct 8 ms 888 KB Output is correct
13 Correct 12 ms 888 KB Output is correct
14 Correct 5 ms 896 KB Output is correct
15 Correct 5 ms 896 KB Output is correct
16 Correct 4 ms 896 KB Output is correct
17 Correct 5 ms 896 KB Output is correct
18 Correct 13 ms 896 KB Output is correct
19 Correct 11 ms 896 KB Output is correct
20 Correct 14 ms 896 KB Output is correct
21 Correct 13 ms 896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 896 KB Output is correct
2 Correct 3 ms 904 KB Output is correct
3 Correct 3 ms 904 KB Output is correct
4 Correct 3 ms 904 KB Output is correct
5 Correct 3 ms 904 KB Output is correct
6 Correct 3 ms 904 KB Output is correct
7 Correct 3 ms 904 KB Output is correct
8 Correct 3 ms 904 KB Output is correct
9 Correct 3 ms 904 KB Output is correct
10 Correct 4 ms 904 KB Output is correct
11 Correct 4 ms 904 KB Output is correct
12 Correct 4 ms 904 KB Output is correct
13 Correct 3 ms 904 KB Output is correct
14 Correct 4 ms 904 KB Output is correct
15 Correct 4 ms 904 KB Output is correct
16 Correct 3 ms 904 KB Output is correct
17 Correct 4 ms 904 KB Output is correct
18 Correct 3 ms 904 KB Output is correct
19 Correct 3 ms 904 KB Output is correct
20 Correct 3 ms 904 KB Output is correct
21 Correct 3 ms 904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 904 KB Output is correct
2 Correct 4 ms 904 KB Output is correct
3 Correct 4 ms 904 KB Output is correct
4 Correct 3 ms 904 KB Output is correct
5 Correct 5 ms 904 KB Output is correct
6 Correct 3 ms 904 KB Output is correct
7 Correct 3 ms 904 KB Output is correct
8 Correct 4 ms 904 KB Output is correct
9 Correct 3 ms 904 KB Output is correct
10 Correct 9 ms 904 KB Output is correct
11 Correct 12 ms 908 KB Output is correct
12 Correct 9 ms 908 KB Output is correct
13 Correct 9 ms 908 KB Output is correct
14 Correct 3 ms 908 KB Output is correct
15 Correct 3 ms 908 KB Output is correct
16 Correct 4 ms 908 KB Output is correct
17 Correct 3 ms 908 KB Output is correct
18 Correct 2 ms 908 KB Output is correct
19 Correct 4 ms 908 KB Output is correct
20 Correct 3 ms 908 KB Output is correct
21 Correct 4 ms 908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 488 KB Output is correct
3 Correct 3 ms 560 KB Output is correct
4 Correct 3 ms 672 KB Output is correct
5 Correct 2 ms 768 KB Output is correct
6 Correct 3 ms 768 KB Output is correct
7 Correct 3 ms 768 KB Output is correct
8 Correct 3 ms 868 KB Output is correct
9 Correct 3 ms 868 KB Output is correct
10 Correct 3 ms 888 KB Output is correct
11 Correct 3 ms 888 KB Output is correct
12 Correct 3 ms 888 KB Output is correct
13 Correct 3 ms 888 KB Output is correct
14 Correct 4 ms 888 KB Output is correct
15 Correct 2 ms 888 KB Output is correct
16 Correct 7 ms 888 KB Output is correct
17 Correct 5 ms 888 KB Output is correct
18 Correct 4 ms 888 KB Output is correct
19 Correct 3 ms 888 KB Output is correct
20 Correct 5 ms 888 KB Output is correct
21 Correct 4 ms 888 KB Output is correct
22 Correct 5 ms 888 KB Output is correct
23 Correct 4 ms 888 KB Output is correct
24 Correct 3 ms 888 KB Output is correct
25 Correct 10 ms 888 KB Output is correct
26 Correct 10 ms 888 KB Output is correct
27 Correct 8 ms 888 KB Output is correct
28 Correct 12 ms 888 KB Output is correct
29 Correct 5 ms 896 KB Output is correct
30 Correct 5 ms 896 KB Output is correct
31 Correct 4 ms 896 KB Output is correct
32 Correct 5 ms 896 KB Output is correct
33 Correct 13 ms 896 KB Output is correct
34 Correct 11 ms 896 KB Output is correct
35 Correct 14 ms 896 KB Output is correct
36 Correct 13 ms 896 KB Output is correct
37 Correct 8 ms 896 KB Output is correct
38 Correct 3 ms 904 KB Output is correct
39 Correct 3 ms 904 KB Output is correct
40 Correct 3 ms 904 KB Output is correct
41 Correct 3 ms 904 KB Output is correct
42 Correct 3 ms 904 KB Output is correct
43 Correct 3 ms 904 KB Output is correct
44 Correct 3 ms 904 KB Output is correct
45 Correct 3 ms 904 KB Output is correct
46 Correct 4 ms 904 KB Output is correct
47 Correct 4 ms 904 KB Output is correct
48 Correct 4 ms 904 KB Output is correct
49 Correct 3 ms 904 KB Output is correct
50 Correct 4 ms 904 KB Output is correct
51 Correct 4 ms 904 KB Output is correct
52 Correct 3 ms 904 KB Output is correct
53 Correct 4 ms 904 KB Output is correct
54 Correct 3 ms 904 KB Output is correct
55 Correct 3 ms 904 KB Output is correct
56 Correct 3 ms 904 KB Output is correct
57 Correct 3 ms 904 KB Output is correct
58 Correct 6 ms 904 KB Output is correct
59 Correct 4 ms 904 KB Output is correct
60 Correct 4 ms 904 KB Output is correct
61 Correct 3 ms 904 KB Output is correct
62 Correct 5 ms 904 KB Output is correct
63 Correct 3 ms 904 KB Output is correct
64 Correct 3 ms 904 KB Output is correct
65 Correct 4 ms 904 KB Output is correct
66 Correct 3 ms 904 KB Output is correct
67 Correct 9 ms 904 KB Output is correct
68 Correct 12 ms 908 KB Output is correct
69 Correct 9 ms 908 KB Output is correct
70 Correct 9 ms 908 KB Output is correct
71 Correct 3 ms 908 KB Output is correct
72 Correct 3 ms 908 KB Output is correct
73 Correct 4 ms 908 KB Output is correct
74 Correct 3 ms 908 KB Output is correct
75 Correct 2 ms 908 KB Output is correct
76 Correct 4 ms 908 KB Output is correct
77 Correct 3 ms 908 KB Output is correct
78 Correct 4 ms 908 KB Output is correct
79 Correct 16 ms 908 KB Output is correct
80 Correct 6 ms 928 KB Output is correct
81 Correct 6 ms 928 KB Output is correct
82 Correct 4 ms 928 KB Output is correct
83 Correct 5 ms 928 KB Output is correct
84 Correct 3 ms 928 KB Output is correct
85 Correct 4 ms 928 KB Output is correct
86 Correct 4 ms 928 KB Output is correct
87 Correct 4 ms 928 KB Output is correct
88 Correct 9 ms 928 KB Output is correct
89 Correct 9 ms 928 KB Output is correct
90 Correct 8 ms 928 KB Output is correct
91 Correct 10 ms 928 KB Output is correct
92 Correct 5 ms 928 KB Output is correct
93 Correct 92 ms 928 KB Output is correct
94 Correct 84 ms 932 KB Output is correct
95 Correct 88 ms 936 KB Output is correct
96 Correct 82 ms 940 KB Output is correct
97 Correct 87 ms 944 KB Output is correct
98 Correct 113 ms 948 KB Output is correct
99 Correct 89 ms 952 KB Output is correct
100 Correct 86 ms 956 KB Output is correct