Submission #1102181

# Submission time Handle Problem Language Result Execution time Memory
1102181 2024-10-17T15:42:32 Z trandangquang Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
598 ms 48044 KB
#include <bits/stdc++.h>

using namespace std;

#define task "test"
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define bit(s, i) (((s) >> (i)) & 1)
#define ii pair <int, int>
#define fi first
#define se second
#define ll long long
#define eb emplace_back
#define pb push_back
#define __builtin_popcount __builtin_popcountll

void solve();

int32_t main() {
    if(fopen(task".inp", "r")) {
		freopen(task".inp", "r", stdin);
		freopen(task".out", "w", stdout);
	}
	cin.tie(0)->sync_with_stdio(0);

    solve();
}

const int LG = 20;

int l, q, dp1[1<<LG], dp2[1<<LG], cnt[1<<LG], cntbit[1<<LG];
string s;

void solve() {
    cin >> l >> q >> s;
    FOR(i, 0, (1<<l)-1) {
        dp1[i] += s[i]-'0';
        dp2[i] += s[i]-'0';
        cnt[i] += s[i]-'0';
    }

    FOR(mask, 0, (1<<l)-1) cntbit[mask] = __builtin_popcount(mask);
    FOR(i, 0, l-1) {
        FOR(mask, 0, (1<<l)-1) {
            if(bit(mask, i)) {
                dp1[mask] += dp1[mask^(1<<i)];
            }
            else {
                dp2[mask] += dp2[mask^(1<<i)];
            }
        }
    }

    FOR(i, 1, q) {
        string t; cin >> t;
        reverse(all(t));

        int qm = 0, one = 0, zero = 0;
        FOR(j, 0, l-1) {
            if(t[j] == '?') qm |= (1<<j);
            else if(t[j] == '1') one |= (1<<j);
            else zero |= (1<<j);
        }

        if(cntbit[qm] <= l/3){
            int ans = 0;
            for(int mask = qm;; mask = (mask-1)&qm) {
                ans += cnt[mask | one];
                if(mask == 0) break;
            }
            cout << ans << '\n';
        }
        else if(cntbit[one] <= l/3) {
            int ans = 0;
            for(int mask = one;; mask = (mask-1)&one) {
                if((cntbit[one] - cntbit[mask]) & 1) ans = ans - dp1[mask | qm];
                else ans = ans + dp1[mask | qm];
                if(mask == 0) break;
            }
            cout << ans << '\n';
        }
        else {
            int ans = 0;
            for(int mask = zero;; mask = (mask-1)&zero) {
                if(cntbit[mask] & 1) ans = ans - dp2[one | mask];
                else ans = ans + dp2[one | mask];
                if(mask == 0) break;
            }
            cout << ans << '\n';
        }
    }
}

Compilation message

