Submission #934942

# Submission time Handle Problem Language Result Execution time Memory
934942 2024-02-28T08:13:39 Z MinaRagy06 Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
662 ms 48288 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
 
int dp[2][1 << 20], dp2[2][1 << 20];
string s;
int a[1 << 20];
int main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    int n, q;
    cin >> n >> q;
    int m = n;
    n = 1 << n;
    for (int i = 0; i < n; i++) {
        char c;
        cin >> c;
        a[i] = c - '0';
    }
    for (int i = 0; i < n; i++) dp[m & 1][i] = a[i], dp2[m & 1][i] = a[i];
    for (int i = m - 1; i >= 0; i--) {
        for (int j = 0; j < n; j++) {
            dp[i & 1][j] = dp[(i + 1) & 1][j];
            dp2[i & 1][j] = dp2[(i + 1) & 1][j];
            if ((j >> i) & 1) {
                dp[i & 1][j] += dp[(i + 1) & 1][j ^ (1 << i)];
            } else {
                dp2[i & 1][j] += dp2[(i + 1) & 1][j ^ (1 << i)];
            }
        }
    }
    while (q--) {
        cin >> s;
        int frq[3]{};
        for (int i = 0; i < m; i++) {
            if (s[i] == '?') {
                frq[2]++;
            } else {
                frq[s[i] - '0']++;
            }
        }
        reverse(s.begin(), s.end());
        int ans = 0;
        if (frq[2] <= frq[0] && frq[2] <= frq[1]) {
            int x = 0, st = 0;
            for (int i = 0; i < m; i++) {
                int b = s[i] - '0';
                if (s[i] == '?') st += 1 << i, b = 0;
                x += b << i;
            }
            for (int msk = st; msk >= 0; msk = ((msk - 1) & st)) {
                int v = x | msk;
                ans += a[v];
                if (!msk) break;
            }
        } else if (frq[1] <= frq[0] && frq[1] <= frq[2]) {
            int x = 0, st = 0;
            for (int i = 0; i < m; i++) {
                int b = s[i] - '0';
                if (s[i] == '?') b = 1;
                if (s[i] == '1') st += 1 << i, b = 0;
                x += b << i;
            }
            for (int msk = st; msk >= 0; msk = ((msk - 1) & st)) {
                int v = x | msk;
                int cnt = __builtin_popcount(msk);
                if ((frq[1] - cnt) & 1) {
                    ans -= dp[0][v];
                } else {
                    ans += dp[0][v];
                }
                if (!msk) break;
            }
        } else {
            int x = 0, st = 0;
            for (int i = 0; i < m; i++) {
                int b = s[i] - '0';
                if (s[i] == '?') b = 0;
                if (s[i] == '0') st += 1 << i;
                x += b << i;
            }
            for (int msk = st; msk >= 0; msk = ((msk - 1) & st)) {
                int v = x | msk;
                int cnt = __builtin_popcount(msk);
                if (cnt & 1) {
                    ans -= dp2[0][v];
                } else {
                    ans += dp2[0][v];
                }
                if (msk == 0) break;
            }
        }
        cout << ans << '\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 2 ms 8696 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 1 ms 8668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 2 ms 8696 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 1 ms 8668 KB Output is correct
11 Correct 176 ms 22352 KB Output is correct
12 Correct 210 ms 22072 KB Output is correct
13 Correct 204 ms 21256 KB Output is correct
14 Correct 184 ms 21300 KB Output is correct
15 Correct 205 ms 22252 KB Output is correct
16 Correct 221 ms 21628 KB Output is correct
17 Correct 210 ms 21324 KB Output is correct
18 Correct 183 ms 23192 KB Output is correct
19 Correct 130 ms 20296 KB Output is correct
20 Correct 209 ms 22212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 2 ms 8696 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 1 ms 8668 KB Output is correct
11 Correct 176 ms 22352 KB Output is correct
12 Correct 210 ms 22072 KB Output is correct
13 Correct 204 ms 21256 KB Output is correct
14 Correct 184 ms 21300 KB Output is correct
15 Correct 205 ms 22252 KB Output is correct
16 Correct 221 ms 21628 KB Output is correct
17 Correct 210 ms 21324 KB Output is correct
18 Correct 183 ms 23192 KB Output is correct
19 Correct 130 ms 20296 KB Output is correct
20 Correct 209 ms 22212 KB Output is correct
21 Correct 236 ms 25184 KB Output is correct
22 Correct 264 ms 25408 KB Output is correct
23 Correct 266 ms 24264 KB Output is correct
24 Correct 210 ms 24088 KB Output is correct
25 Correct 254 ms 26236 KB Output is correct
26 Correct 275 ms 24836 KB Output is correct
27 Correct 269 ms 24516 KB Output is correct
28 Correct 175 ms 27116 KB Output is correct
29 Correct 145 ms 23120 KB Output is correct
30 Correct 257 ms 25564 KB Output is correct
31 Correct 277 ms 25072 KB Output is correct
32 Correct 316 ms 25152 KB Output is correct
33 Correct 195 ms 23932 KB Output is correct
34 Correct 240 ms 24148 KB Output is correct
35 Correct 281 ms 24656 KB Output is correct
36 Correct 159 ms 23120 KB Output is correct
37 Correct 239 ms 23380 KB Output is correct
38 Correct 145 ms 21328 KB Output is correct
39 Correct 227 ms 22388 KB Output is correct
40 Correct 211 ms 22284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 2 ms 8696 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 1 ms 8668 KB Output is correct
11 Correct 60 ms 23124 KB Output is correct
12 Correct 59 ms 23256 KB Output is correct
13 Correct 66 ms 23120 KB Output is correct
14 Correct 77 ms 23116 KB Output is correct
15 Correct 58 ms 23120 KB Output is correct
16 Correct 72 ms 23124 KB Output is correct
17 Correct 77 ms 23124 KB Output is correct
18 Correct 52 ms 23380 KB Output is correct
19 Correct 52 ms 22868 KB Output is correct
20 Correct 57 ms 23148 KB Output is correct
21 Correct 62 ms 23252 KB Output is correct
22 Correct 67 ms 23124 KB Output is correct
23 Correct 56 ms 22988 KB Output is correct
24 Correct 65 ms 22984 KB Output is correct
25 Correct 73 ms 23120 KB Output is correct
26 Correct 53 ms 22868 KB Output is correct
27 Correct 62 ms 23240 KB Output is correct
28 Correct 53 ms 22868 KB Output is correct
29 Correct 71 ms 23120 KB Output is correct
30 Correct 60 ms 23120 KB Output is correct
31 Correct 55 ms 23004 KB Output is correct
32 Correct 70 ms 22980 KB Output is correct
33 Correct 89 ms 23124 KB Output is correct
34 Correct 50 ms 22868 KB Output is correct
35 Correct 64 ms 23000 KB Output is correct
36 Correct 63 ms 23124 KB Output is correct
37 Correct 67 ms 23124 KB Output is correct
38 Correct 84 ms 23308 KB Output is correct
39 Correct 70 ms 23128 KB Output is correct
40 Correct 63 ms 23120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 2 ms 8696 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 1 ms 8668 KB Output is correct
11 Correct 176 ms 22352 KB Output is correct
12 Correct 210 ms 22072 KB Output is correct
13 Correct 204 ms 21256 KB Output is correct
14 Correct 184 ms 21300 KB Output is correct
15 Correct 205 ms 22252 KB Output is correct
16 Correct 221 ms 21628 KB Output is correct
17 Correct 210 ms 21324 KB Output is correct
18 Correct 183 ms 23192 KB Output is correct
19 Correct 130 ms 20296 KB Output is correct
20 Correct 209 ms 22212 KB Output is correct
21 Correct 236 ms 25184 KB Output is correct
22 Correct 264 ms 25408 KB Output is correct
23 Correct 266 ms 24264 KB Output is correct
24 Correct 210 ms 24088 KB Output is correct
25 Correct 254 ms 26236 KB Output is correct
26 Correct 275 ms 24836 KB Output is correct
27 Correct 269 ms 24516 KB Output is correct
28 Correct 175 ms 27116 KB Output is correct
29 Correct 145 ms 23120 KB Output is correct
30 Correct 257 ms 25564 KB Output is correct
31 Correct 277 ms 25072 KB Output is correct
32 Correct 316 ms 25152 KB Output is correct
33 Correct 195 ms 23932 KB Output is correct
34 Correct 240 ms 24148 KB Output is correct
35 Correct 281 ms 24656 KB Output is correct
36 Correct 159 ms 23120 KB Output is correct
37 Correct 239 ms 23380 KB Output is correct
38 Correct 145 ms 21328 KB Output is correct
39 Correct 227 ms 22388 KB Output is correct
40 Correct 211 ms 22284 KB Output is correct
41 Correct 60 ms 23124 KB Output is correct
42 Correct 59 ms 23256 KB Output is correct
43 Correct 66 ms 23120 KB Output is correct
44 Correct 77 ms 23116 KB Output is correct
45 Correct 58 ms 23120 KB Output is correct
46 Correct 72 ms 23124 KB Output is correct
47 Correct 77 ms 23124 KB Output is correct
48 Correct 52 ms 23380 KB Output is correct
49 Correct 52 ms 22868 KB Output is correct
50 Correct 57 ms 23148 KB Output is correct
51 Correct 62 ms 23252 KB Output is correct
52 Correct 67 ms 23124 KB Output is correct
53 Correct 56 ms 22988 KB Output is correct
54 Correct 65 ms 22984 KB Output is correct
55 Correct 73 ms 23120 KB Output is correct
56 Correct 53 ms 22868 KB Output is correct
57 Correct 62 ms 23240 KB Output is correct
58 Correct 53 ms 22868 KB Output is correct
59 Correct 71 ms 23120 KB Output is correct
60 Correct 60 ms 23120 KB Output is correct
61 Correct 55 ms 23004 KB Output is correct
62 Correct 70 ms 22980 KB Output is correct
63 Correct 89 ms 23124 KB Output is correct
64 Correct 50 ms 22868 KB Output is correct
65 Correct 64 ms 23000 KB Output is correct
66 Correct 63 ms 23124 KB Output is correct
67 Correct 67 ms 23124 KB Output is correct
68 Correct 84 ms 23308 KB Output is correct
69 Correct 70 ms 23128 KB Output is correct
70 Correct 63 ms 23120 KB Output is correct
71 Correct 395 ms 42572 KB Output is correct
72 Correct 389 ms 42676 KB Output is correct
73 Correct 365 ms 44068 KB Output is correct
74 Correct 662 ms 44336 KB Output is correct
75 Correct 396 ms 46336 KB Output is correct
76 Correct 625 ms 44624 KB Output is correct
77 Correct 509 ms 44588 KB Output is correct
78 Correct 237 ms 48288 KB Output is correct
79 Correct 218 ms 42836 KB Output is correct
80 Correct 373 ms 45436 KB Output is correct
81 Correct 487 ms 45412 KB Output is correct
82 Correct 577 ms 44412 KB Output is correct
83 Correct 310 ms 43600 KB Output is correct
84 Correct 445 ms 44280 KB Output is correct
85 Correct 503 ms 44660 KB Output is correct
86 Correct 219 ms 42204 KB Output is correct
87 Correct 368 ms 45396 KB Output is correct
88 Correct 210 ms 42324 KB Output is correct
89 Correct 352 ms 41044 KB Output is correct
90 Correct 410 ms 41488 KB Output is correct
91 Correct 317 ms 40684 KB Output is correct
92 Correct 542 ms 41808 KB Output is correct
93 Correct 496 ms 41576 KB Output is correct
94 Correct 231 ms 41324 KB Output is correct
95 Correct 438 ms 44356 KB Output is correct
96 Correct 482 ms 44392 KB Output is correct
97 Correct 486 ms 44316 KB Output is correct
98 Correct 436 ms 44472 KB Output is correct
99 Correct 435 ms 44368 KB Output is correct
100 Correct 428 ms 44468 KB Output is correct