Submission #522345

# Submission time Handle Problem Language Result Execution time Memory
522345 2022-02-04T16:08:22 Z Monarchuwu Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
723 ms 25956 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 ?
            for (int m = m2; true; m = (m - 1) & m2) {
                ans += a[m | m1];
                if (m == 0) break;
            }
        }
        else if (lg[m0] <= l / 3) { // inc-exc 0, dp sos 1 and ? => dùng g
            for (int m = m0; true; m = (m - 1) & m0) {
                if (lg[m] & 1)
                    ans -= g[m | m1];
                else ans += g[m | m1];
                if (m == 0) break;
            }
        }
        else { // inc-exc 1, dp sos 0 and ? => dùng f
            for (int m = m1, k = lg[m1] & 1; true; m = (m - 1) & m1) {
                if ((lg[m] & 1) == k)
                    ans += f[m | m2];
                else ans -= f[m | m2];
                if (m == 0) break;
            }
        }
        cout << ans << '\n';
    }
}
/**  /\_/\
 *  (= ._.)
 *  / >0  \>1
**/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 368 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 324 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
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 368 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 324 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 198 ms 4876 KB Output is correct
12 Correct 196 ms 4544 KB Output is correct
13 Correct 219 ms 3908 KB Output is correct
14 Correct 202 ms 3780 KB Output is correct
15 Correct 200 ms 4884 KB Output is correct
16 Correct 223 ms 4036 KB Output is correct
17 Correct 230 ms 3924 KB Output is correct
18 Correct 153 ms 5828 KB Output is correct
19 Correct 183 ms 2828 KB Output is correct
20 Correct 198 ms 4492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 368 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 324 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 198 ms 4876 KB Output is correct
12 Correct 196 ms 4544 KB Output is correct
13 Correct 219 ms 3908 KB Output is correct
14 Correct 202 ms 3780 KB Output is correct
15 Correct 200 ms 4884 KB Output is correct
16 Correct 223 ms 4036 KB Output is correct
17 Correct 230 ms 3924 KB Output is correct
18 Correct 153 ms 5828 KB Output is correct
19 Correct 183 ms 2828 KB Output is correct
20 Correct 198 ms 4492 KB Output is correct
21 Correct 209 ms 5020 KB Output is correct
22 Correct 218 ms 4936 KB Output is correct
23 Correct 254 ms 3964 KB Output is correct
24 Correct 254 ms 3868 KB Output is correct
25 Correct 227 ms 5812 KB Output is correct
26 Correct 266 ms 4420 KB Output is correct
27 Correct 261 ms 4280 KB Output is correct
28 Correct 163 ms 6832 KB Output is correct
29 Correct 205 ms 2876 KB Output is correct
30 Correct 225 ms 4936 KB Output is correct
31 Correct 243 ms 4884 KB Output is correct
32 Correct 254 ms 4876 KB Output is correct
33 Correct 222 ms 3652 KB Output is correct
34 Correct 254 ms 3816 KB Output is correct
35 Correct 282 ms 4548 KB Output is correct
36 Correct 178 ms 2764 KB Output is correct
37 Correct 211 ms 5024 KB Output is correct
38 Correct 217 ms 3012 KB Output is correct
39 Correct 235 ms 4032 KB Output is correct
40 Correct 243 ms 3792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 368 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 324 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 49 ms 18488 KB Output is correct
12 Correct 55 ms 18496 KB Output is correct
13 Correct 55 ms 18380 KB Output is correct
14 Correct 62 ms 18404 KB Output is correct
15 Correct 53 ms 18548 KB Output is correct
16 Correct 64 ms 18552 KB Output is correct
17 Correct 67 ms 18448 KB Output is correct
18 Correct 45 ms 18540 KB Output is correct
19 Correct 45 ms 18276 KB Output is correct
20 Correct 53 ms 18524 KB Output is correct
21 Correct 55 ms 18484 KB Output is correct
22 Correct 61 ms 18404 KB Output is correct
23 Correct 48 ms 18404 KB Output is correct
24 Correct 62 ms 18336 KB Output is correct
25 Correct 66 ms 18372 KB Output is correct
26 Correct 43 ms 18276 KB Output is correct
27 Correct 50 ms 18500 KB Output is correct
28 Correct 48 ms 18252 KB Output is correct
29 Correct 55 ms 18412 KB Output is correct
30 Correct 58 ms 18372 KB Output is correct
31 Correct 51 ms 18324 KB Output is correct
32 Correct 78 ms 18396 KB Output is correct
33 Correct 72 ms 18468 KB Output is correct
34 Correct 40 ms 18296 KB Output is correct
35 Correct 63 ms 18632 KB Output is correct
36 Correct 60 ms 18412 KB Output is correct
37 Correct 62 ms 18384 KB Output is correct
38 Correct 60 ms 18564 KB Output is correct
39 Correct 60 ms 18364 KB Output is correct
40 Correct 58 ms 18364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 368 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 324 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 198 ms 4876 KB Output is correct
12 Correct 196 ms 4544 KB Output is correct
13 Correct 219 ms 3908 KB Output is correct
14 Correct 202 ms 3780 KB Output is correct
15 Correct 200 ms 4884 KB Output is correct
16 Correct 223 ms 4036 KB Output is correct
17 Correct 230 ms 3924 KB Output is correct
18 Correct 153 ms 5828 KB Output is correct
19 Correct 183 ms 2828 KB Output is correct
20 Correct 198 ms 4492 KB Output is correct
21 Correct 209 ms 5020 KB Output is correct
22 Correct 218 ms 4936 KB Output is correct
23 Correct 254 ms 3964 KB Output is correct
24 Correct 254 ms 3868 KB Output is correct
25 Correct 227 ms 5812 KB Output is correct
26 Correct 266 ms 4420 KB Output is correct
27 Correct 261 ms 4280 KB Output is correct
28 Correct 163 ms 6832 KB Output is correct
29 Correct 205 ms 2876 KB Output is correct
30 Correct 225 ms 4936 KB Output is correct
31 Correct 243 ms 4884 KB Output is correct
32 Correct 254 ms 4876 KB Output is correct
33 Correct 222 ms 3652 KB Output is correct
34 Correct 254 ms 3816 KB Output is correct
35 Correct 282 ms 4548 KB Output is correct
36 Correct 178 ms 2764 KB Output is correct
37 Correct 211 ms 5024 KB Output is correct
38 Correct 217 ms 3012 KB Output is correct
39 Correct 235 ms 4032 KB Output is correct
40 Correct 243 ms 3792 KB Output is correct
41 Correct 49 ms 18488 KB Output is correct
42 Correct 55 ms 18496 KB Output is correct
43 Correct 55 ms 18380 KB Output is correct
44 Correct 62 ms 18404 KB Output is correct
45 Correct 53 ms 18548 KB Output is correct
46 Correct 64 ms 18552 KB Output is correct
47 Correct 67 ms 18448 KB Output is correct
48 Correct 45 ms 18540 KB Output is correct
49 Correct 45 ms 18276 KB Output is correct
50 Correct 53 ms 18524 KB Output is correct
51 Correct 55 ms 18484 KB Output is correct
52 Correct 61 ms 18404 KB Output is correct
53 Correct 48 ms 18404 KB Output is correct
54 Correct 62 ms 18336 KB Output is correct
55 Correct 66 ms 18372 KB Output is correct
56 Correct 43 ms 18276 KB Output is correct
57 Correct 50 ms 18500 KB Output is correct
58 Correct 48 ms 18252 KB Output is correct
59 Correct 55 ms 18412 KB Output is correct
60 Correct 58 ms 18372 KB Output is correct
61 Correct 51 ms 18324 KB Output is correct
62 Correct 78 ms 18396 KB Output is correct
63 Correct 72 ms 18468 KB Output is correct
64 Correct 40 ms 18296 KB Output is correct
65 Correct 63 ms 18632 KB Output is correct
66 Correct 60 ms 18412 KB Output is correct
67 Correct 62 ms 18384 KB Output is correct
68 Correct 60 ms 18564 KB Output is correct
69 Correct 60 ms 18364 KB Output is correct
70 Correct 58 ms 18364 KB Output is correct
71 Correct 362 ms 23280 KB Output is correct
72 Correct 397 ms 23144 KB Output is correct
73 Correct 449 ms 21604 KB Output is correct
74 Correct 557 ms 21948 KB Output is correct
75 Correct 361 ms 24064 KB Output is correct
76 Correct 681 ms 22392 KB Output is correct
77 Correct 665 ms 22212 KB Output is correct
78 Correct 220 ms 25956 KB Output is correct
79 Correct 312 ms 19908 KB Output is correct
80 Correct 410 ms 23208 KB Output is correct
81 Correct 520 ms 23036 KB Output is correct
82 Correct 590 ms 22036 KB Output is correct
83 Correct 340 ms 21220 KB Output is correct
84 Correct 482 ms 23972 KB Output is correct
85 Correct 635 ms 24176 KB Output is correct
86 Correct 232 ms 21988 KB Output is correct
87 Correct 367 ms 24924 KB Output is correct
88 Correct 332 ms 22068 KB Output is correct
89 Correct 470 ms 23800 KB Output is correct
90 Correct 454 ms 23972 KB Output is correct
91 Correct 353 ms 23064 KB Output is correct
92 Correct 723 ms 24272 KB Output is correct
93 Correct 652 ms 24164 KB Output is correct
94 Correct 214 ms 21912 KB Output is correct
95 Correct 556 ms 24088 KB Output is correct
96 Correct 549 ms 24036 KB Output is correct
97 Correct 570 ms 23928 KB Output is correct
98 Correct 554 ms 24144 KB Output is correct
99 Correct 554 ms 23908 KB Output is correct
100 Correct 572 ms 23952 KB Output is correct