답안 #522359

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
522359 2022-02-04T16:34:22 Z Monarchuwu Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
722 ms 23008 KB
#include<iostream>
#include<algorithm>
#include<string>
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;

const int N = 1 << 20;
int l, q;
string s;

bool lg[N];
int a[N], f[N], g[N]; // f chứa con (1 = 01), g chứa cha (0 = 01)
void prep() {
    for (int msk = 0; msk < (1 << l); ++msk) {
        lg[msk] = lg[msk >> 1] ^ (msk & 1);
        a[msk] = f[msk] = g[msk] = s[msk] ^ 48;
    }

    for (int j = 0; j < l; ++j)
        for (int msk = 0; msk < (1 << l); ++msk)
            if (msk >> j & 1)
                f[msk] += f[msk ^ (1 << j)];
            else g[msk] += g[msk | (1 << j)];
}

int main() {
    cin.tie(NULL)->sync_with_stdio(false);
    cin >> l >> q >> s;
    prep();

    while (q--) {
        string t; cin >> t;
        int m0(0), m1(0), m2(0), cnt0(0), cnt2(0);
        for (char c : t) {
            m0 <<= 1, m1 <<= 1, m2 <<= 1;
            if (c == '0') ++m0, ++cnt0;
            else if (c == '1') ++m1;
            else ++m2, ++cnt2;
        }

        int ans(0);
        if (cnt2 <= l / 3) { // brute ?
            ans = a[m1];
            for (int m = m2; m; m = (m - 1) & m2)
                ans += a[m | m1];
        }
        else if (cnt0 <= l / 3) { // inc-exc 0, dp sos 1 and ? => dùng g
            ans = g[m1];
            for (int m = m0; m; m = (m - 1) & m0)
                ans += lg[m] ? -g[m | m1] : g[m | m1];
        }
        else { // inc-exc 1, dp sos 0 and ? => dùng f
            ans = lg[m1] ? -f[m2] : f[m2];
            for (int m = m1; m; m = (m - 1) & m1)
                ans += lg[m] == lg[m1] ? f[m | m2] : -f[m | m2];
        }
        cout << ans << '\n';
    }
}
/**  /\_/\
 *  (= ._.)
 *  / >0  \>1
**/
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 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 332 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 332 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 332 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 181 ms 4288 KB Output is correct
12 Correct 212 ms 4024 KB Output is correct
13 Correct 208 ms 3268 KB Output is correct
14 Correct 219 ms 3276 KB Output is correct
15 Correct 189 ms 4260 KB Output is correct
16 Correct 224 ms 3416 KB Output is correct
17 Correct 221 ms 3416 KB Output is correct
18 Correct 158 ms 5332 KB Output is correct
19 Correct 188 ms 2356 KB Output is correct
20 Correct 215 ms 4112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 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 332 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 181 ms 4288 KB Output is correct
12 Correct 212 ms 4024 KB Output is correct
13 Correct 208 ms 3268 KB Output is correct
14 Correct 219 ms 3276 KB Output is correct
15 Correct 189 ms 4260 KB Output is correct
16 Correct 224 ms 3416 KB Output is correct
17 Correct 221 ms 3416 KB Output is correct
18 Correct 158 ms 5332 KB Output is correct
19 Correct 188 ms 2356 KB Output is correct
20 Correct 215 ms 4112 KB Output is correct
21 Correct 213 ms 4396 KB Output is correct
22 Correct 262 ms 4528 KB Output is correct
23 Correct 252 ms 3580 KB Output is correct
24 Correct 278 ms 3396 KB Output is correct
25 Correct 217 ms 5384 KB Output is correct
26 Correct 251 ms 3908 KB Output is correct
27 Correct 287 ms 3808 KB Output is correct
28 Correct 152 ms 6460 KB Output is correct
29 Correct 209 ms 2340 KB Output is correct
30 Correct 214 ms 4620 KB Output is correct
31 Correct 244 ms 4420 KB Output is correct
32 Correct 238 ms 4400 KB Output is correct
33 Correct 235 ms 3256 KB Output is correct
34 Correct 254 ms 3476 KB Output is correct
35 Correct 270 ms 3920 KB Output is correct
36 Correct 163 ms 2424 KB Output is correct
37 Correct 205 ms 4408 KB Output is correct
38 Correct 211 ms 2432 KB Output is correct
39 Correct 245 ms 3592 KB Output is correct
40 Correct 243 ms 3460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 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 332 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 45 ms 14948 KB Output is correct
12 Correct 56 ms 15064 KB Output is correct
13 Correct 47 ms 14944 KB Output is correct
14 Correct 50 ms 14932 KB Output is correct
15 Correct 44 ms 15064 KB Output is correct
16 Correct 56 ms 14996 KB Output is correct
17 Correct 81 ms 14952 KB Output is correct
18 Correct 40 ms 15176 KB Output is correct
19 Correct 44 ms 14840 KB Output is correct
20 Correct 43 ms 14936 KB Output is correct
21 Correct 48 ms 14940 KB Output is correct
22 Correct 57 ms 14920 KB Output is correct
23 Correct 49 ms 14948 KB Output is correct
24 Correct 50 ms 14956 KB Output is correct
25 Correct 57 ms 14928 KB Output is correct
26 Correct 38 ms 14876 KB Output is correct
27 Correct 43 ms 14936 KB Output is correct
28 Correct 44 ms 14816 KB Output is correct
29 Correct 46 ms 14940 KB Output is correct
30 Correct 58 ms 14948 KB Output is correct
31 Correct 45 ms 14936 KB Output is correct
32 Correct 61 ms 14912 KB Output is correct
33 Correct 59 ms 14952 KB Output is correct
34 Correct 37 ms 14808 KB Output is correct
35 Correct 51 ms 15044 KB Output is correct
36 Correct 54 ms 14944 KB Output is correct
37 Correct 58 ms 14900 KB Output is correct
38 Correct 60 ms 14952 KB Output is correct
39 Correct 59 ms 14980 KB Output is correct
40 Correct 51 ms 14932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 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 332 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 181 ms 4288 KB Output is correct
12 Correct 212 ms 4024 KB Output is correct
13 Correct 208 ms 3268 KB Output is correct
14 Correct 219 ms 3276 KB Output is correct
15 Correct 189 ms 4260 KB Output is correct
16 Correct 224 ms 3416 KB Output is correct
17 Correct 221 ms 3416 KB Output is correct
18 Correct 158 ms 5332 KB Output is correct
19 Correct 188 ms 2356 KB Output is correct
20 Correct 215 ms 4112 KB Output is correct
21 Correct 213 ms 4396 KB Output is correct
22 Correct 262 ms 4528 KB Output is correct
23 Correct 252 ms 3580 KB Output is correct
24 Correct 278 ms 3396 KB Output is correct
25 Correct 217 ms 5384 KB Output is correct
26 Correct 251 ms 3908 KB Output is correct
27 Correct 287 ms 3808 KB Output is correct
28 Correct 152 ms 6460 KB Output is correct
29 Correct 209 ms 2340 KB Output is correct
30 Correct 214 ms 4620 KB Output is correct
31 Correct 244 ms 4420 KB Output is correct
32 Correct 238 ms 4400 KB Output is correct
33 Correct 235 ms 3256 KB Output is correct
34 Correct 254 ms 3476 KB Output is correct
35 Correct 270 ms 3920 KB Output is correct
36 Correct 163 ms 2424 KB Output is correct
37 Correct 205 ms 4408 KB Output is correct
38 Correct 211 ms 2432 KB Output is correct
39 Correct 245 ms 3592 KB Output is correct
40 Correct 243 ms 3460 KB Output is correct
41 Correct 45 ms 14948 KB Output is correct
42 Correct 56 ms 15064 KB Output is correct
43 Correct 47 ms 14944 KB Output is correct
44 Correct 50 ms 14932 KB Output is correct
45 Correct 44 ms 15064 KB Output is correct
46 Correct 56 ms 14996 KB Output is correct
47 Correct 81 ms 14952 KB Output is correct
48 Correct 40 ms 15176 KB Output is correct
49 Correct 44 ms 14840 KB Output is correct
50 Correct 43 ms 14936 KB Output is correct
51 Correct 48 ms 14940 KB Output is correct
52 Correct 57 ms 14920 KB Output is correct
53 Correct 49 ms 14948 KB Output is correct
54 Correct 50 ms 14956 KB Output is correct
55 Correct 57 ms 14928 KB Output is correct
56 Correct 38 ms 14876 KB Output is correct
57 Correct 43 ms 14936 KB Output is correct
58 Correct 44 ms 14816 KB Output is correct
59 Correct 46 ms 14940 KB Output is correct
60 Correct 58 ms 14948 KB Output is correct
61 Correct 45 ms 14936 KB Output is correct
62 Correct 61 ms 14912 KB Output is correct
63 Correct 59 ms 14952 KB Output is correct
64 Correct 37 ms 14808 KB Output is correct
65 Correct 51 ms 15044 KB Output is correct
66 Correct 54 ms 14944 KB Output is correct
67 Correct 58 ms 14900 KB Output is correct
68 Correct 60 ms 14952 KB Output is correct
69 Correct 59 ms 14980 KB Output is correct
70 Correct 51 ms 14932 KB Output is correct
71 Correct 355 ms 19820 KB Output is correct
72 Correct 358 ms 19892 KB Output is correct
73 Correct 419 ms 18392 KB Output is correct
74 Correct 504 ms 18788 KB Output is correct
75 Correct 420 ms 20720 KB Output is correct
76 Correct 722 ms 19116 KB Output is correct
77 Correct 638 ms 18984 KB Output is correct
78 Correct 224 ms 23008 KB Output is correct
79 Correct 340 ms 16736 KB Output is correct
80 Correct 374 ms 19944 KB Output is correct
81 Correct 448 ms 19716 KB Output is correct
82 Correct 510 ms 18812 KB Output is correct
83 Correct 313 ms 17892 KB Output is correct
84 Correct 499 ms 18860 KB Output is correct
85 Correct 584 ms 18940 KB Output is correct
86 Correct 233 ms 16732 KB Output is correct
87 Correct 339 ms 19704 KB Output is correct
88 Correct 319 ms 16808 KB Output is correct
89 Correct 456 ms 18392 KB Output is correct
90 Correct 512 ms 18728 KB Output is correct
91 Correct 413 ms 17996 KB Output is correct
92 Correct 673 ms 19032 KB Output is correct
93 Correct 602 ms 18932 KB Output is correct
94 Correct 208 ms 16856 KB Output is correct
95 Correct 543 ms 18924 KB Output is correct
96 Correct 558 ms 18864 KB Output is correct
97 Correct 517 ms 18856 KB Output is correct
98 Correct 496 ms 18848 KB Output is correct
99 Correct 489 ms 18804 KB Output is correct
100 Correct 506 ms 18836 KB Output is correct