Submission #1034413

# Submission time Handle Problem Language Result Execution time Memory
1034413 2024-07-25T13:30:25 Z Shayan86 Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
588 ms 43508 KB
#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

#define Mp          make_pair
#define sep         ' '
#define endl        '\n'
#define F	        first
#define S	        second
#define pb          push_back
#define SZ(x)       (int)x.size()
#define all(x)      (x).begin(),(x).end()
#define kill(res)	cout << res << '\n', exit(0);
#define set_dec(x)	cout << fixed << setprecision(x);
#define fast_io     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io     freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout);

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll N = 22;
const ll Mod = 1e9 + 7;

int n, q, cnt[(1 << N)], sos[2][(1 << N)];

string s;

int main(){
    fast_io;
    
    cin >> n >> q >> s;

    for(int i = 0; i < (1 << n); i++){
        cnt[i] = s[i] - '0';
        sos[0][i] = sos[1][i] = cnt[i];
    }

    for(int i = 0; i < n; i++) for(int mask = (1 << n)-1; mask >= 0; mask--) if(mask >> i & 1) sos[0][mask] += sos[0][mask^(1 << i)];
    for(int i = 0; i < n; i++) for(int mask = 0; mask < (1 << n); mask++) if((mask >> i & 1) == 0) sos[1][mask] += sos[1][mask^(1 << i)];

    while(q--){
        string t; cin >> t;
        reverse(all(t));

        int c[3] = {0, 0, 0};
        for(int i = 0; i < n; i++){
            if(t[i] == '?') c[2]++;
            else c[t[i]-'0']++;
        }

        int mask[3] = {0, 0, 0}, num[2];
        for(int i = 0; i < n; i++){
            if(t[i] == '?') mask[2] += (1 << i);
            else mask[t[i]-'0'] += (1 << i);
        }
        num[0] = mask[1];
        num[1] = mask[1] | mask[2];

        int res = 0;
        if(c[0] <= 6){
            res = sos[1][num[0]];
            for(int i = mask[0]; i > 0; i = (i-1)&mask[0]) res += (__builtin_popcount(i) & 1 ? -1 : 1) * sos[1][num[0]|i];
        }
        else if(c[1] <= 6){
            res = sos[0][num[1]];
            for(int i = mask[1]; i > 0; i = (i-1)&mask[1]) res += (__builtin_popcount(i) & 1 ? -1 : 1) * sos[0][num[1]^i];
        }
        else{
            res = cnt[num[0]];
            for(int i = mask[2]; i > 0; i = (i-1)&mask[2]) res += cnt[num[0]|i];
        }

        cout << res << endl;
    }

}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 161 ms 15044 KB Output is correct
12 Correct 218 ms 14812 KB Output is correct
13 Correct 219 ms 14004 KB Output is correct
14 Correct 181 ms 14112 KB Output is correct
15 Correct 169 ms 15140 KB Output is correct
16 Correct 167 ms 14324 KB Output is correct
17 Correct 221 ms 14196 KB Output is correct
18 Correct 114 ms 16212 KB Output is correct
19 Correct 198 ms 13196 KB Output is correct
20 Correct 159 ms 14756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 161 ms 15044 KB Output is correct
12 Correct 218 ms 14812 KB Output is correct
13 Correct 219 ms 14004 KB Output is correct
14 Correct 181 ms 14112 KB Output is correct
15 Correct 169 ms 15140 KB Output is correct
16 Correct 167 ms 14324 KB Output is correct
17 Correct 221 ms 14196 KB Output is correct
18 Correct 114 ms 16212 KB Output is correct
19 Correct 198 ms 13196 KB Output is correct
20 Correct 159 ms 14756 KB Output is correct
21 Correct 169 ms 18256 KB Output is correct
22 Correct 272 ms 18264 KB Output is correct
23 Correct 312 ms 17432 KB Output is correct
24 Correct 232 ms 17236 KB Output is correct
25 Correct 193 ms 19256 KB Output is correct
26 Correct 228 ms 17632 KB Output is correct
27 Correct 277 ms 17492 KB Output is correct
28 Correct 131 ms 20304 KB Output is correct
29 Correct 294 ms 16208 KB Output is correct
30 Correct 223 ms 18256 KB Output is correct
31 Correct 227 ms 18256 KB Output is correct
32 Correct 205 ms 18308 KB Output is correct
33 Correct 169 ms 17052 KB Output is correct
34 Correct 220 ms 17236 KB Output is correct
35 Correct 252 ms 17748 KB Output is correct
36 Correct 134 ms 16212 KB Output is correct
37 Correct 173 ms 18296 KB Output is correct
38 Correct 263 ms 16072 KB Output is correct
39 Correct 207 ms 17248 KB Output is correct
40 Correct 198 ms 17260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 49 ms 16116 KB Output is correct
12 Correct 43 ms 16052 KB Output is correct
13 Correct 43 ms 16116 KB Output is correct
14 Correct 50 ms 15964 KB Output is correct
15 Correct 42 ms 16244 KB Output is correct
16 Correct 51 ms 15980 KB Output is correct
17 Correct 52 ms 16112 KB Output is correct
18 Correct 32 ms 16372 KB Output is correct
19 Correct 37 ms 15980 KB Output is correct
20 Correct 41 ms 16116 KB Output is correct
21 Correct 45 ms 16052 KB Output is correct
22 Correct 50 ms 16116 KB Output is correct
23 Correct 38 ms 15980 KB Output is correct
24 Correct 47 ms 16112 KB Output is correct
25 Correct 47 ms 16188 KB Output is correct
26 Correct 33 ms 15992 KB Output is correct
27 Correct 40 ms 16240 KB Output is correct
28 Correct 37 ms 15984 KB Output is correct
29 Correct 43 ms 16368 KB Output is correct
30 Correct 43 ms 16120 KB Output is correct
31 Correct 40 ms 16112 KB Output is correct
32 Correct 56 ms 16164 KB Output is correct
33 Correct 48 ms 16124 KB Output is correct
34 Correct 33 ms 15856 KB Output is correct
35 Correct 44 ms 16120 KB Output is correct
36 Correct 44 ms 16112 KB Output is correct
37 Correct 44 ms 16116 KB Output is correct
38 Correct 44 ms 16112 KB Output is correct
39 Correct 43 ms 16120 KB Output is correct
40 Correct 57 ms 16116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 161 ms 15044 KB Output is correct
12 Correct 218 ms 14812 KB Output is correct
13 Correct 219 ms 14004 KB Output is correct
14 Correct 181 ms 14112 KB Output is correct
15 Correct 169 ms 15140 KB Output is correct
16 Correct 167 ms 14324 KB Output is correct
17 Correct 221 ms 14196 KB Output is correct
18 Correct 114 ms 16212 KB Output is correct
19 Correct 198 ms 13196 KB Output is correct
20 Correct 159 ms 14756 KB Output is correct
21 Correct 169 ms 18256 KB Output is correct
22 Correct 272 ms 18264 KB Output is correct
23 Correct 312 ms 17432 KB Output is correct
24 Correct 232 ms 17236 KB Output is correct
25 Correct 193 ms 19256 KB Output is correct
26 Correct 228 ms 17632 KB Output is correct
27 Correct 277 ms 17492 KB Output is correct
28 Correct 131 ms 20304 KB Output is correct
29 Correct 294 ms 16208 KB Output is correct
30 Correct 223 ms 18256 KB Output is correct
31 Correct 227 ms 18256 KB Output is correct
32 Correct 205 ms 18308 KB Output is correct
33 Correct 169 ms 17052 KB Output is correct
34 Correct 220 ms 17236 KB Output is correct
35 Correct 252 ms 17748 KB Output is correct
36 Correct 134 ms 16212 KB Output is correct
37 Correct 173 ms 18296 KB Output is correct
38 Correct 263 ms 16072 KB Output is correct
39 Correct 207 ms 17248 KB Output is correct
40 Correct 198 ms 17260 KB Output is correct
41 Correct 49 ms 16116 KB Output is correct
42 Correct 43 ms 16052 KB Output is correct
43 Correct 43 ms 16116 KB Output is correct
44 Correct 50 ms 15964 KB Output is correct
45 Correct 42 ms 16244 KB Output is correct
46 Correct 51 ms 15980 KB Output is correct
47 Correct 52 ms 16112 KB Output is correct
48 Correct 32 ms 16372 KB Output is correct
49 Correct 37 ms 15980 KB Output is correct
50 Correct 41 ms 16116 KB Output is correct
51 Correct 45 ms 16052 KB Output is correct
52 Correct 50 ms 16116 KB Output is correct
53 Correct 38 ms 15980 KB Output is correct
54 Correct 47 ms 16112 KB Output is correct
55 Correct 47 ms 16188 KB Output is correct
56 Correct 33 ms 15992 KB Output is correct
57 Correct 40 ms 16240 KB Output is correct
58 Correct 37 ms 15984 KB Output is correct
59 Correct 43 ms 16368 KB Output is correct
60 Correct 43 ms 16120 KB Output is correct
61 Correct 40 ms 16112 KB Output is correct
62 Correct 56 ms 16164 KB Output is correct
63 Correct 48 ms 16124 KB Output is correct
64 Correct 33 ms 15856 KB Output is correct
65 Correct 44 ms 16120 KB Output is correct
66 Correct 44 ms 16112 KB Output is correct
67 Correct 44 ms 16116 KB Output is correct
68 Correct 44 ms 16112 KB Output is correct
69 Correct 43 ms 16120 KB Output is correct
70 Correct 57 ms 16116 KB Output is correct
71 Correct 343 ms 40468 KB Output is correct
72 Correct 375 ms 40848 KB Output is correct
73 Correct 411 ms 39032 KB Output is correct
74 Correct 558 ms 39528 KB Output is correct
75 Correct 410 ms 41372 KB Output is correct
76 Correct 559 ms 39836 KB Output is correct
77 Correct 474 ms 39588 KB Output is correct
78 Correct 199 ms 43508 KB Output is correct
79 Correct 208 ms 37360 KB Output is correct
80 Correct 346 ms 40640 KB Output is correct
81 Correct 442 ms 40792 KB Output is correct
82 Correct 588 ms 39668 KB Output is correct
83 Correct 340 ms 38632 KB Output is correct
84 Correct 542 ms 39524 KB Output is correct
85 Correct 474 ms 39532 KB Output is correct
86 Correct 212 ms 37436 KB Output is correct
87 Correct 343 ms 40304 KB Output is correct
88 Correct 231 ms 37616 KB Output is correct
89 Correct 379 ms 39172 KB Output is correct
90 Correct 412 ms 39320 KB Output is correct
91 Correct 310 ms 38648 KB Output is correct
92 Correct 537 ms 39792 KB Output is correct
93 Correct 510 ms 39716 KB Output is correct
94 Correct 208 ms 37484 KB Output is correct
95 Correct 405 ms 39412 KB Output is correct
96 Correct 476 ms 39412 KB Output is correct
97 Correct 419 ms 39400 KB Output is correct
98 Correct 399 ms 39536 KB Output is correct
99 Correct 400 ms 39412 KB Output is correct
100 Correct 406 ms 39416 KB Output is correct