답안 #522360

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
522360 2022-02-04T16:37:09 Z Monarchuwu Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
671 ms 22756 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() {
    int MASK(1 << l);
    for (int msk = 0; msk < MASK; ++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 < MASK; ++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), cnt1(0), cnt2(0);
        for (char c : t) {
            m0 <<= 1, m1 <<= 1, m2 <<= 1;
            if (c == '0') ++m0, ++cnt0;
            else if (c == '1') ++m1, ++cnt1;
            else ++m2, ++cnt2;
        }

        int ans(0), mi = min({ cnt0, cnt1, cnt2 });
        if (cnt2 == mi) { // brute ?
            ans = a[m1];
            for (int m = m2; m; m = (m - 1) & m2)
                ans += a[m | m1];
        }
        else if (cnt0 == mi) { // 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 188 ms 4288 KB Output is correct
12 Correct 205 ms 3956 KB Output is correct
13 Correct 217 ms 3152 KB Output is correct
14 Correct 207 ms 3268 KB Output is correct
15 Correct 184 ms 4292 KB Output is correct
16 Correct 220 ms 3524 KB Output is correct
17 Correct 214 ms 3432 KB Output is correct
18 Correct 148 ms 5312 KB Output is correct
19 Correct 176 ms 2244 KB Output is correct
20 Correct 209 ms 3944 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 188 ms 4288 KB Output is correct
12 Correct 205 ms 3956 KB Output is correct
13 Correct 217 ms 3152 KB Output is correct
14 Correct 207 ms 3268 KB Output is correct
15 Correct 184 ms 4292 KB Output is correct
16 Correct 220 ms 3524 KB Output is correct
17 Correct 214 ms 3432 KB Output is correct
18 Correct 148 ms 5312 KB Output is correct
19 Correct 176 ms 2244 KB Output is correct
20 Correct 209 ms 3944 KB Output is correct
21 Correct 209 ms 4400 KB Output is correct
22 Correct 221 ms 4656 KB Output is correct
23 Correct 241 ms 3528 KB Output is correct
24 Correct 247 ms 3408 KB Output is correct
25 Correct 211 ms 5444 KB Output is correct
26 Correct 250 ms 3932 KB Output is correct
27 Correct 269 ms 3824 KB Output is correct
28 Correct 155 ms 6404 KB Output is correct
29 Correct 199 ms 2404 KB Output is correct
30 Correct 212 ms 4600 KB Output is correct
31 Correct 271 ms 4368 KB Output is correct
32 Correct 251 ms 4472 KB Output is correct
33 Correct 203 ms 3292 KB Output is correct
34 Correct 295 ms 3536 KB Output is correct
35 Correct 254 ms 3832 KB Output is correct
36 Correct 194 ms 2492 KB Output is correct
37 Correct 195 ms 4444 KB Output is correct
38 Correct 238 ms 2408 KB Output is correct
39 Correct 225 ms 3632 KB Output is correct
40 Correct 263 ms 3388 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 42 ms 14936 KB Output is correct
12 Correct 48 ms 14952 KB Output is correct
13 Correct 54 ms 14968 KB Output is correct
14 Correct 54 ms 14880 KB Output is correct
15 Correct 45 ms 15060 KB Output is correct
16 Correct 55 ms 14916 KB Output is correct
17 Correct 58 ms 14972 KB Output is correct
18 Correct 37 ms 15192 KB Output is correct
19 Correct 46 ms 14820 KB Output is correct
20 Correct 62 ms 14952 KB Output is correct
21 Correct 51 ms 14964 KB Output is correct
22 Correct 74 ms 14936 KB Output is correct
23 Correct 45 ms 14848 KB Output is correct
24 Correct 56 ms 14924 KB Output is correct
25 Correct 56 ms 14892 KB Output is correct
26 Correct 38 ms 14820 KB Output is correct
27 Correct 44 ms 14996 KB Output is correct
28 Correct 50 ms 14820 KB Output is correct
29 Correct 63 ms 14944 KB Output is correct
30 Correct 66 ms 14976 KB Output is correct
31 Correct 45 ms 14880 KB Output is correct
32 Correct 69 ms 14932 KB Output is correct
33 Correct 58 ms 14936 KB Output is correct
34 Correct 43 ms 14820 KB Output is correct
35 Correct 54 ms 14992 KB Output is correct
36 Correct 59 ms 14948 KB Output is correct
37 Correct 52 ms 14948 KB Output is correct
38 Correct 52 ms 14940 KB Output is correct
39 Correct 50 ms 14948 KB Output is correct
40 Correct 61 ms 15020 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 188 ms 4288 KB Output is correct
12 Correct 205 ms 3956 KB Output is correct
13 Correct 217 ms 3152 KB Output is correct
14 Correct 207 ms 3268 KB Output is correct
15 Correct 184 ms 4292 KB Output is correct
16 Correct 220 ms 3524 KB Output is correct
17 Correct 214 ms 3432 KB Output is correct
18 Correct 148 ms 5312 KB Output is correct
19 Correct 176 ms 2244 KB Output is correct
20 Correct 209 ms 3944 KB Output is correct
21 Correct 209 ms 4400 KB Output is correct
22 Correct 221 ms 4656 KB Output is correct
23 Correct 241 ms 3528 KB Output is correct
24 Correct 247 ms 3408 KB Output is correct
25 Correct 211 ms 5444 KB Output is correct
26 Correct 250 ms 3932 KB Output is correct
27 Correct 269 ms 3824 KB Output is correct
28 Correct 155 ms 6404 KB Output is correct
29 Correct 199 ms 2404 KB Output is correct
30 Correct 212 ms 4600 KB Output is correct
31 Correct 271 ms 4368 KB Output is correct
32 Correct 251 ms 4472 KB Output is correct
33 Correct 203 ms 3292 KB Output is correct
34 Correct 295 ms 3536 KB Output is correct
35 Correct 254 ms 3832 KB Output is correct
36 Correct 194 ms 2492 KB Output is correct
37 Correct 195 ms 4444 KB Output is correct
38 Correct 238 ms 2408 KB Output is correct
39 Correct 225 ms 3632 KB Output is correct
40 Correct 263 ms 3388 KB Output is correct
41 Correct 42 ms 14936 KB Output is correct
42 Correct 48 ms 14952 KB Output is correct
43 Correct 54 ms 14968 KB Output is correct
44 Correct 54 ms 14880 KB Output is correct
45 Correct 45 ms 15060 KB Output is correct
46 Correct 55 ms 14916 KB Output is correct
47 Correct 58 ms 14972 KB Output is correct
48 Correct 37 ms 15192 KB Output is correct
49 Correct 46 ms 14820 KB Output is correct
50 Correct 62 ms 14952 KB Output is correct
51 Correct 51 ms 14964 KB Output is correct
52 Correct 74 ms 14936 KB Output is correct
53 Correct 45 ms 14848 KB Output is correct
54 Correct 56 ms 14924 KB Output is correct
55 Correct 56 ms 14892 KB Output is correct
56 Correct 38 ms 14820 KB Output is correct
57 Correct 44 ms 14996 KB Output is correct
58 Correct 50 ms 14820 KB Output is correct
59 Correct 63 ms 14944 KB Output is correct
60 Correct 66 ms 14976 KB Output is correct
61 Correct 45 ms 14880 KB Output is correct
62 Correct 69 ms 14932 KB Output is correct
63 Correct 58 ms 14936 KB Output is correct
64 Correct 43 ms 14820 KB Output is correct
65 Correct 54 ms 14992 KB Output is correct
66 Correct 59 ms 14948 KB Output is correct
67 Correct 52 ms 14948 KB Output is correct
68 Correct 52 ms 14940 KB Output is correct
69 Correct 50 ms 14948 KB Output is correct
70 Correct 61 ms 15020 KB Output is correct
71 Correct 380 ms 19812 KB Output is correct
72 Correct 350 ms 19912 KB Output is correct
73 Correct 508 ms 18404 KB Output is correct
74 Correct 544 ms 18744 KB Output is correct
75 Correct 312 ms 20780 KB Output is correct
76 Correct 593 ms 19072 KB Output is correct
77 Correct 548 ms 18988 KB Output is correct
78 Correct 230 ms 22756 KB Output is correct
79 Correct 328 ms 16708 KB Output is correct
80 Correct 332 ms 19884 KB Output is correct
81 Correct 424 ms 19888 KB Output is correct
82 Correct 555 ms 18880 KB Output is correct
83 Correct 328 ms 17892 KB Output is correct
84 Correct 561 ms 18772 KB Output is correct
85 Correct 604 ms 19064 KB Output is correct
86 Correct 233 ms 16856 KB Output is correct
87 Correct 318 ms 19696 KB Output is correct
88 Correct 383 ms 16740 KB Output is correct
89 Correct 389 ms 18468 KB Output is correct
90 Correct 463 ms 18796 KB Output is correct
91 Correct 383 ms 17936 KB Output is correct
92 Correct 671 ms 19236 KB Output is correct
93 Correct 602 ms 19004 KB Output is correct
94 Correct 201 ms 16700 KB Output is correct
95 Correct 488 ms 18824 KB Output is correct
96 Correct 544 ms 18812 KB Output is correct
97 Correct 494 ms 18864 KB Output is correct
98 Correct 512 ms 18916 KB Output is correct
99 Correct 579 ms 18916 KB Output is correct
100 Correct 499 ms 18820 KB Output is correct