Submission #1048173

# Submission time Handle Problem Language Result Execution time Memory
1048173 2024-08-08T03:48:46 Z khanhtb Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
403 ms 46416 KB
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define pf push_front
#define vi vector<ll>
#define vii vector<vi>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define all(a) a.begin(), a.end()
#define fi first
#define se second
using namespace std;
const int mod = 998244353; 
const ll inf = 2e18;
const int B = 320;
const int N = 2e6+8;
int n,q,a[N];
int sub[N],sup[N];
void solve(){
    cin >> n >> q;
    for(int i = 0; i < 1<<n; i++){
        char c;cin >> c;
        a[i] = c-'0';
        sub[i] = sup[i] = a[i];
    }
    for(int i = 0; i < n; i++){
        for(int mask = 0; mask < 1<<n; mask++){
            if(mask>>i&1) sub[mask] += sub[mask^(1<<i)];
            else sup[mask] += sup[mask^(1<<i)];
        }
    }
    while(q--){
        string t;cin >> t;
        int mask[3] = {0};
        int cnt[3] = {0};
        reverse(all(t));
        for(int i = 0; i < t.size(); i++){
            int type;
            if(t[i] == '0') type = 0;
            else if(t[i] == '1') type = 1;
            else type = 2;
            cnt[type]++;
            mask[type] |= (1<<i);
        }
        int ans = 0;
        if(cnt[2] <= 7){
            for(int m = mask[2]; ; m = (m-1)&mask[2]){
                ans += a[mask[1]|m];
                if(m == 0) break;
            }
            cout << ans << '\n';
        }
        else if(cnt[1] <= 7){
            for(int m = mask[1]; ; m = (m-1)&mask[1]){
                if(__builtin_popcount(m)&1) ans -= sub[(m ^ mask[1]) | mask[2]];
                else ans += sub[(m ^ mask[1]) | mask[2]];
                if(m == 0) break;
            }
            cout << ans << '\n';
        }
        else{
            for(int m = mask[0]; ; m = (m-1)&mask[0]){
                if(__builtin_popcount(m)&1) ans -= sup[m | mask[1]];
                else ans += sup[m | mask[1]];
                if(m == 0) break;
            }
            cout << ans << '\n';
        }
    }
}
signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    if (fopen("test.inp", "r")) {
        freopen("test.inp", "r", stdin);
        freopen("test.out", "w", stdout);
    }   
    ll T = 1;
    // cin >> T;
    for (ll i = 1; i <= T; i++) {
        solve();
        cout << '\n';
    }
}

Compilation message

