답안 #391765

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
391765 2021-04-19T17:57:24 Z timmyfeng Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1012 ms 43272 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 20;

int zero[1 << N], one[1 << N], value[1 << N];

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

    int n, q;
    string s;
    cin >> n >> q >> s;

    for (int i = 0; i < (1 << n); ++i) {
        zero[i] = one[i] = value[i] = s[i] - '0';
    }

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < (1 << n); ++j) {
            if ((j & (1 << i)) > 0) {
                one[j ^ (1 << i)] += one[j];
            } else {
                zero[j ^ (1 << i)] += zero[j];
            }
        }
    }

    while (q--) {
        cin >> s;

        reverse(s.begin(), s.end());

        int mask_0 = 0, mask_1 = 0, mask_q = 0;
        for (int i = 0; i < n; ++i) {
            if (s[i] == '0') {
                mask_0 |= 1 << i;
            } else if (s[i] == '1') {
                mask_1 |= 1 << i;
            } else {
                mask_q |= 1 << i;
            }
        }

        int ans = 0;
        if (__builtin_popcount(mask_0) <= n / 3) {
            ans = one[mask_1];
            for (int i = mask_0; i > 0; i = mask_0 & (i - 1)) {
                if (__builtin_parity(i) == 1) {
                    ans -= one[mask_1 ^ i];
                } else {
                    ans += one[mask_1 ^ i];
                }
            }
        } else if (__builtin_popcount(mask_1) <= n / 3) {
            ans = zero[mask_1 ^ mask_q];
            for (int i = mask_1; i > 0; i = mask_1 & (i - 1)) {
                if (__builtin_parity(i) == 1) {
                    ans -= zero[mask_q ^ mask_1 ^ i];
                } else {
                    ans += zero[mask_q ^ mask_1 ^ i];
                }
            }
        } else {
            ans = value[mask_1];
            for (int i = mask_q; i > 0; i = mask_q & (i - 1)) {
                ans += value[mask_1 ^ i];
            }
        }

        cout << ans << "\n";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 254 ms 14972 KB Output is correct
12 Correct 273 ms 12228 KB Output is correct
13 Correct 299 ms 11568 KB Output is correct
14 Correct 284 ms 11520 KB Output is correct
15 Correct 279 ms 12596 KB Output is correct
16 Correct 300 ms 11844 KB Output is correct
17 Correct 303 ms 11716 KB Output is correct
18 Correct 218 ms 13588 KB Output is correct
19 Correct 256 ms 10612 KB Output is correct
20 Correct 269 ms 12456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 254 ms 14972 KB Output is correct
12 Correct 273 ms 12228 KB Output is correct
13 Correct 299 ms 11568 KB Output is correct
14 Correct 284 ms 11520 KB Output is correct
15 Correct 279 ms 12596 KB Output is correct
16 Correct 300 ms 11844 KB Output is correct
17 Correct 303 ms 11716 KB Output is correct
18 Correct 218 ms 13588 KB Output is correct
19 Correct 256 ms 10612 KB Output is correct
20 Correct 269 ms 12456 KB Output is correct
21 Correct 282 ms 15912 KB Output is correct
22 Correct 300 ms 16068 KB Output is correct
23 Correct 350 ms 12820 KB Output is correct
24 Correct 348 ms 12868 KB Output is correct
25 Correct 301 ms 14916 KB Output is correct
26 Correct 353 ms 13420 KB Output is correct
27 Correct 355 ms 13552 KB Output is correct
28 Correct 221 ms 16296 KB Output is correct
29 Correct 278 ms 12276 KB Output is correct
30 Correct 300 ms 14672 KB Output is correct
31 Correct 329 ms 14560 KB Output is correct
32 Correct 345 ms 14652 KB Output is correct
33 Correct 306 ms 13584 KB Output is correct
34 Correct 401 ms 13704 KB Output is correct
35 Correct 356 ms 14312 KB Output is correct
36 Correct 241 ms 13252 KB Output is correct
37 Correct 271 ms 15112 KB Output is correct
38 Correct 305 ms 13200 KB Output is correct
39 Correct 327 ms 14532 KB Output is correct
40 Correct 338 ms 14520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 53 ms 15980 KB Output is correct
12 Correct 57 ms 16076 KB Output is correct
13 Correct 72 ms 15916 KB Output is correct
14 Correct 65 ms 15972 KB Output is correct
15 Correct 52 ms 16076 KB Output is correct
16 Correct 64 ms 15940 KB Output is correct
17 Correct 74 ms 15968 KB Output is correct
18 Correct 46 ms 16188 KB Output is correct
19 Correct 51 ms 15812 KB Output is correct
20 Correct 52 ms 15972 KB Output is correct
21 Correct 59 ms 15956 KB Output is correct
22 Correct 68 ms 16072 KB Output is correct
23 Correct 53 ms 15968 KB Output is correct
24 Correct 78 ms 16016 KB Output is correct
25 Correct 74 ms 15948 KB Output is correct
26 Correct 44 ms 15816 KB Output is correct
27 Correct 55 ms 16040 KB Output is correct
28 Correct 59 ms 15812 KB Output is correct
29 Correct 59 ms 15996 KB Output is correct
30 Correct 59 ms 15972 KB Output is correct
31 Correct 53 ms 15876 KB Output is correct
32 Correct 76 ms 16020 KB Output is correct
33 Correct 78 ms 15976 KB Output is correct
34 Correct 44 ms 15812 KB Output is correct
35 Correct 70 ms 15948 KB Output is correct
36 Correct 67 ms 15912 KB Output is correct
37 Correct 71 ms 15944 KB Output is correct
38 Correct 67 ms 15976 KB Output is correct
39 Correct 68 ms 15932 KB Output is correct
40 Correct 70 ms 15940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 254 ms 14972 KB Output is correct
12 Correct 273 ms 12228 KB Output is correct
13 Correct 299 ms 11568 KB Output is correct
14 Correct 284 ms 11520 KB Output is correct
15 Correct 279 ms 12596 KB Output is correct
16 Correct 300 ms 11844 KB Output is correct
17 Correct 303 ms 11716 KB Output is correct
18 Correct 218 ms 13588 KB Output is correct
19 Correct 256 ms 10612 KB Output is correct
20 Correct 269 ms 12456 KB Output is correct
21 Correct 282 ms 15912 KB Output is correct
22 Correct 300 ms 16068 KB Output is correct
23 Correct 350 ms 12820 KB Output is correct
24 Correct 348 ms 12868 KB Output is correct
25 Correct 301 ms 14916 KB Output is correct
26 Correct 353 ms 13420 KB Output is correct
27 Correct 355 ms 13552 KB Output is correct
28 Correct 221 ms 16296 KB Output is correct
29 Correct 278 ms 12276 KB Output is correct
30 Correct 300 ms 14672 KB Output is correct
31 Correct 329 ms 14560 KB Output is correct
32 Correct 345 ms 14652 KB Output is correct
33 Correct 306 ms 13584 KB Output is correct
34 Correct 401 ms 13704 KB Output is correct
35 Correct 356 ms 14312 KB Output is correct
36 Correct 241 ms 13252 KB Output is correct
37 Correct 271 ms 15112 KB Output is correct
38 Correct 305 ms 13200 KB Output is correct
39 Correct 327 ms 14532 KB Output is correct
40 Correct 338 ms 14520 KB Output is correct
41 Correct 53 ms 15980 KB Output is correct
42 Correct 57 ms 16076 KB Output is correct
43 Correct 72 ms 15916 KB Output is correct
44 Correct 65 ms 15972 KB Output is correct
45 Correct 52 ms 16076 KB Output is correct
46 Correct 64 ms 15940 KB Output is correct
47 Correct 74 ms 15968 KB Output is correct
48 Correct 46 ms 16188 KB Output is correct
49 Correct 51 ms 15812 KB Output is correct
50 Correct 52 ms 15972 KB Output is correct
51 Correct 59 ms 15956 KB Output is correct
52 Correct 68 ms 16072 KB Output is correct
53 Correct 53 ms 15968 KB Output is correct
54 Correct 78 ms 16016 KB Output is correct
55 Correct 74 ms 15948 KB Output is correct
56 Correct 44 ms 15816 KB Output is correct
57 Correct 55 ms 16040 KB Output is correct
58 Correct 59 ms 15812 KB Output is correct
59 Correct 59 ms 15996 KB Output is correct
60 Correct 59 ms 15972 KB Output is correct
61 Correct 53 ms 15876 KB Output is correct
62 Correct 76 ms 16020 KB Output is correct
63 Correct 78 ms 15976 KB Output is correct
64 Correct 44 ms 15812 KB Output is correct
65 Correct 70 ms 15948 KB Output is correct
66 Correct 67 ms 15912 KB Output is correct
67 Correct 71 ms 15944 KB Output is correct
68 Correct 67 ms 15976 KB Output is correct
69 Correct 68 ms 15932 KB Output is correct
70 Correct 70 ms 15940 KB Output is correct
71 Correct 444 ms 40344 KB Output is correct
72 Correct 467 ms 40380 KB Output is correct
73 Correct 648 ms 38880 KB Output is correct
74 Correct 722 ms 39240 KB Output is correct
75 Correct 428 ms 41216 KB Output is correct
76 Correct 753 ms 39684 KB Output is correct
77 Correct 963 ms 39476 KB Output is correct
78 Correct 286 ms 43272 KB Output is correct
79 Correct 407 ms 37344 KB Output is correct
80 Correct 475 ms 40408 KB Output is correct
81 Correct 591 ms 40208 KB Output is correct
82 Correct 696 ms 39424 KB Output is correct
83 Correct 453 ms 38492 KB Output is correct
84 Correct 1012 ms 39428 KB Output is correct
85 Correct 923 ms 39516 KB Output is correct
86 Correct 300 ms 37292 KB Output is correct
87 Correct 423 ms 40316 KB Output is correct
88 Correct 510 ms 37428 KB Output is correct
89 Correct 586 ms 39020 KB Output is correct
90 Correct 616 ms 39284 KB Output is correct
91 Correct 489 ms 38572 KB Output is correct
92 Correct 996 ms 39532 KB Output is correct
93 Correct 936 ms 39448 KB Output is correct
94 Correct 275 ms 37308 KB Output is correct
95 Correct 781 ms 39340 KB Output is correct
96 Correct 795 ms 39376 KB Output is correct
97 Correct 761 ms 39324 KB Output is correct
98 Correct 751 ms 39380 KB Output is correct
99 Correct 794 ms 39420 KB Output is correct
100 Correct 768 ms 39460 KB Output is correct