답안 #522354

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
522354 2022-02-04T16:24:03 Z Monarchuwu Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
711 ms 25904 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;

int pw[20 + 1], lg[N];
int a[N], f[N], g[N]; // f chứa con (1 = 01), g chứa cha (0 = 01)
void prep() {
    pw[0] = 1;
    for (int i = 1; i <= l; ++i) pw[i] = pw[i - 1] << 1;

    for (int msk = 0; msk < pw[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 < pw[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);
        for (char c : t) {
            m0 <<= 1, m1 <<= 1, m2 <<= 1;
            if (c == '0') ++m0;
            else if (c == '1') ++m1;
            else ++m2;
        }

        int ans(0);
        if (lg[m2] <= l / 3) { // brute ?
            ans = a[m1];
            for (int m = m2; m; m = (m - 1) & m2)
                ans += a[m | m1];
        }
        else if (lg[m0] <= 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] & 1 ? -g[m | m1] : g[m | m1];
        }
        else { // inc-exc 1, dp sos 0 and ? => dùng f
            ans = lg[m1] & 1 ? -f[m2] : f[m2];
            for (int m = m1, k = lg[m1] & 1; m; m = (m - 1) & m1)
                ans += (lg[m] & 1) == k ? 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 0 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 0 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 180 ms 4296 KB Output is correct
12 Correct 193 ms 3912 KB Output is correct
13 Correct 215 ms 3228 KB Output is correct
14 Correct 209 ms 3340 KB Output is correct
15 Correct 185 ms 4268 KB Output is correct
16 Correct 270 ms 3440 KB Output is correct
17 Correct 240 ms 3496 KB Output is correct
18 Correct 160 ms 5316 KB Output is correct
19 Correct 171 ms 2272 KB Output is correct
20 Correct 239 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 0 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 180 ms 4296 KB Output is correct
12 Correct 193 ms 3912 KB Output is correct
13 Correct 215 ms 3228 KB Output is correct
14 Correct 209 ms 3340 KB Output is correct
15 Correct 185 ms 4268 KB Output is correct
16 Correct 270 ms 3440 KB Output is correct
17 Correct 240 ms 3496 KB Output is correct
18 Correct 160 ms 5316 KB Output is correct
19 Correct 171 ms 2272 KB Output is correct
20 Correct 239 ms 4112 KB Output is correct
21 Correct 199 ms 4372 KB Output is correct
22 Correct 245 ms 4676 KB Output is correct
23 Correct 256 ms 3652 KB Output is correct
24 Correct 246 ms 3392 KB Output is correct
25 Correct 216 ms 5424 KB Output is correct
26 Correct 258 ms 3896 KB Output is correct
27 Correct 255 ms 3908 KB Output is correct
28 Correct 151 ms 6384 KB Output is correct
29 Correct 195 ms 2380 KB Output is correct
30 Correct 215 ms 4608 KB Output is correct
31 Correct 249 ms 4420 KB Output is correct
32 Correct 244 ms 4412 KB Output is correct
33 Correct 225 ms 3428 KB Output is correct
34 Correct 248 ms 3396 KB Output is correct
35 Correct 265 ms 3828 KB Output is correct
36 Correct 172 ms 2380 KB Output is correct
37 Correct 210 ms 4636 KB Output is correct
38 Correct 202 ms 2404 KB Output is correct
39 Correct 230 ms 3552 KB Output is correct
40 Correct 239 ms 3484 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 0 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 48 ms 18044 KB Output is correct
12 Correct 50 ms 18076 KB Output is correct
13 Correct 53 ms 18032 KB Output is correct
14 Correct 70 ms 18020 KB Output is correct
15 Correct 61 ms 18136 KB Output is correct
16 Correct 69 ms 18012 KB Output is correct
17 Correct 59 ms 18016 KB Output is correct
18 Correct 43 ms 18276 KB Output is correct
19 Correct 45 ms 17872 KB Output is correct
20 Correct 55 ms 18060 KB Output is correct
21 Correct 72 ms 18016 KB Output is correct
22 Correct 79 ms 18020 KB Output is correct
23 Correct 55 ms 17924 KB Output is correct
24 Correct 63 ms 18144 KB Output is correct
25 Correct 68 ms 18020 KB Output is correct
26 Correct 43 ms 17888 KB Output is correct
27 Correct 51 ms 18008 KB Output is correct
28 Correct 49 ms 17896 KB Output is correct
29 Correct 49 ms 18020 KB Output is correct
30 Correct 52 ms 18008 KB Output is correct
31 Correct 46 ms 18004 KB Output is correct
32 Correct 63 ms 18004 KB Output is correct
33 Correct 65 ms 18264 KB Output is correct
34 Correct 40 ms 17892 KB Output is correct
35 Correct 57 ms 18020 KB Output is correct
36 Correct 57 ms 18016 KB Output is correct
37 Correct 55 ms 18012 KB Output is correct
38 Correct 61 ms 18164 KB Output is correct
39 Correct 56 ms 18000 KB Output is correct
40 Correct 54 ms 18020 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 0 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 180 ms 4296 KB Output is correct
12 Correct 193 ms 3912 KB Output is correct
13 Correct 215 ms 3228 KB Output is correct
14 Correct 209 ms 3340 KB Output is correct
15 Correct 185 ms 4268 KB Output is correct
16 Correct 270 ms 3440 KB Output is correct
17 Correct 240 ms 3496 KB Output is correct
18 Correct 160 ms 5316 KB Output is correct
19 Correct 171 ms 2272 KB Output is correct
20 Correct 239 ms 4112 KB Output is correct
21 Correct 199 ms 4372 KB Output is correct
22 Correct 245 ms 4676 KB Output is correct
23 Correct 256 ms 3652 KB Output is correct
24 Correct 246 ms 3392 KB Output is correct
25 Correct 216 ms 5424 KB Output is correct
26 Correct 258 ms 3896 KB Output is correct
27 Correct 255 ms 3908 KB Output is correct
28 Correct 151 ms 6384 KB Output is correct
29 Correct 195 ms 2380 KB Output is correct
30 Correct 215 ms 4608 KB Output is correct
31 Correct 249 ms 4420 KB Output is correct
32 Correct 244 ms 4412 KB Output is correct
33 Correct 225 ms 3428 KB Output is correct
34 Correct 248 ms 3396 KB Output is correct
35 Correct 265 ms 3828 KB Output is correct
36 Correct 172 ms 2380 KB Output is correct
37 Correct 210 ms 4636 KB Output is correct
38 Correct 202 ms 2404 KB Output is correct
39 Correct 230 ms 3552 KB Output is correct
40 Correct 239 ms 3484 KB Output is correct
41 Correct 48 ms 18044 KB Output is correct
42 Correct 50 ms 18076 KB Output is correct
43 Correct 53 ms 18032 KB Output is correct
44 Correct 70 ms 18020 KB Output is correct
45 Correct 61 ms 18136 KB Output is correct
46 Correct 69 ms 18012 KB Output is correct
47 Correct 59 ms 18016 KB Output is correct
48 Correct 43 ms 18276 KB Output is correct
49 Correct 45 ms 17872 KB Output is correct
50 Correct 55 ms 18060 KB Output is correct
51 Correct 72 ms 18016 KB Output is correct
52 Correct 79 ms 18020 KB Output is correct
53 Correct 55 ms 17924 KB Output is correct
54 Correct 63 ms 18144 KB Output is correct
55 Correct 68 ms 18020 KB Output is correct
56 Correct 43 ms 17888 KB Output is correct
57 Correct 51 ms 18008 KB Output is correct
58 Correct 49 ms 17896 KB Output is correct
59 Correct 49 ms 18020 KB Output is correct
60 Correct 52 ms 18008 KB Output is correct
61 Correct 46 ms 18004 KB Output is correct
62 Correct 63 ms 18004 KB Output is correct
63 Correct 65 ms 18264 KB Output is correct
64 Correct 40 ms 17892 KB Output is correct
65 Correct 57 ms 18020 KB Output is correct
66 Correct 57 ms 18016 KB Output is correct
67 Correct 55 ms 18012 KB Output is correct
68 Correct 61 ms 18164 KB Output is correct
69 Correct 56 ms 18000 KB Output is correct
70 Correct 54 ms 18020 KB Output is correct
71 Correct 389 ms 22864 KB Output is correct
72 Correct 417 ms 22964 KB Output is correct
73 Correct 474 ms 21600 KB Output is correct
74 Correct 580 ms 21992 KB Output is correct
75 Correct 341 ms 23796 KB Output is correct
76 Correct 639 ms 22108 KB Output is correct
77 Correct 642 ms 22048 KB Output is correct
78 Correct 230 ms 25904 KB Output is correct
79 Correct 308 ms 19940 KB Output is correct
80 Correct 380 ms 23016 KB Output is correct
81 Correct 512 ms 22884 KB Output is correct
82 Correct 569 ms 22040 KB Output is correct
83 Correct 328 ms 20964 KB Output is correct
84 Correct 494 ms 21972 KB Output is correct
85 Correct 622 ms 22068 KB Output is correct
86 Correct 225 ms 19940 KB Output is correct
87 Correct 324 ms 22844 KB Output is correct
88 Correct 333 ms 19828 KB Output is correct
89 Correct 429 ms 21488 KB Output is correct
90 Correct 506 ms 21840 KB Output is correct
91 Correct 360 ms 21072 KB Output is correct
92 Correct 711 ms 22332 KB Output is correct
93 Correct 628 ms 22068 KB Output is correct
94 Correct 198 ms 19804 KB Output is correct
95 Correct 543 ms 21968 KB Output is correct
96 Correct 551 ms 22088 KB Output is correct
97 Correct 548 ms 22004 KB Output is correct
98 Correct 543 ms 21960 KB Output is correct
99 Correct 572 ms 21904 KB Output is correct
100 Correct 551 ms 21860 KB Output is correct