Submission #680165

# Submission time Handle Problem Language Result Execution time Memory
680165 2023-01-10T03:46:46 Z Duy_e Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1119 ms 50504 KB
#include <bits/stdc++.h>
#define ll long long
#define pii pair<ll, ll>
#define st first
#define nd second
#define file "test"
#define rep(i, begin, end) for (ll i = (begin); i <= (end); i ++)
#define rrep(i, begin, end) for (ll i = (begin); i >= (end); i --)
using namespace std;
const long long INF = 1e18;
const long long N = 2e6 + 5;

int a[N], L, q, n;

ll dp[2][N];

// dp[bit][mask] = sum of mask such bit on = bit

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
        //freopen(file".inp", "r", stdin); freopen(file".out", "w", stdout);
    #endif
    
    cin >> L >> q;  
    n = 1 << L;

    rep(i, 0, n - 1) {
        char ch; cin >> ch;
        a[i] = ch - '0';
    }

    rep(bit, 0, 1) {
        rep(mask, 0, n - 1) dp[bit][mask] = a[bit == 0 ? (n - 1 - mask) : mask];
        rep(i, 0, L - 1) rep(mask, 0, n - 1) 
            if (mask >> i & 1)
                dp[bit][mask] += dp[bit][mask ^ (1 << i)];
        // rep(mask, 0, n - 1)
        //cout << bit << ' ' << mask << ": " << dp[bit][mask] << '\n';
    }   

    //return 0;
    string s;

    while (q --) {
        int na = 0, nb = 0, nc = 0, ma = 0, mb = 0, mc = 0;
        cin >> s;
        reverse(s.begin(), s.end());

        rep(i, 0, L - 1) {
            if (s[i] == '1') {
                na ++; ma ^= 1 << i;
            }

            if (s[i] == '0') {
                nb ++; mb ^= 1 << i;
            }

            if (s[i] == '?') {
                nc ++; mc ^= 1 << i;
            }
        }

        if (nc <= na && nc <= nb) {
            ll ans = 0;
            for (int i = mc; ; i = (i - 1) & mc) {
                ans += a[i ^ ma];
                if (i == 0) break;
            }            

            cout << ans << '\n';
        } else {
            int bit = 1, mask = ma, other = mb;
            if (nb < na) {
                bit = 0;
                mask = mb;
                other = ma;
            }

            // if (bit == 0) mc ^= (n - 1) ^ m;

            ll ans = 0;
            int bb = __builtin_popcount(mask) & 1;
            for (int i = mask; ; i = (i - 1) & mask) {
                if ((__builtin_popcount(i) & 1) == bb) {
                    ans += dp[bit][i ^ mc];;
                }
                else    {
                    ans -= dp[bit][i ^ mc];
                }
                if (i == 0) break;
            }

            cout << ans << '\n';
        }
    }

    return 0;
}    
/**  /\_/\
*   (= ._.)
*   / >🍵 \>🍮

3 case
cnt 0 min
cnt 1 min
cnt ? min

-> handle each case in 2^cnt, cnt <= L / 3 -> O(q * 2 ^ (L / 3));

**/

Compilation message

