Submission #557059

# Submission time Handle Problem Language Result Execution time Memory
557059 2022-05-04T16:46:22 Z Jomnoi Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
812 ms 47408 KB
#include <bits/stdc++.h>
using namespace std;

const int MAX_L = 20;
const int MAX_N = (1<<MAX_L);

int S[MAX_N];
int super[MAX_N], sub[MAX_N];
int cnt_bit[MAX_N];

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int L, Q;
    string str;
    cin >> L >> Q >> str;
    for(int i = 0; i < (1<<L); i++) {
        S[i] = super[i] = sub[i] = str[i] - '0';
        cnt_bit[i] = __builtin_popcount(i);
    }

    for(int i = 0; i < L; i++) {
        for(int mask = 0; mask < (1<<L); mask++) {
            if(mask & (1<<i)) {
                sub[mask] += sub[mask ^ (1<<i)];
            }
            else {
                super[mask] += super[mask ^ (1<<i)];
            }
        }
    }

    while(Q--) {
        string t;
        cin >> t;
        reverse(t.begin(), t.end());

        int A = 0, B = 0, C = 0;
        int cntA = 0, cntB = 0, cntC = 0;
        for(int i = 0; i < L; i++) {
            if(t[i] == '0') {
                A |= (1<<i);
                cntA++;
            }
            else if(t[i] == '1') {
                B |= (1<<i);
                cntB++;
            }
            else { // t[i] == '?'
                C |= (1<<i);
                cntC++;
            }
        }

        int ans = 0;
        if(cntA <= min(cntB, cntC)) {
            for(int mask = A;; mask = (mask - 1) & A) {
                if(cnt_bit[mask] % 2 == 0) {
                    ans += super[B | mask];
                }
                else {
                    ans -= super[B | mask];
                }

                if(mask == 0) {
                    break;
                }
            }
        }
        else if(cntB <= min(cntA, cntC)) {
            for(int mask = B;; mask = (mask - 1) & B) {
                if(cnt_bit[mask] % 2 == 0) {
                    ans += sub[C | (mask ^ B)];
                }
                else {
                    ans -= sub[C | (mask ^ B)];
                }

                if(mask == 0) {
                    break;
                }
            }
        }
        else if(cntC <= min(cntA, cntB)) {
            for(int mask = C;; mask = (mask - 1) & C) {
                ans += S[mask | B];

                if(mask == 0) {
                    break;
                }
            }
        }

        cout << ans << '\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 214 ms 15040 KB Output is correct
12 Correct 211 ms 14704 KB Output is correct
13 Correct 234 ms 14032 KB Output is correct
14 Correct 241 ms 14224 KB Output is correct
15 Correct 209 ms 15124 KB Output is correct
16 Correct 229 ms 14384 KB Output is correct
17 Correct 238 ms 14264 KB Output is correct
18 Correct 167 ms 16024 KB Output is correct
19 Correct 192 ms 13004 KB Output is correct
20 Correct 213 ms 14708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 214 ms 15040 KB Output is correct
12 Correct 211 ms 14704 KB Output is correct
13 Correct 234 ms 14032 KB Output is correct
14 Correct 241 ms 14224 KB Output is correct
15 Correct 209 ms 15124 KB Output is correct
16 Correct 229 ms 14384 KB Output is correct
17 Correct 238 ms 14264 KB Output is correct
18 Correct 167 ms 16024 KB Output is correct
19 Correct 192 ms 13004 KB Output is correct
20 Correct 213 ms 14708 KB Output is correct
21 Correct 228 ms 18124 KB Output is correct
22 Correct 238 ms 18304 KB Output is correct
23 Correct 274 ms 17396 KB Output is correct
24 Correct 283 ms 17216 KB Output is correct
25 Correct 275 ms 19108 KB Output is correct
26 Correct 287 ms 17612 KB Output is correct
27 Correct 291 ms 17652 KB Output is correct
28 Correct 176 ms 20108 KB Output is correct
29 Correct 222 ms 16340 KB Output is correct
30 Correct 231 ms 18380 KB Output is correct
31 Correct 256 ms 18252 KB Output is correct
32 Correct 275 ms 18212 KB Output is correct
33 Correct 233 ms 17040 KB Output is correct
34 Correct 276 ms 17188 KB Output is correct
35 Correct 284 ms 17616 KB Output is correct
36 Correct 183 ms 16188 KB Output is correct
37 Correct 222 ms 18100 KB Output is correct
38 Correct 241 ms 16104 KB Output is correct
39 Correct 264 ms 17432 KB Output is correct
40 Correct 269 ms 17228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 51 ms 20076 KB Output is correct
12 Correct 46 ms 20180 KB Output is correct
13 Correct 56 ms 20056 KB Output is correct
14 Correct 63 ms 20056 KB Output is correct
15 Correct 48 ms 20144 KB Output is correct
16 Correct 66 ms 20076 KB Output is correct
17 Correct 67 ms 20076 KB Output is correct
18 Correct 42 ms 20264 KB Output is correct
19 Correct 47 ms 19964 KB Output is correct
20 Correct 49 ms 20204 KB Output is correct
21 Correct 54 ms 20100 KB Output is correct
22 Correct 61 ms 20072 KB Output is correct
23 Correct 51 ms 20076 KB Output is correct
24 Correct 77 ms 20148 KB Output is correct
25 Correct 65 ms 20044 KB Output is correct
26 Correct 43 ms 19916 KB Output is correct
27 Correct 51 ms 20264 KB Output is correct
28 Correct 48 ms 20020 KB Output is correct
29 Correct 57 ms 20016 KB Output is correct
30 Correct 56 ms 20080 KB Output is correct
31 Correct 50 ms 20048 KB Output is correct
32 Correct 69 ms 20092 KB Output is correct
33 Correct 63 ms 20172 KB Output is correct
34 Correct 42 ms 19948 KB Output is correct
35 Correct 61 ms 20056 KB Output is correct
36 Correct 68 ms 20044 KB Output is correct
37 Correct 64 ms 20088 KB Output is correct
38 Correct 62 ms 20076 KB Output is correct
39 Correct 61 ms 20048 KB Output is correct
40 Correct 59 ms 20076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 214 ms 15040 KB Output is correct
12 Correct 211 ms 14704 KB Output is correct
13 Correct 234 ms 14032 KB Output is correct
14 Correct 241 ms 14224 KB Output is correct
15 Correct 209 ms 15124 KB Output is correct
16 Correct 229 ms 14384 KB Output is correct
17 Correct 238 ms 14264 KB Output is correct
18 Correct 167 ms 16024 KB Output is correct
19 Correct 192 ms 13004 KB Output is correct
20 Correct 213 ms 14708 KB Output is correct
21 Correct 228 ms 18124 KB Output is correct
22 Correct 238 ms 18304 KB Output is correct
23 Correct 274 ms 17396 KB Output is correct
24 Correct 283 ms 17216 KB Output is correct
25 Correct 275 ms 19108 KB Output is correct
26 Correct 287 ms 17612 KB Output is correct
27 Correct 291 ms 17652 KB Output is correct
28 Correct 176 ms 20108 KB Output is correct
29 Correct 222 ms 16340 KB Output is correct
30 Correct 231 ms 18380 KB Output is correct
31 Correct 256 ms 18252 KB Output is correct
32 Correct 275 ms 18212 KB Output is correct
33 Correct 233 ms 17040 KB Output is correct
34 Correct 276 ms 17188 KB Output is correct
35 Correct 284 ms 17616 KB Output is correct
36 Correct 183 ms 16188 KB Output is correct
37 Correct 222 ms 18100 KB Output is correct
38 Correct 241 ms 16104 KB Output is correct
39 Correct 264 ms 17432 KB Output is correct
40 Correct 269 ms 17228 KB Output is correct
41 Correct 51 ms 20076 KB Output is correct
42 Correct 46 ms 20180 KB Output is correct
43 Correct 56 ms 20056 KB Output is correct
44 Correct 63 ms 20056 KB Output is correct
45 Correct 48 ms 20144 KB Output is correct
46 Correct 66 ms 20076 KB Output is correct
47 Correct 67 ms 20076 KB Output is correct
48 Correct 42 ms 20264 KB Output is correct
49 Correct 47 ms 19964 KB Output is correct
50 Correct 49 ms 20204 KB Output is correct
51 Correct 54 ms 20100 KB Output is correct
52 Correct 61 ms 20072 KB Output is correct
53 Correct 51 ms 20076 KB Output is correct
54 Correct 77 ms 20148 KB Output is correct
55 Correct 65 ms 20044 KB Output is correct
56 Correct 43 ms 19916 KB Output is correct
57 Correct 51 ms 20264 KB Output is correct
58 Correct 48 ms 20020 KB Output is correct
59 Correct 57 ms 20016 KB Output is correct
60 Correct 56 ms 20080 KB Output is correct
61 Correct 50 ms 20048 KB Output is correct
62 Correct 69 ms 20092 KB Output is correct
63 Correct 63 ms 20172 KB Output is correct
64 Correct 42 ms 19948 KB Output is correct
65 Correct 61 ms 20056 KB Output is correct
66 Correct 68 ms 20044 KB Output is correct
67 Correct 64 ms 20088 KB Output is correct
68 Correct 62 ms 20076 KB Output is correct
69 Correct 61 ms 20048 KB Output is correct
70 Correct 59 ms 20076 KB Output is correct
71 Correct 399 ms 44492 KB Output is correct
72 Correct 394 ms 44504 KB Output is correct
73 Correct 505 ms 43224 KB Output is correct
74 Correct 812 ms 43472 KB Output is correct
75 Correct 386 ms 45480 KB Output is correct
76 Correct 740 ms 43796 KB Output is correct
77 Correct 648 ms 43596 KB Output is correct
78 Correct 260 ms 47408 KB Output is correct
79 Correct 383 ms 41420 KB Output is correct
80 Correct 395 ms 44620 KB Output is correct
81 Correct 484 ms 44404 KB Output is correct
82 Correct 646 ms 43616 KB Output is correct
83 Correct 392 ms 42592 KB Output is correct
84 Correct 724 ms 43484 KB Output is correct
85 Correct 636 ms 43644 KB Output is correct
86 Correct 277 ms 41456 KB Output is correct
87 Correct 354 ms 44540 KB Output is correct
88 Correct 376 ms 41452 KB Output is correct
89 Correct 457 ms 43092 KB Output is correct
90 Correct 527 ms 43368 KB Output is correct
91 Correct 401 ms 42620 KB Output is correct
92 Correct 727 ms 43948 KB Output is correct
93 Correct 654 ms 43760 KB Output is correct
94 Correct 270 ms 41396 KB Output is correct
95 Correct 578 ms 43572 KB Output is correct
96 Correct 559 ms 43572 KB Output is correct
97 Correct 595 ms 43472 KB Output is correct
98 Correct 570 ms 43544 KB Output is correct
99 Correct 589 ms 43532 KB Output is correct
100 Correct 587 ms 43540 KB Output is correct