Submission #961215

# Submission time Handle Problem Language Result Execution time Memory
961215 2024-04-11T17:30:14 Z efishel Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
662 ms 48020 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector <ll>;

const ll MAXN = 1.1E6;
ll dp1[MAXN], dp0[MAXN];

int main () {
    cin.tie(nullptr) -> sync_with_stdio(false);
    ll n, Q;
    cin >> n >> Q;
    ll ALL = (1<<n)-1;
    string str;
    cin >> str;
    for (ll i = 0; i <= ALL; i++)
        dp1[i] = str[i]-'0';
    for (ll bit = 0; bit < n; bit++)
        for (int mask = 0; mask <= ALL; mask++)
            if (mask>>bit&1) dp1[mask] += dp1[mask ^ (1<<bit)];
    for (ll i = 0; i <= ALL; i++)
        dp0[i] = str[i ^ ALL]-'0';
    for (ll bit = 0; bit < n; bit++)
        for (int mask = 0; mask <= ALL; mask++)
            if (mask>>bit&1) dp0[mask] += dp0[mask ^ (1<<bit)];
    while (Q--) {
        string q;
        cin >> q;
        reverse(q.begin(), q.end());
        int nul = 0, unu = 0, dem;
        for (ll i = 0; i < n; i++) nul |= (q[i] == '0') << i;
        for (ll i = 0; i < n; i++) unu |= (q[i] == '1') << i;
        dem = ALL ^ nul ^ unu;
        if (count(q.begin(), q.end(), '?') <= 6) {
            ll ans = 0;
            for (int mask = 0; mask <= ALL; mask++) {
                mask |= unu | nul;
                ans += str[mask ^ nul]-'0';
            }
            cout << ans << '\n';
            continue;
        }
        if (count(q.begin(), q.end(), '0') <= 6) {
            ll ans = 0;
            for (int mask = nul; ; mask--, mask &= nul) {
                bool addSub = __builtin_popcountll(mask ^ nul)%2 == 0;
                ans += (addSub ? 1 : -1) * dp0[dem | mask];
                if (!mask) break;
            }
            cout << ans << '\n';
            continue;
        }
        if (count(q.begin(), q.end(), '1') <= 6) {
            ll ans = 0;
            for (int mask = unu; ; mask--, mask &= unu) {
                bool addSub = __builtin_popcountll(mask ^ unu)%2 == 0;
                ans += (addSub ? 1 : -1) * dp1[dem | mask];
                if (!mask) break;
            }
            cout << ans << '\n';
            continue;
        }
        assert(false);
    }
    return 0;
}

