Submission #702543

# Submission time Handle Problem Language Result Execution time Memory
702543 2023-02-24T10:23:03 Z bebra Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
761 ms 20580 KB
#include <bits/stdc++.h>
using namespace std;

#define dbg(x) cerr << #x << ": " << x << endl;


constexpr int MAX_BIT = 20;
constexpr int MAX_N = 1 << 20;
constexpr int MAGIC = 6;

int dp_sub[MAX_N];
int dp_super[MAX_N];


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int k, q;
    cin >> k >> q;

    string s;
    cin >> s;

    for (int mask = 0; mask < (1 << k); ++mask) {
        dp_sub[mask] = s[mask] - '0';
        dp_super[mask] = s[mask] - '0';
    }
    for (int bit = 0; bit < k; ++bit) {
        for (int mask = 0; mask < (1 << k); ++mask) {
            if (mask & (1 << bit)) {
                dp_sub[mask] += dp_sub[mask ^ (1 << bit)];
            } else {
                dp_super[mask] += dp_super[mask ^ (1 << bit)];
            }
        }
    }

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

        int cnt_null = 0;
        int cnt_one = 0;
        int cnt_mark = 0;
        int mask_null = 0;
        int mask_one = 0;
        int mask_mark = 0;
        for (int i = 0; i < k; ++i) {
            if (t[i] == '0') {
                ++cnt_null;
                mask_null |= 1 << i;
            } else if (t[i] == '1') {
                ++cnt_one;
                mask_one |= 1 << i;
            } else {
                ++cnt_mark;
                mask_mark |= 1 << i;
            }
        }

        int res = 0;
        if (cnt_null <= MAGIC) {
            res = dp_super[mask_one];
            for (int submask = mask_null; submask > 0; submask = (submask - 1) & mask_null) {
                int sign = (__builtin_popcount(submask) % 2 == 0 ? 1 : -1);
                res += dp_super[submask | mask_one] * sign;
            }
        } else if (cnt_one <= MAGIC) {
            res = dp_sub[mask_mark | mask_one];
            for (int submask = mask_one; submask > 0; submask = (submask - 1) & mask_one) {
                int sign = (__builtin_popcount(submask) % 2 == 0 ? 1 : -1);
                res += dp_sub[(submask ^ mask_one) | mask_mark] * sign;
            }
        } else {
            res = s[mask_one] - '0';
            for (int submask = mask_mark; submask > 0; submask = (submask - 1) & mask_mark) {
                res += s[submask | mask_one] - '0';
            }
        }

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

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 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 336 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 336 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 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 336 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 189 ms 12900 KB Output is correct
12 Correct 290 ms 12612 KB Output is correct
13 Correct 321 ms 11740 KB Output is correct
14 Correct 267 ms 11776 KB Output is correct
15 Correct 222 ms 12824 KB Output is correct
16 Correct 243 ms 12056 KB Output is correct
17 Correct 287 ms 11984 KB Output is correct
18 Correct 161 ms 9548 KB Output is correct
19 Correct 319 ms 6532 KB Output is correct
20 Correct 205 ms 8232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 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 336 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 189 ms 12900 KB Output is correct
12 Correct 290 ms 12612 KB Output is correct
13 Correct 321 ms 11740 KB Output is correct
14 Correct 267 ms 11776 KB Output is correct
15 Correct 222 ms 12824 KB Output is correct
16 Correct 243 ms 12056 KB Output is correct
17 Correct 287 ms 11984 KB Output is correct
18 Correct 161 ms 9548 KB Output is correct
19 Correct 319 ms 6532 KB Output is correct
20 Correct 205 ms 8232 KB Output is correct
21 Correct 216 ms 8064 KB Output is correct
22 Correct 281 ms 8220 KB Output is correct
23 Correct 460 ms 7200 KB Output is correct
24 Correct 345 ms 6988 KB Output is correct
25 Correct 245 ms 9100 KB Output is correct
26 Correct 306 ms 7540 KB Output is correct
27 Correct 319 ms 7440 KB Output is correct
28 Correct 166 ms 10136 KB Output is correct
29 Correct 410 ms 6036 KB Output is correct
30 Correct 214 ms 8268 KB Output is correct
31 Correct 299 ms 8020 KB Output is correct
32 Correct 286 ms 8256 KB Output is correct
33 Correct 236 ms 6932 KB Output is correct
34 Correct 331 ms 7172 KB Output is correct
35 Correct 336 ms 7512 KB Output is correct
36 Correct 170 ms 6040 KB Output is correct
37 Correct 200 ms 8212 KB Output is correct
38 Correct 347 ms 6128 KB Output is correct
39 Correct 303 ms 7260 KB Output is correct
40 Correct 285 ms 7288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 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 336 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 40 ms 11944 KB Output is correct
12 Correct 41 ms 11972 KB Output is correct
13 Correct 48 ms 11800 KB Output is correct
14 Correct 65 ms 11880 KB Output is correct
15 Correct 42 ms 11992 KB Output is correct
16 Correct 58 ms 11920 KB Output is correct
17 Correct 59 ms 11876 KB Output is correct
18 Correct 34 ms 12100 KB Output is correct
19 Correct 41 ms 11704 KB Output is correct
20 Correct 42 ms 11976 KB Output is correct
21 Correct 54 ms 11972 KB Output is correct
22 Correct 55 ms 11844 KB Output is correct
23 Correct 42 ms 11776 KB Output is correct
24 Correct 61 ms 11848 KB Output is correct
25 Correct 62 ms 11904 KB Output is correct
26 Correct 39 ms 11732 KB Output is correct
27 Correct 44 ms 11944 KB Output is correct
28 Correct 41 ms 11816 KB Output is correct
29 Correct 46 ms 11900 KB Output is correct
30 Correct 45 ms 11848 KB Output is correct
31 Correct 43 ms 11852 KB Output is correct
32 Correct 62 ms 11844 KB Output is correct
33 Correct 59 ms 11820 KB Output is correct
34 Correct 38 ms 11728 KB Output is correct
35 Correct 52 ms 11884 KB Output is correct
36 Correct 54 ms 11848 KB Output is correct
37 Correct 53 ms 11892 KB Output is correct
38 Correct 53 ms 11892 KB Output is correct
39 Correct 53 ms 11900 KB Output is correct
40 Correct 52 ms 11920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 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 336 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 189 ms 12900 KB Output is correct
12 Correct 290 ms 12612 KB Output is correct
13 Correct 321 ms 11740 KB Output is correct
14 Correct 267 ms 11776 KB Output is correct
15 Correct 222 ms 12824 KB Output is correct
16 Correct 243 ms 12056 KB Output is correct
17 Correct 287 ms 11984 KB Output is correct
18 Correct 161 ms 9548 KB Output is correct
19 Correct 319 ms 6532 KB Output is correct
20 Correct 205 ms 8232 KB Output is correct
21 Correct 216 ms 8064 KB Output is correct
22 Correct 281 ms 8220 KB Output is correct
23 Correct 460 ms 7200 KB Output is correct
24 Correct 345 ms 6988 KB Output is correct
25 Correct 245 ms 9100 KB Output is correct
26 Correct 306 ms 7540 KB Output is correct
27 Correct 319 ms 7440 KB Output is correct
28 Correct 166 ms 10136 KB Output is correct
29 Correct 410 ms 6036 KB Output is correct
30 Correct 214 ms 8268 KB Output is correct
31 Correct 299 ms 8020 KB Output is correct
32 Correct 286 ms 8256 KB Output is correct
33 Correct 236 ms 6932 KB Output is correct
34 Correct 331 ms 7172 KB Output is correct
35 Correct 336 ms 7512 KB Output is correct
36 Correct 170 ms 6040 KB Output is correct
37 Correct 200 ms 8212 KB Output is correct
38 Correct 347 ms 6128 KB Output is correct
39 Correct 303 ms 7260 KB Output is correct
40 Correct 285 ms 7288 KB Output is correct
41 Correct 40 ms 11944 KB Output is correct
42 Correct 41 ms 11972 KB Output is correct
43 Correct 48 ms 11800 KB Output is correct
44 Correct 65 ms 11880 KB Output is correct
45 Correct 42 ms 11992 KB Output is correct
46 Correct 58 ms 11920 KB Output is correct
47 Correct 59 ms 11876 KB Output is correct
48 Correct 34 ms 12100 KB Output is correct
49 Correct 41 ms 11704 KB Output is correct
50 Correct 42 ms 11976 KB Output is correct
51 Correct 54 ms 11972 KB Output is correct
52 Correct 55 ms 11844 KB Output is correct
53 Correct 42 ms 11776 KB Output is correct
54 Correct 61 ms 11848 KB Output is correct
55 Correct 62 ms 11904 KB Output is correct
56 Correct 39 ms 11732 KB Output is correct
57 Correct 44 ms 11944 KB Output is correct
58 Correct 41 ms 11816 KB Output is correct
59 Correct 46 ms 11900 KB Output is correct
60 Correct 45 ms 11848 KB Output is correct
61 Correct 43 ms 11852 KB Output is correct
62 Correct 62 ms 11844 KB Output is correct
63 Correct 59 ms 11820 KB Output is correct
64 Correct 38 ms 11728 KB Output is correct
65 Correct 52 ms 11884 KB Output is correct
66 Correct 54 ms 11848 KB Output is correct
67 Correct 53 ms 11892 KB Output is correct
68 Correct 53 ms 11892 KB Output is correct
69 Correct 53 ms 11900 KB Output is correct
70 Correct 52 ms 11920 KB Output is correct
71 Correct 346 ms 17460 KB Output is correct
72 Correct 366 ms 17584 KB Output is correct
73 Correct 491 ms 16248 KB Output is correct
74 Correct 684 ms 16504 KB Output is correct
75 Correct 375 ms 18408 KB Output is correct
76 Correct 655 ms 16764 KB Output is correct
77 Correct 743 ms 16756 KB Output is correct
78 Correct 251 ms 20580 KB Output is correct
79 Correct 312 ms 14592 KB Output is correct
80 Correct 322 ms 17604 KB Output is correct
81 Correct 482 ms 17460 KB Output is correct
82 Correct 719 ms 16580 KB Output is correct
83 Correct 364 ms 15576 KB Output is correct
84 Correct 748 ms 16496 KB Output is correct
85 Correct 710 ms 16792 KB Output is correct
86 Correct 232 ms 14420 KB Output is correct
87 Correct 311 ms 17496 KB Output is correct
88 Correct 342 ms 14444 KB Output is correct
89 Correct 496 ms 16068 KB Output is correct
90 Correct 429 ms 16448 KB Output is correct
91 Correct 390 ms 15708 KB Output is correct
92 Correct 761 ms 16912 KB Output is correct
93 Correct 687 ms 16804 KB Output is correct
94 Correct 215 ms 14460 KB Output is correct
95 Correct 599 ms 16652 KB Output is correct
96 Correct 629 ms 16640 KB Output is correct
97 Correct 582 ms 16560 KB Output is correct
98 Correct 619 ms 16492 KB Output is correct
99 Correct 593 ms 16484 KB Output is correct
100 Correct 603 ms 16576 KB Output is correct