snake_escaping.cpp:101:9: warning: "/*" within comment [-Wcomment]
  101 | /**  /\_/\
      |          
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:74:37: warning: variable 'other' set but not used [-Wunused-but-set-variable]
   74 |             int bit = 1, mask = ma, other = mb;
      |                                     ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 205 ms 5016 KB Output is correct
12 Correct 207 ms 4676 KB Output is correct
13 Correct 217 ms 3928 KB Output is correct
14 Correct 224 ms 4060 KB Output is correct
15 Correct 202 ms 5160 KB Output is correct
16 Correct 233 ms 4268 KB Output is correct
17 Correct 241 ms 4240 KB Output is correct
18 Correct 155 ms 6092 KB Output is correct
19 Correct 182 ms 3076 KB Output is correct
20 Correct 199 ms 4676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 205 ms 5016 KB Output is correct
12 Correct 207 ms 4676 KB Output is correct
13 Correct 217 ms 3928 KB Output is correct
14 Correct 224 ms 4060 KB Output is correct
15 Correct 202 ms 5160 KB Output is correct
16 Correct 233 ms 4268 KB Output is correct
17 Correct 241 ms 4240 KB Output is correct
18 Correct 155 ms 6092 KB Output is correct
19 Correct 182 ms 3076 KB Output is correct
20 Correct 199 ms 4676 KB Output is correct
21 Correct 234 ms 5268 KB Output is correct
22 Correct 237 ms 5520 KB Output is correct
23 Correct 249 ms 4420 KB Output is correct
24 Correct 250 ms 4180 KB Output is correct
25 Correct 230 ms 6272 KB Output is correct
26 Correct 288 ms 4680 KB Output is correct
27 Correct 267 ms 4668 KB Output is correct
28 Correct 176 ms 7692 KB Output is correct
29 Correct 198 ms 3220 KB Output is correct
30 Correct 214 ms 5524 KB Output is correct
31 Correct 269 ms 5172 KB Output is correct
32 Correct 283 ms 5268 KB Output is correct
33 Correct 246 ms 4260 KB Output is correct
34 Correct 267 ms 4172 KB Output is correct
35 Correct 301 ms 4680 KB Output is correct
36 Correct 140 ms 3148 KB Output is correct
37 Correct 208 ms 5452 KB Output is correct
38 Correct 205 ms 3052 KB Output is correct
39 Correct 258 ms 3808 KB Output is correct
40 Correct 248 ms 3444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 81 ms 21248 KB Output is correct
12 Correct 75 ms 21080 KB Output is correct
13 Correct 79 ms 20972 KB Output is correct
14 Correct 103 ms 20992 KB Output is correct
15 Correct 86 ms 21196 KB Output is correct
16 Correct 117 ms 20984 KB Output is correct
17 Correct 117 ms 21076 KB Output is correct
18 Correct 67 ms 21196 KB Output is correct
19 Correct 75 ms 20836 KB Output is correct
20 Correct 106 ms 21084 KB Output is correct
21 Correct 85 ms 21096 KB Output is correct
22 Correct 100 ms 21032 KB Output is correct
23 Correct 77 ms 20948 KB Output is correct
24 Correct 101 ms 20988 KB Output is correct
25 Correct 97 ms 20980 KB Output is correct
26 Correct 72 ms 20840 KB Output is correct
27 Correct 77 ms 21076 KB Output is correct
28 Correct 98 ms 20844 KB Output is correct
29 Correct 90 ms 20912 KB Output is correct
30 Correct 84 ms 20968 KB Output is correct
31 Correct 77 ms 20960 KB Output is correct
32 Correct 117 ms 21120 KB Output is correct
33 Correct 96 ms 20964 KB Output is correct
34 Correct 68 ms 20840 KB Output is correct
35 Correct 102 ms 20952 KB Output is correct
36 Correct 91 ms 20968 KB Output is correct
37 Correct 91 ms 20976 KB Output is correct
38 Correct 86 ms 20944 KB Output is correct
39 Correct 93 ms 20972 KB Output is correct
40 Correct 113 ms 21028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 205 ms 5016 KB Output is correct
12 Correct 207 ms 4676 KB Output is correct
13 Correct 217 ms 3928 KB Output is correct
14 Correct 224 ms 4060 KB Output is correct
15 Correct 202 ms 5160 KB Output is correct
16 Correct 233 ms 4268 KB Output is correct
17 Correct 241 ms 4240 KB Output is correct
18 Correct 155 ms 6092 KB Output is correct
19 Correct 182 ms 3076 KB Output is correct
20 Correct 199 ms 4676 KB Output is correct
21 Correct 234 ms 5268 KB Output is correct
22 Correct 237 ms 5520 KB Output is correct
23 Correct 249 ms 4420 KB Output is correct
24 Correct 250 ms 4180 KB Output is correct
25 Correct 230 ms 6272 KB Output is correct
26 Correct 288 ms 4680 KB Output is correct
27 Correct 267 ms 4668 KB Output is correct
28 Correct 176 ms 7692 KB Output is correct
29 Correct 198 ms 3220 KB Output is correct
30 Correct 214 ms 5524 KB Output is correct
31 Correct 269 ms 5172 KB Output is correct
32 Correct 283 ms 5268 KB Output is correct
33 Correct 246 ms 4260 KB Output is correct
34 Correct 267 ms 4172 KB Output is correct
35 Correct 301 ms 4680 KB Output is correct
36 Correct 140 ms 3148 KB Output is correct
37 Correct 208 ms 5452 KB Output is correct
38 Correct 205 ms 3052 KB Output is correct
39 Correct 258 ms 3808 KB Output is correct
40 Correct 248 ms 3444 KB Output is correct
41 Correct 81 ms 21248 KB Output is correct
42 Correct 75 ms 21080 KB Output is correct
43 Correct 79 ms 20972 KB Output is correct
44 Correct 103 ms 20992 KB Output is correct
45 Correct 86 ms 21196 KB Output is correct
46 Correct 117 ms 20984 KB Output is correct
47 Correct 117 ms 21076 KB Output is correct
48 Correct 67 ms 21196 KB Output is correct
49 Correct 75 ms 20836 KB Output is correct
50 Correct 106 ms 21084 KB Output is correct
51 Correct 85 ms 21096 KB Output is correct
52 Correct 100 ms 21032 KB Output is correct
53 Correct 77 ms 20948 KB Output is correct
54 Correct 101 ms 20988 KB Output is correct
55 Correct 97 ms 20980 KB Output is correct
56 Correct 72 ms 20840 KB Output is correct
57 Correct 77 ms 21076 KB Output is correct
58 Correct 98 ms 20844 KB Output is correct
59 Correct 90 ms 20912 KB Output is correct
60 Correct 84 ms 20968 KB Output is correct
61 Correct 77 ms 20960 KB Output is correct
62 Correct 117 ms 21120 KB Output is correct
63 Correct 96 ms 20964 KB Output is correct
64 Correct 68 ms 20840 KB Output is correct
65 Correct 102 ms 20952 KB Output is correct
66 Correct 91 ms 20968 KB Output is correct
67 Correct 91 ms 20976 KB Output is correct
68 Correct 86 ms 20944 KB Output is correct
69 Correct 93 ms 20972 KB Output is correct
70 Correct 113 ms 21028 KB Output is correct
71 Correct 355 ms 39116 KB Output is correct
72 Correct 400 ms 47524 KB Output is correct
73 Correct 514 ms 46124 KB Output is correct
74 Correct 980 ms 46368 KB Output is correct
75 Correct 468 ms 48396 KB Output is correct
76 Correct 1119 ms 46776 KB Output is correct
77 Correct 822 ms 46540 KB Output is correct
78 Correct 255 ms 50504 KB Output is correct
79 Correct 359 ms 44388 KB Output is correct
80 Correct 376 ms 47436 KB Output is correct
81 Correct 601 ms 47296 KB Output is correct
82 Correct 1061 ms 46432 KB Output is correct
83 Correct 407 ms 45540 KB Output is correct
84 Correct 771 ms 46288 KB Output is correct
85 Correct 847 ms 46588 KB Output is correct
86 Correct 250 ms 44328 KB Output is correct
87 Correct 380 ms 47420 KB Output is correct
88 Correct 358 ms 44376 KB Output is correct
89 Correct 545 ms 46040 KB Output is correct
90 Correct 487 ms 46316 KB Output is correct
91 Correct 396 ms 45696 KB Output is correct
92 Correct 990 ms 46780 KB Output is correct
93 Correct 833 ms 46740 KB Output is correct
94 Correct 258 ms 44384 KB Output is correct
95 Correct 688 ms 46592 KB Output is correct
96 Correct 669 ms 46516 KB Output is correct
97 Correct 682 ms 46436 KB Output is correct
98 Correct 688 ms 46400 KB Output is correct
99 Correct 748 ms 46484 KB Output is correct
100 Correct 698 ms 46392 KB Output is correct