Submission #1100371

# Submission time Handle Problem Language Result Execution time Memory
1100371 2024-10-13T16:21:25 Z KietJ Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
647 ms 50764 KB
#include <bits/stdc++.h>
using namespace std;

#define f first
#define s second
#define pb(x) push_back(x)
#define sz(x) (int) x.size()
#define all(x) x.begin(), x.end()
#define un(x) (x).erase(unique(all(x)), x.end())

typedef long long ll;
typedef double db;
typedef pair <int, int> ii;
typedef pair <ll, ll> pll;
typedef vector <ll> V;

const int N = 1e7 + 1;
const int M = (1 << 20);
const int mod = 1e9 + 7;
const int inf = 1061109567;
const int block = 490;

template <class T1, class Gz> void add(T1 &x, Gz y) { x += y; if (x < 0) x += mod; if (x >= mod) x -= mod; }
template <class T1, class Gz> bool maximize(T1 &x, Gz y) { if (x < y) { x = y; return true; } return false; }
template <class T1, class Gz> bool minimize(T1 &x, Gz y) { if (x > y) { x = y; return true; } return false; }

int n, q, a[M];
ll dp[2][M];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    cin >> n >> q;

    for(int i = 0; i < (1 << n); i++) {
        char x; cin >> x;
        a[i] = x - '0';
        for(int j = 0; j < 2; j++)  
            dp[j][i] = a[i];
    }

    for(int i = 0; i < n; i++) {
        for(int mask = 0; mask < (1 << n); mask++) {
            if (mask >> i & 1) {
                dp[0][mask ^ (1 << i)] += dp[0][mask]; 
                dp[1][mask] += dp[1][mask ^ (1 << i)]; 
            }
        }
    }

    for(int i = 1; i <= q; i++) {
        string s; cin >> s;
        reverse(all(s));

        vector <int> mask(3, 0);

        for(int j = 0; j < n; j++) {
            char x = s[j];
            if (x == '0') mask[0] ^= (1 << j);
            if (x == '1') mask[1] ^= (1 << j);
            if (x == '?') mask[2] ^= (1 << j);
        }

        ll ans = 0;
        if (__builtin_popcount(mask[2]) <= 6) {
            ans = a[mask[1]];
            for(int mask1 = mask[2]; mask1; mask1 = (mask1 - 1) & mask[2]) {
                ans += a[mask[1] ^ mask1];
            }
        } else {
            if (__builtin_popcount(mask[0]) <= 6) {
                ans = dp[0][mask[1]];
                for(int mask1 = mask[0]; mask1; mask1 = (mask1 - 1) & mask[0]) {
                    if (__builtin_popcount(mask1) & 1) {
                        ans -= dp[0][mask[1] ^ mask1];
                    } else {
                        ans += dp[0][mask[1] ^ mask1];
                    }
                }
            } else {
                int lim = __builtin_popcount(mask[1]);

                if (lim & 1) {
                    ans -= dp[1][mask[2]];
                } else {
                    ans += dp[1][mask[2]];
                }

                for(int mask1 = mask[1]; mask1; mask1 = (mask1 - 1) & mask[1]) {
                    if ((lim - __builtin_popcount(mask1)) & 1) {
                        ans -= dp[1][mask1 ^ mask[2]];
                    }  else {
                        ans += dp[1][mask1 ^ mask[2]];
                    }
                }
            }
        } cout << ans << "\n";
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4432 KB Output is correct
2 Correct 2 ms 4432 KB Output is correct
3 Correct 1 ms 4600 KB Output is correct
4 Correct 1 ms 4432 KB Output is correct
5 Correct 1 ms 4432 KB Output is correct
6 Correct 1 ms 4432 KB Output is correct
7 Correct 1 ms 4432 KB Output is correct
8 Correct 1 ms 4432 KB Output is correct
9 Correct 1 ms 4432 KB Output is correct
10 Correct 1 ms 4432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4432 KB Output is correct
2 Correct 2 ms 4432 KB Output is correct
3 Correct 1 ms 4600 KB Output is correct
4 Correct 1 ms 4432 KB Output is correct
5 Correct 1 ms 4432 KB Output is correct
6 Correct 1 ms 4432 KB Output is correct
7 Correct 1 ms 4432 KB Output is correct
8 Correct 1 ms 4432 KB Output is correct
9 Correct 1 ms 4432 KB Output is correct
10 Correct 1 ms 4432 KB Output is correct
11 Correct 190 ms 8464 KB Output is correct
12 Correct 189 ms 8012 KB Output is correct
13 Correct 186 ms 7496 KB Output is correct
14 Correct 184 ms 7496 KB Output is correct
15 Correct 219 ms 8508 KB Output is correct
16 Correct 200 ms 7636 KB Output is correct
17 Correct 200 ms 7516 KB Output is correct
18 Correct 119 ms 9524 KB Output is correct
19 Correct 148 ms 6476 KB Output is correct
20 Correct 181 ms 8008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4432 KB Output is correct
2 Correct 2 ms 4432 KB Output is correct
3 Correct 1 ms 4600 KB Output is correct
4 Correct 1 ms 4432 KB Output is correct
5 Correct 1 ms 4432 KB Output is correct
6 Correct 1 ms 4432 KB Output is correct
7 Correct 1 ms 4432 KB Output is correct
8 Correct 1 ms 4432 KB Output is correct
9 Correct 1 ms 4432 KB Output is correct
10 Correct 1 ms 4432 KB Output is correct
11 Correct 190 ms 8464 KB Output is correct
12 Correct 189 ms 8012 KB Output is correct
13 Correct 186 ms 7496 KB Output is correct
14 Correct 184 ms 7496 KB Output is correct
15 Correct 219 ms 8508 KB Output is correct
16 Correct 200 ms 7636 KB Output is correct
17 Correct 200 ms 7516 KB Output is correct
18 Correct 119 ms 9524 KB Output is correct
19 Correct 148 ms 6476 KB Output is correct
20 Correct 181 ms 8008 KB Output is correct
21 Correct 170 ms 8528 KB Output is correct
22 Correct 252 ms 8776 KB Output is correct
23 Correct 212 ms 7816 KB Output is correct
24 Correct 223 ms 7496 KB Output is correct
25 Correct 206 ms 9544 KB Output is correct
26 Correct 232 ms 8008 KB Output is correct
27 Correct 245 ms 8024 KB Output is correct
28 Correct 120 ms 10636 KB Output is correct
29 Correct 175 ms 6496 KB Output is correct
30 Correct 217 ms 8776 KB Output is correct
31 Correct 249 ms 8500 KB Output is correct
32 Correct 235 ms 8520 KB Output is correct
33 Correct 189 ms 7508 KB Output is correct
34 Correct 212 ms 7568 KB Output is correct
35 Correct 234 ms 8008 KB Output is correct
36 Correct 129 ms 6616 KB Output is correct
37 Correct 230 ms 8520 KB Output is correct
38 Correct 190 ms 6472 KB Output is correct
39 Correct 218 ms 7752 KB Output is correct
40 Correct 216 ms 7500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4432 KB Output is correct
2 Correct 2 ms 4432 KB Output is correct
3 Correct 1 ms 4600 KB Output is correct
4 Correct 1 ms 4432 KB Output is correct
5 Correct 1 ms 4432 KB Output is correct
6 Correct 1 ms 4432 KB Output is correct
7 Correct 1 ms 4432 KB Output is correct
8 Correct 1 ms 4432 KB Output is correct
9 Correct 1 ms 4432 KB Output is correct
10 Correct 1 ms 4432 KB Output is correct
11 Correct 48 ms 21064 KB Output is correct
12 Correct 49 ms 21064 KB Output is correct
13 Correct 54 ms 21064 KB Output is correct
14 Correct 66 ms 23008 KB Output is correct
15 Correct 51 ms 23124 KB Output is correct
16 Correct 64 ms 23116 KB Output is correct
17 Correct 57 ms 23008 KB Output is correct
18 Correct 44 ms 23292 KB Output is correct
19 Correct 49 ms 23116 KB Output is correct
20 Correct 55 ms 23116 KB Output is correct
21 Correct 69 ms 23116 KB Output is correct
22 Correct 64 ms 23116 KB Output is correct
23 Correct 50 ms 23128 KB Output is correct
24 Correct 56 ms 23116 KB Output is correct
25 Correct 63 ms 23004 KB Output is correct
26 Correct 49 ms 23116 KB Output is correct
27 Correct 49 ms 23116 KB Output is correct
28 Correct 51 ms 23116 KB Output is correct
29 Correct 56 ms 23116 KB Output is correct
30 Correct 55 ms 23124 KB Output is correct
31 Correct 52 ms 22996 KB Output is correct
32 Correct 61 ms 23116 KB Output is correct
33 Correct 65 ms 23000 KB Output is correct
34 Correct 44 ms 23116 KB Output is correct
35 Correct 54 ms 23124 KB Output is correct
36 Correct 58 ms 23116 KB Output is correct
37 Correct 56 ms 23036 KB Output is correct
38 Correct 54 ms 22996 KB Output is correct
39 Correct 53 ms 23116 KB Output is correct
40 Correct 57 ms 23120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4432 KB Output is correct
2 Correct 2 ms 4432 KB Output is correct
3 Correct 1 ms 4600 KB Output is correct
4 Correct 1 ms 4432 KB Output is correct
5 Correct 1 ms 4432 KB Output is correct
6 Correct 1 ms 4432 KB Output is correct
7 Correct 1 ms 4432 KB Output is correct
8 Correct 1 ms 4432 KB Output is correct
9 Correct 1 ms 4432 KB Output is correct
10 Correct 1 ms 4432 KB Output is correct
11 Correct 190 ms 8464 KB Output is correct
12 Correct 189 ms 8012 KB Output is correct
13 Correct 186 ms 7496 KB Output is correct
14 Correct 184 ms 7496 KB Output is correct
15 Correct 219 ms 8508 KB Output is correct
16 Correct 200 ms 7636 KB Output is correct
17 Correct 200 ms 7516 KB Output is correct
18 Correct 119 ms 9524 KB Output is correct
19 Correct 148 ms 6476 KB Output is correct
20 Correct 181 ms 8008 KB Output is correct
21 Correct 170 ms 8528 KB Output is correct
22 Correct 252 ms 8776 KB Output is correct
23 Correct 212 ms 7816 KB Output is correct
24 Correct 223 ms 7496 KB Output is correct
25 Correct 206 ms 9544 KB Output is correct
26 Correct 232 ms 8008 KB Output is correct
27 Correct 245 ms 8024 KB Output is correct
28 Correct 120 ms 10636 KB Output is correct
29 Correct 175 ms 6496 KB Output is correct
30 Correct 217 ms 8776 KB Output is correct
31 Correct 249 ms 8500 KB Output is correct
32 Correct 235 ms 8520 KB Output is correct
33 Correct 189 ms 7508 KB Output is correct
34 Correct 212 ms 7568 KB Output is correct
35 Correct 234 ms 8008 KB Output is correct
36 Correct 129 ms 6616 KB Output is correct
37 Correct 230 ms 8520 KB Output is correct
38 Correct 190 ms 6472 KB Output is correct
39 Correct 218 ms 7752 KB Output is correct
40 Correct 216 ms 7500 KB Output is correct
41 Correct 48 ms 21064 KB Output is correct
42 Correct 49 ms 21064 KB Output is correct
43 Correct 54 ms 21064 KB Output is correct
44 Correct 66 ms 23008 KB Output is correct
45 Correct 51 ms 23124 KB Output is correct
46 Correct 64 ms 23116 KB Output is correct
47 Correct 57 ms 23008 KB Output is correct
48 Correct 44 ms 23292 KB Output is correct
49 Correct 49 ms 23116 KB Output is correct
50 Correct 55 ms 23116 KB Output is correct
51 Correct 69 ms 23116 KB Output is correct
52 Correct 64 ms 23116 KB Output is correct
53 Correct 50 ms 23128 KB Output is correct
54 Correct 56 ms 23116 KB Output is correct
55 Correct 63 ms 23004 KB Output is correct
56 Correct 49 ms 23116 KB Output is correct
57 Correct 49 ms 23116 KB Output is correct
58 Correct 51 ms 23116 KB Output is correct
59 Correct 56 ms 23116 KB Output is correct
60 Correct 55 ms 23124 KB Output is correct
61 Correct 52 ms 22996 KB Output is correct
62 Correct 61 ms 23116 KB Output is correct
63 Correct 65 ms 23000 KB Output is correct
64 Correct 44 ms 23116 KB Output is correct
65 Correct 54 ms 23124 KB Output is correct
66 Correct 58 ms 23116 KB Output is correct
67 Correct 56 ms 23036 KB Output is correct
68 Correct 54 ms 22996 KB Output is correct
69 Correct 53 ms 23116 KB Output is correct
70 Correct 57 ms 23120 KB Output is correct
71 Correct 282 ms 47436 KB Output is correct
72 Correct 330 ms 47692 KB Output is correct
73 Correct 357 ms 46156 KB Output is correct
74 Correct 584 ms 46532 KB Output is correct
75 Correct 346 ms 48460 KB Output is correct
76 Correct 542 ms 46812 KB Output is correct
77 Correct 465 ms 46660 KB Output is correct
78 Correct 212 ms 50764 KB Output is correct
79 Correct 288 ms 44488 KB Output is correct
80 Correct 355 ms 47692 KB Output is correct
81 Correct 434 ms 47376 KB Output is correct
82 Correct 647 ms 46560 KB Output is correct
83 Correct 309 ms 45644 KB Output is correct
84 Correct 450 ms 46412 KB Output is correct
85 Correct 480 ms 46560 KB Output is correct
86 Correct 200 ms 44364 KB Output is correct
87 Correct 345 ms 47436 KB Output is correct
88 Correct 275 ms 44392 KB Output is correct
89 Correct 335 ms 46156 KB Output is correct
90 Correct 410 ms 46412 KB Output is correct
91 Correct 312 ms 45532 KB Output is correct
92 Correct 535 ms 46668 KB Output is correct
93 Correct 456 ms 46556 KB Output is correct
94 Correct 204 ms 44364 KB Output is correct
95 Correct 415 ms 46668 KB Output is correct
96 Correct 453 ms 46588 KB Output is correct
97 Correct 481 ms 46496 KB Output is correct
98 Correct 407 ms 46464 KB Output is correct
99 Correct 451 ms 46548 KB Output is correct
100 Correct 433 ms 46668 KB Output is correct