/*

????? - ????0
= ????1

????? - ???00
= ???01
+ ???10
+ ???11

????? = ???00 + ???01 + ???10 + ???11
00??? = 00?00 + 00?01 + 00?10 + 00?11

iterate over all subsets of 1's, lets call it 'mask'
check the parity of 'mask^unu' and subtract/add accordingly:
    0: subtract
    1: add
what you work with is dp[maskQM^mask]

   ??????????
+dp1111111111
=
 + ???????000
+dp1111111000

 + ???????001
+dp1111111001
-dp1111111000

 + ???????010
+dp1111111010
-dp1111111000

 + ???????011
+dp1111111011
-dp1111111001
-dp1111111010
+dp1111111000

 + ???????100
+dp1111111100
-dp1111111000

 + ???????101
+dp1111111101
-dp1111111001
-dp1111111100
+dp1111111000

 + ???????110
+dp1111111110
-dp1111111010
-dp1111111100
+dp1111111000

 + ???????111
+dp1111111111
-dp1111111011
-dp1111111101
-dp1111111110
+dp1111111001
+dp1111111010
+dp1111111100
-dp1111111000


// tallied up

 + ???????000
 + ???????001
 + ???????010
 + ???????011
 + ???????100
 + ???????101
 + ???????110
 + ???????111
=
+dp1111111111


// 

-dp1111111111
+dp1111111011
+dp1111111101
+dp1111111110
-dp1111111001
-dp1111111010
-dp1111111100
+dp1111111000

// AHAHAHAHAH TEHWYW WEOUHWEOYHWE HUWHYYYYW HYYY WHYYYY WHYY TOAEUNOAEUNOHEUNSHOASENU HOANEHUOMG OMG WYH
 + ???????111
-dp1111111111
+dp1111111011
+dp1111111101
+dp1111111110
-dp1111111001
-dp1111111010
-dp1111111100
+dp1111111000


*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2648 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2648 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 166 ms 17156 KB Output is correct
12 Correct 178 ms 16900 KB Output is correct
13 Correct 149 ms 16128 KB Output is correct
14 Correct 146 ms 16076 KB Output is correct
15 Correct 194 ms 17072 KB Output is correct
16 Correct 158 ms 16464 KB Output is correct
17 Correct 158 ms 16208 KB Output is correct
18 Correct 150 ms 18044 KB Output is correct
19 Correct 161 ms 15144 KB Output is correct
20 Correct 173 ms 16932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2648 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 166 ms 17156 KB Output is correct
12 Correct 178 ms 16900 KB Output is correct
13 Correct 149 ms 16128 KB Output is correct
14 Correct 146 ms 16076 KB Output is correct
15 Correct 194 ms 17072 KB Output is correct
16 Correct 158 ms 16464 KB Output is correct
17 Correct 158 ms 16208 KB Output is correct
18 Correct 150 ms 18044 KB Output is correct
19 Correct 161 ms 15144 KB Output is correct
20 Correct 173 ms 16932 KB Output is correct
21 Correct 156 ms 20304 KB Output is correct
22 Correct 277 ms 20428 KB Output is correct
23 Correct 174 ms 19412 KB Output is correct
24 Correct 163 ms 19080 KB Output is correct
25 Correct 198 ms 21152 KB Output is correct
26 Correct 193 ms 19792 KB Output is correct
27 Correct 190 ms 19888 KB Output is correct
28 Correct 172 ms 22100 KB Output is correct
29 Correct 146 ms 18220 KB Output is correct
30 Correct 180 ms 20308 KB Output is correct
31 Correct 214 ms 20320 KB Output is correct
32 Correct 185 ms 20308 KB Output is correct
33 Correct 163 ms 18968 KB Output is correct
34 Correct 166 ms 19280 KB Output is correct
35 Correct 181 ms 19536 KB Output is correct
36 Correct 151 ms 18264 KB Output is correct
37 Correct 208 ms 20264 KB Output is correct
38 Correct 158 ms 18236 KB Output is correct
39 Correct 180 ms 19364 KB Output is correct
40 Correct 171 ms 19268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2648 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 52 ms 20532 KB Output is correct
12 Correct 62 ms 20532 KB Output is correct
13 Correct 53 ms 20536 KB Output is correct
14 Correct 74 ms 20460 KB Output is correct
15 Correct 52 ms 20528 KB Output is correct
16 Correct 59 ms 20536 KB Output is correct
17 Correct 68 ms 20468 KB Output is correct
18 Correct 58 ms 20724 KB Output is correct
19 Correct 46 ms 20508 KB Output is correct
20 Correct 49 ms 20724 KB Output is correct
21 Correct 74 ms 20708 KB Output is correct
22 Correct 62 ms 20672 KB Output is correct
23 Correct 48 ms 20468 KB Output is correct
24 Correct 63 ms 20520 KB Output is correct
25 Correct 72 ms 20512 KB Output is correct
26 Correct 47 ms 20736 KB Output is correct
27 Correct 55 ms 20720 KB Output is correct
28 Correct 49 ms 20716 KB Output is correct
29 Correct 49 ms 20468 KB Output is correct
30 Correct 51 ms 20632 KB Output is correct
31 Correct 55 ms 20400 KB Output is correct
32 Correct 88 ms 20456 KB Output is correct
33 Correct 68 ms 20468 KB Output is correct
34 Correct 46 ms 20464 KB Output is correct
35 Correct 63 ms 20532 KB Output is correct
36 Correct 56 ms 20468 KB Output is correct
37 Correct 59 ms 20472 KB Output is correct
38 Correct 60 ms 20588 KB Output is correct
39 Correct 58 ms 20452 KB Output is correct
40 Correct 57 ms 20532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2648 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 166 ms 17156 KB Output is correct
12 Correct 178 ms 16900 KB Output is correct
13 Correct 149 ms 16128 KB Output is correct
14 Correct 146 ms 16076 KB Output is correct
15 Correct 194 ms 17072 KB Output is correct
16 Correct 158 ms 16464 KB Output is correct
17 Correct 158 ms 16208 KB Output is correct
18 Correct 150 ms 18044 KB Output is correct
19 Correct 161 ms 15144 KB Output is correct
20 Correct 173 ms 16932 KB Output is correct
21 Correct 156 ms 20304 KB Output is correct
22 Correct 277 ms 20428 KB Output is correct
23 Correct 174 ms 19412 KB Output is correct
24 Correct 163 ms 19080 KB Output is correct
25 Correct 198 ms 21152 KB Output is correct
26 Correct 193 ms 19792 KB Output is correct
27 Correct 190 ms 19888 KB Output is correct
28 Correct 172 ms 22100 KB Output is correct
29 Correct 146 ms 18220 KB Output is correct
30 Correct 180 ms 20308 KB Output is correct
31 Correct 214 ms 20320 KB Output is correct
32 Correct 185 ms 20308 KB Output is correct
33 Correct 163 ms 18968 KB Output is correct
34 Correct 166 ms 19280 KB Output is correct
35 Correct 181 ms 19536 KB Output is correct
36 Correct 151 ms 18264 KB Output is correct
37 Correct 208 ms 20264 KB Output is correct
38 Correct 158 ms 18236 KB Output is correct
39 Correct 180 ms 19364 KB Output is correct
40 Correct 171 ms 19268 KB Output is correct
41 Correct 52 ms 20532 KB Output is correct
42 Correct 62 ms 20532 KB Output is correct
43 Correct 53 ms 20536 KB Output is correct
44 Correct 74 ms 20460 KB Output is correct
45 Correct 52 ms 20528 KB Output is correct
46 Correct 59 ms 20536 KB Output is correct
47 Correct 68 ms 20468 KB Output is correct
48 Correct 58 ms 20724 KB Output is correct
49 Correct 46 ms 20508 KB Output is correct
50 Correct 49 ms 20724 KB Output is correct
51 Correct 74 ms 20708 KB Output is correct
52 Correct 62 ms 20672 KB Output is correct
53 Correct 48 ms 20468 KB Output is correct
54 Correct 63 ms 20520 KB Output is correct
55 Correct 72 ms 20512 KB Output is correct
56 Correct 47 ms 20736 KB Output is correct
57 Correct 55 ms 20720 KB Output is correct
58 Correct 49 ms 20716 KB Output is correct
59 Correct 49 ms 20468 KB Output is correct
60 Correct 51 ms 20632 KB Output is correct
61 Correct 55 ms 20400 KB Output is correct
62 Correct 88 ms 20456 KB Output is correct
63 Correct 68 ms 20468 KB Output is correct
64 Correct 46 ms 20464 KB Output is correct
65 Correct 63 ms 20532 KB Output is correct
66 Correct 56 ms 20468 KB Output is correct
67 Correct 59 ms 20472 KB Output is correct
68 Correct 60 ms 20588 KB Output is correct
69 Correct 58 ms 20452 KB Output is correct
70 Correct 57 ms 20532 KB Output is correct
71 Correct 295 ms 44936 KB Output is correct
72 Correct 365 ms 44948 KB Output is correct
73 Correct 309 ms 43500 KB Output is correct
74 Correct 618 ms 44112 KB Output is correct
75 Correct 343 ms 45864 KB Output is correct
76 Correct 620 ms 44100 KB Output is correct
77 Correct 531 ms 44188 KB Output is correct
78 Correct 278 ms 48020 KB Output is correct
79 Correct 243 ms 41968 KB Output is correct
80 Correct 306 ms 45084 KB Output is correct
81 Correct 439 ms 45032 KB Output is correct
82 Correct 631 ms 43816 KB Output is correct
83 Correct 256 ms 43068 KB Output is correct
84 Correct 317 ms 44012 KB Output is correct
85 Correct 546 ms 45060 KB Output is correct
86 Correct 231 ms 41888 KB Output is correct
87 Correct 282 ms 45032 KB Output is correct
88 Correct 252 ms 41976 KB Output is correct
89 Correct 295 ms 43504 KB Output is correct
90 Correct 330 ms 43936 KB Output is correct
91 Correct 261 ms 43060 KB Output is correct
92 Correct 662 ms 44272 KB Output is correct
93 Correct 577 ms 44208 KB Output is correct
94 Correct 230 ms 41972 KB Output is correct
95 Correct 464 ms 44012 KB Output is correct
96 Correct 431 ms 44160 KB Output is correct
97 Correct 448 ms 44268 KB Output is correct
98 Correct 439 ms 44068 KB Output is correct
99 Correct 448 ms 44088 KB Output is correct
100 Correct 434 ms 44016 KB Output is correct