snake_escaping.cpp: In function 'int32_t main()':
snake_escaping.cpp:23:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |   freopen(task".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:24:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |   freopen(task".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 340 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 340 KB Output is correct
7 Correct 1 ms 504 KB Output is correct
8 Correct 1 ms 472 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 340 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 340 KB Output is correct
7 Correct 1 ms 504 KB Output is correct
8 Correct 1 ms 472 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 140 ms 15180 KB Output is correct
12 Correct 145 ms 14924 KB Output is correct
13 Correct 166 ms 13960 KB Output is correct
14 Correct 197 ms 14156 KB Output is correct
15 Correct 159 ms 15180 KB Output is correct
16 Correct 200 ms 14412 KB Output is correct
17 Correct 181 ms 14364 KB Output is correct
18 Correct 116 ms 16280 KB Output is correct
19 Correct 147 ms 13132 KB Output is correct
20 Correct 149 ms 14924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 340 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 340 KB Output is correct
7 Correct 1 ms 504 KB Output is correct
8 Correct 1 ms 472 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 140 ms 15180 KB Output is correct
12 Correct 145 ms 14924 KB Output is correct
13 Correct 166 ms 13960 KB Output is correct
14 Correct 197 ms 14156 KB Output is correct
15 Correct 159 ms 15180 KB Output is correct
16 Correct 200 ms 14412 KB Output is correct
17 Correct 181 ms 14364 KB Output is correct
18 Correct 116 ms 16280 KB Output is correct
19 Correct 147 ms 13132 KB Output is correct
20 Correct 149 ms 14924 KB Output is correct
21 Correct 158 ms 18252 KB Output is correct
22 Correct 176 ms 18508 KB Output is correct
23 Correct 224 ms 17524 KB Output is correct
24 Correct 199 ms 17152 KB Output is correct
25 Correct 205 ms 19436 KB Output is correct
26 Correct 202 ms 17748 KB Output is correct
27 Correct 208 ms 17740 KB Output is correct
28 Correct 118 ms 20252 KB Output is correct
29 Correct 167 ms 16124 KB Output is correct
30 Correct 191 ms 18432 KB Output is correct
31 Correct 207 ms 18252 KB Output is correct
32 Correct 219 ms 18360 KB Output is correct
33 Correct 196 ms 17108 KB Output is correct
34 Correct 209 ms 17228 KB Output is correct
35 Correct 240 ms 17740 KB Output is correct
36 Correct 117 ms 16344 KB Output is correct
37 Correct 182 ms 18252 KB Output is correct
38 Correct 200 ms 16204 KB Output is correct
39 Correct 202 ms 17428 KB Output is correct
40 Correct 205 ms 17292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 340 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 340 KB Output is correct
7 Correct 1 ms 504 KB Output is correct
8 Correct 1 ms 472 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 50 ms 21196 KB Output is correct
12 Correct 56 ms 21336 KB Output is correct
13 Correct 52 ms 21044 KB Output is correct
14 Correct 63 ms 21076 KB Output is correct
15 Correct 56 ms 21328 KB Output is correct
16 Correct 72 ms 21072 KB Output is correct
17 Correct 65 ms 21232 KB Output is correct
18 Correct 46 ms 21332 KB Output is correct
19 Correct 56 ms 20968 KB Output is correct
20 Correct 76 ms 21256 KB Output is correct
21 Correct 62 ms 21296 KB Output is correct
22 Correct 84 ms 21124 KB Output is correct
23 Correct 53 ms 21268 KB Output is correct
24 Correct 67 ms 21228 KB Output is correct
25 Correct 72 ms 21240 KB Output is correct
26 Correct 43 ms 20976 KB Output is correct
27 Correct 65 ms 21228 KB Output is correct
28 Correct 58 ms 20972 KB Output is correct
29 Correct 54 ms 21228 KB Output is correct
30 Correct 63 ms 21228 KB Output is correct
31 Correct 48 ms 21076 KB Output is correct
32 Correct 66 ms 21232 KB Output is correct
33 Correct 62 ms 21292 KB Output is correct
34 Correct 46 ms 21040 KB Output is correct
35 Correct 56 ms 21228 KB Output is correct
36 Correct 56 ms 21232 KB Output is correct
37 Correct 77 ms 21280 KB Output is correct
38 Correct 63 ms 21300 KB Output is correct
39 Correct 68 ms 21228 KB Output is correct
40 Correct 52 ms 21232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 340 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 340 KB Output is correct
7 Correct 1 ms 504 KB Output is correct
8 Correct 1 ms 472 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 140 ms 15180 KB Output is correct
12 Correct 145 ms 14924 KB Output is correct
13 Correct 166 ms 13960 KB Output is correct
14 Correct 197 ms 14156 KB Output is correct
15 Correct 159 ms 15180 KB Output is correct
16 Correct 200 ms 14412 KB Output is correct
17 Correct 181 ms 14364 KB Output is correct
18 Correct 116 ms 16280 KB Output is correct
19 Correct 147 ms 13132 KB Output is correct
20 Correct 149 ms 14924 KB Output is correct
21 Correct 158 ms 18252 KB Output is correct
22 Correct 176 ms 18508 KB Output is correct
23 Correct 224 ms 17524 KB Output is correct
24 Correct 199 ms 17152 KB Output is correct
25 Correct 205 ms 19436 KB Output is correct
26 Correct 202 ms 17748 KB Output is correct
27 Correct 208 ms 17740 KB Output is correct
28 Correct 118 ms 20252 KB Output is correct
29 Correct 167 ms 16124 KB Output is correct
30 Correct 191 ms 18432 KB Output is correct
31 Correct 207 ms 18252 KB Output is correct
32 Correct 219 ms 18360 KB Output is correct
33 Correct 196 ms 17108 KB Output is correct
34 Correct 209 ms 17228 KB Output is correct
35 Correct 240 ms 17740 KB Output is correct
36 Correct 117 ms 16344 KB Output is correct
37 Correct 182 ms 18252 KB Output is correct
38 Correct 200 ms 16204 KB Output is correct
39 Correct 202 ms 17428 KB Output is correct
40 Correct 205 ms 17292 KB Output is correct
41 Correct 50 ms 21196 KB Output is correct
42 Correct 56 ms 21336 KB Output is correct
43 Correct 52 ms 21044 KB Output is correct
44 Correct 63 ms 21076 KB Output is correct
45 Correct 56 ms 21328 KB Output is correct
46 Correct 72 ms 21072 KB Output is correct
47 Correct 65 ms 21232 KB Output is correct
48 Correct 46 ms 21332 KB Output is correct
49 Correct 56 ms 20968 KB Output is correct
50 Correct 76 ms 21256 KB Output is correct
51 Correct 62 ms 21296 KB Output is correct
52 Correct 84 ms 21124 KB Output is correct
53 Correct 53 ms 21268 KB Output is correct
54 Correct 67 ms 21228 KB Output is correct
55 Correct 72 ms 21240 KB Output is correct
56 Correct 43 ms 20976 KB Output is correct
57 Correct 65 ms 21228 KB Output is correct
58 Correct 58 ms 20972 KB Output is correct
59 Correct 54 ms 21228 KB Output is correct
60 Correct 63 ms 21228 KB Output is correct
61 Correct 48 ms 21076 KB Output is correct
62 Correct 66 ms 21232 KB Output is correct
63 Correct 62 ms 21292 KB Output is correct
64 Correct 46 ms 21040 KB Output is correct
65 Correct 56 ms 21228 KB Output is correct
66 Correct 56 ms 21232 KB Output is correct
67 Correct 77 ms 21280 KB Output is correct
68 Correct 63 ms 21300 KB Output is correct
69 Correct 68 ms 21228 KB Output is correct
70 Correct 52 ms 21232 KB Output is correct
71 Correct 321 ms 45512 KB Output is correct
72 Correct 336 ms 45564 KB Output is correct
73 Correct 405 ms 44224 KB Output is correct
74 Correct 521 ms 44448 KB Output is correct
75 Correct 355 ms 46636 KB Output is correct
76 Correct 506 ms 44876 KB Output is correct
77 Correct 507 ms 44600 KB Output is correct
78 Correct 233 ms 48044 KB Output is correct
79 Correct 331 ms 42388 KB Output is correct
80 Correct 407 ms 44636 KB Output is correct
81 Correct 540 ms 44376 KB Output is correct
82 Correct 533 ms 44620 KB Output is correct
83 Correct 310 ms 43756 KB Output is correct
84 Correct 446 ms 44592 KB Output is correct
85 Correct 555 ms 44664 KB Output is correct
86 Correct 186 ms 42584 KB Output is correct
87 Correct 384 ms 45572 KB Output is correct
88 Correct 290 ms 42500 KB Output is correct
89 Correct 438 ms 44264 KB Output is correct
90 Correct 431 ms 44464 KB Output is correct
91 Correct 316 ms 43724 KB Output is correct
92 Correct 598 ms 44884 KB Output is correct
93 Correct 578 ms 44628 KB Output is correct
94 Correct 202 ms 42416 KB Output is correct
95 Correct 533 ms 43756 KB Output is correct
96 Correct 443 ms 44524 KB Output is correct
97 Correct 438 ms 43500 KB Output is correct
98 Correct 439 ms 43500 KB Output is correct
99 Correct 448 ms 43528 KB Output is correct
100 Correct 424 ms 43652 KB Output is correct