snake_escaping.cpp: In function 'void solve()':
snake_escaping.cpp:40:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |         for(int i = 0; i < t.size(); i++){
      |                        ~~^~~~~~~~~~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:79:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |         freopen("test.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:80:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |         freopen("test.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 0 ms 4444 KB Output is correct
4 Correct 0 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 0 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 0 ms 4444 KB Output is correct
4 Correct 0 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 0 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 116 ms 8472 KB Output is correct
12 Correct 156 ms 8012 KB Output is correct
13 Correct 124 ms 7252 KB Output is correct
14 Correct 115 ms 7504 KB Output is correct
15 Correct 155 ms 8532 KB Output is correct
16 Correct 127 ms 7508 KB Output is correct
17 Correct 136 ms 7700 KB Output is correct
18 Correct 98 ms 9376 KB Output is correct
19 Correct 106 ms 6524 KB Output is correct
20 Correct 147 ms 8020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 0 ms 4444 KB Output is correct
4 Correct 0 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 0 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 116 ms 8472 KB Output is correct
12 Correct 156 ms 8012 KB Output is correct
13 Correct 124 ms 7252 KB Output is correct
14 Correct 115 ms 7504 KB Output is correct
15 Correct 155 ms 8532 KB Output is correct
16 Correct 127 ms 7508 KB Output is correct
17 Correct 136 ms 7700 KB Output is correct
18 Correct 98 ms 9376 KB Output is correct
19 Correct 106 ms 6524 KB Output is correct
20 Correct 147 ms 8020 KB Output is correct
21 Correct 199 ms 8496 KB Output is correct
22 Correct 174 ms 8524 KB Output is correct
23 Correct 144 ms 7764 KB Output is correct
24 Correct 135 ms 7504 KB Output is correct
25 Correct 133 ms 9552 KB Output is correct
26 Correct 151 ms 8048 KB Output is correct
27 Correct 156 ms 8020 KB Output is correct
28 Correct 111 ms 10604 KB Output is correct
29 Correct 125 ms 6428 KB Output is correct
30 Correct 158 ms 8532 KB Output is correct
31 Correct 172 ms 8576 KB Output is correct
32 Correct 147 ms 8532 KB Output is correct
33 Correct 137 ms 7252 KB Output is correct
34 Correct 136 ms 7508 KB Output is correct
35 Correct 163 ms 8016 KB Output is correct
36 Correct 122 ms 6588 KB Output is correct
37 Correct 166 ms 8476 KB Output is correct
38 Correct 129 ms 6484 KB Output is correct
39 Correct 134 ms 7764 KB Output is correct
40 Correct 129 ms 7512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 0 ms 4444 KB Output is correct
4 Correct 0 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 0 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 24 ms 17244 KB Output is correct
12 Correct 30 ms 19204 KB Output is correct
13 Correct 30 ms 19028 KB Output is correct
14 Correct 41 ms 19280 KB Output is correct
15 Correct 30 ms 19280 KB Output is correct
16 Correct 39 ms 19288 KB Output is correct
17 Correct 32 ms 19284 KB Output is correct
18 Correct 26 ms 19292 KB Output is correct
19 Correct 27 ms 19192 KB Output is correct
20 Correct 28 ms 19284 KB Output is correct
21 Correct 33 ms 19340 KB Output is correct
22 Correct 36 ms 19280 KB Output is correct
23 Correct 32 ms 19036 KB Output is correct
24 Correct 31 ms 19288 KB Output is correct
25 Correct 38 ms 19284 KB Output is correct
26 Correct 25 ms 19292 KB Output is correct
27 Correct 29 ms 19280 KB Output is correct
28 Correct 27 ms 19028 KB Output is correct
29 Correct 29 ms 19024 KB Output is correct
30 Correct 31 ms 19280 KB Output is correct
31 Correct 28 ms 19024 KB Output is correct
32 Correct 35 ms 19280 KB Output is correct
33 Correct 37 ms 19284 KB Output is correct
34 Correct 24 ms 19084 KB Output is correct
35 Correct 31 ms 19280 KB Output is correct
36 Correct 33 ms 19160 KB Output is correct
37 Correct 31 ms 19236 KB Output is correct
38 Correct 33 ms 19192 KB Output is correct
39 Correct 30 ms 19280 KB Output is correct
40 Correct 33 ms 19284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 0 ms 4444 KB Output is correct
4 Correct 0 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 0 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 116 ms 8472 KB Output is correct
12 Correct 156 ms 8012 KB Output is correct
13 Correct 124 ms 7252 KB Output is correct
14 Correct 115 ms 7504 KB Output is correct
15 Correct 155 ms 8532 KB Output is correct
16 Correct 127 ms 7508 KB Output is correct
17 Correct 136 ms 7700 KB Output is correct
18 Correct 98 ms 9376 KB Output is correct
19 Correct 106 ms 6524 KB Output is correct
20 Correct 147 ms 8020 KB Output is correct
21 Correct 199 ms 8496 KB Output is correct
22 Correct 174 ms 8524 KB Output is correct
23 Correct 144 ms 7764 KB Output is correct
24 Correct 135 ms 7504 KB Output is correct
25 Correct 133 ms 9552 KB Output is correct
26 Correct 151 ms 8048 KB Output is correct
27 Correct 156 ms 8020 KB Output is correct
28 Correct 111 ms 10604 KB Output is correct
29 Correct 125 ms 6428 KB Output is correct
30 Correct 158 ms 8532 KB Output is correct
31 Correct 172 ms 8576 KB Output is correct
32 Correct 147 ms 8532 KB Output is correct
33 Correct 137 ms 7252 KB Output is correct
34 Correct 136 ms 7508 KB Output is correct
35 Correct 163 ms 8016 KB Output is correct
36 Correct 122 ms 6588 KB Output is correct
37 Correct 166 ms 8476 KB Output is correct
38 Correct 129 ms 6484 KB Output is correct
39 Correct 134 ms 7764 KB Output is correct
40 Correct 129 ms 7512 KB Output is correct
41 Correct 24 ms 17244 KB Output is correct
42 Correct 30 ms 19204 KB Output is correct
43 Correct 30 ms 19028 KB Output is correct
44 Correct 41 ms 19280 KB Output is correct
45 Correct 30 ms 19280 KB Output is correct
46 Correct 39 ms 19288 KB Output is correct
47 Correct 32 ms 19284 KB Output is correct
48 Correct 26 ms 19292 KB Output is correct
49 Correct 27 ms 19192 KB Output is correct
50 Correct 28 ms 19284 KB Output is correct
51 Correct 33 ms 19340 KB Output is correct
52 Correct 36 ms 19280 KB Output is correct
53 Correct 32 ms 19036 KB Output is correct
54 Correct 31 ms 19288 KB Output is correct
55 Correct 38 ms 19284 KB Output is correct
56 Correct 25 ms 19292 KB Output is correct
57 Correct 29 ms 19280 KB Output is correct
58 Correct 27 ms 19028 KB Output is correct
59 Correct 29 ms 19024 KB Output is correct
60 Correct 31 ms 19280 KB Output is correct
61 Correct 28 ms 19024 KB Output is correct
62 Correct 35 ms 19280 KB Output is correct
63 Correct 37 ms 19284 KB Output is correct
64 Correct 24 ms 19084 KB Output is correct
65 Correct 31 ms 19280 KB Output is correct
66 Correct 33 ms 19160 KB Output is correct
67 Correct 31 ms 19236 KB Output is correct
68 Correct 33 ms 19192 KB Output is correct
69 Correct 30 ms 19280 KB Output is correct
70 Correct 33 ms 19284 KB Output is correct
71 Correct 156 ms 43604 KB Output is correct
72 Correct 235 ms 43600 KB Output is correct
73 Correct 276 ms 42244 KB Output is correct
74 Correct 377 ms 42624 KB Output is correct
75 Correct 240 ms 44528 KB Output is correct
76 Correct 344 ms 42876 KB Output is correct
77 Correct 328 ms 42832 KB Output is correct
78 Correct 174 ms 46416 KB Output is correct
79 Correct 209 ms 40532 KB Output is correct
80 Correct 219 ms 43604 KB Output is correct
81 Correct 333 ms 43604 KB Output is correct
82 Correct 403 ms 42576 KB Output is correct
83 Correct 202 ms 41804 KB Output is correct
84 Correct 305 ms 42636 KB Output is correct
85 Correct 316 ms 42720 KB Output is correct
86 Correct 151 ms 40784 KB Output is correct
87 Correct 218 ms 43604 KB Output is correct
88 Correct 207 ms 40572 KB Output is correct
89 Correct 250 ms 42068 KB Output is correct
90 Correct 288 ms 42576 KB Output is correct
91 Correct 219 ms 41812 KB Output is correct
92 Correct 363 ms 42900 KB Output is correct
93 Correct 316 ms 42832 KB Output is correct
94 Correct 175 ms 40528 KB Output is correct
95 Correct 272 ms 42736 KB Output is correct
96 Correct 263 ms 42604 KB Output is correct
97 Correct 283 ms 42640 KB Output is correct
98 Correct 271 ms 42804 KB Output is correct
99 Correct 267 ms 42488 KB Output is correct
100 Correct 262 ms 42636 KB Output is correct