Submission #445839

# Submission time Handle Problem Language Result Execution time Memory
445839 2021-07-19T20:28:48 Z JovanB Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1774 ms 24640 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;

const int N = 20;
int str[1<<N];
int sub[1<<N];
int over[1<<N];

int main(){
    ios_base::sync_with_stdio(false), cin.tie(0);
    cout.precision(10);
    cout << fixed;

    int n, qrs;
    cin >> n >> qrs;
    string td;
    cin >> td;
    for(int i=0; i<(1<<n); i++){
        str[i] = td[i] - '0';
        sub[i] = over[i] = str[i];
    }
    for(int i=0; i<n; i++){
        for(int mask=0; mask<(1<<n); mask++){
            if(mask & (1 << i)) sub[mask] += sub[mask ^ (1 << i)];
        }
    }
    for(int i=0; i<n; i++){
        for(int mask=0; mask<(1<<n); mask++){
            if(!(mask & (1 << i))) over[mask] += over[mask ^ (1 << i)];
        }
    }
    while(qrs--){
        string s;
        cin >> s;
        reverse(s.begin(), s.end());
        int cp = 0, c1 = 0, c0 = 0;
        for(auto c : s){
            if(c == '?') cp++;
            else if(c == '0') c0++;
            else c1++;
        }
        int mn = min(min(c0, c1), cp);
        if(cp == mn){
            vector <int> pos;
            int p = 0;
            int res = 0;
            for(int i=0; i<n; i++){
                char c = s[i];
                if(c == '?') pos.push_back(1 << i);
                else if(c == '1') p += (1 << i);
            }
            if(cp == 0){
                cout << str[p] << "\n";
                continue;
            }
            for(int mask=0; mask<(1<<pos.size()); mask++){
                int g = p;
                int k = mask;
                while(k){
                    int x = k & -k;
                    int v = __builtin_ctz(x);
                    g += pos[v];
                    k -= x;
                }
                res += str[g];
            }
            cout << res << "\n";
        }
        else if(c0 == mn){
            vector <int> pos;
            int p = 0;
            for(int i=0; i<n; i++){
                char c = s[i];
                if(c == '0') pos.push_back(1 << i);
                else if(c == '1') p += (1 << i);
            }
            if(c0 == 0){
                cout << over[p] << "\n";
                continue;
            }
            int res = 0;
            for(int mask=0; mask<(1<<pos.size()); mask++){
                int g = p, pr = 1;
                int k = mask;
                while(k){
                    int x = k & -k;
                    int v = __builtin_ctz(x);
                    pr = -pr;
                    g += pos[v];
                    k -= x;
                }
                res += pr*over[g];
            }
            cout << res << "\n";
        }
        else{
            vector <int> pos;
            int p = 0;
            for(int i=0; i<n; i++){
                char c = s[i];
                if(c != '0') p += (1 << i);
                if(c == '1') pos.push_back(1 << i);
            }
            if(c1 == 0){
                cout << sub[p] << "\n";
                continue;
            }
            int res = 0;
            for(int mask=0; mask<(1<<pos.size()); mask++){
                int g = p, pr = 1;
                int k = mask;
                while(k){
                    int x = k & -k;
                    int v = __builtin_ctz(x);
                    pr = -pr;
                    g -= pos[v];
                    k -= x;
                }
                res += pr*sub[g];
            }
            cout << res << "\n";
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 277 ms 15156 KB Output is correct
12 Correct 312 ms 14756 KB Output is correct
13 Correct 389 ms 13996 KB Output is correct
14 Correct 445 ms 14032 KB Output is correct
15 Correct 365 ms 15056 KB Output is correct
16 Correct 435 ms 14256 KB Output is correct
17 Correct 424 ms 14280 KB Output is correct
18 Correct 229 ms 15992 KB Output is correct
19 Correct 264 ms 13032 KB Output is correct
20 Correct 276 ms 14664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 277 ms 15156 KB Output is correct
12 Correct 312 ms 14756 KB Output is correct
13 Correct 389 ms 13996 KB Output is correct
14 Correct 445 ms 14032 KB Output is correct
15 Correct 365 ms 15056 KB Output is correct
16 Correct 435 ms 14256 KB Output is correct
17 Correct 424 ms 14280 KB Output is correct
18 Correct 229 ms 15992 KB Output is correct
19 Correct 264 ms 13032 KB Output is correct
20 Correct 276 ms 14664 KB Output is correct
21 Correct 296 ms 18120 KB Output is correct
22 Correct 362 ms 18240 KB Output is correct
23 Correct 512 ms 17220 KB Output is correct
24 Correct 545 ms 17224 KB Output is correct
25 Correct 439 ms 19016 KB Output is correct
26 Correct 560 ms 17640 KB Output is correct
27 Correct 526 ms 17516 KB Output is correct
28 Correct 242 ms 20124 KB Output is correct
29 Correct 292 ms 16068 KB Output is correct
30 Correct 311 ms 18312 KB Output is correct
31 Correct 507 ms 18224 KB Output is correct
32 Correct 560 ms 18264 KB Output is correct
33 Correct 389 ms 16940 KB Output is correct
34 Correct 550 ms 17068 KB Output is correct
35 Correct 530 ms 17484 KB Output is correct
36 Correct 240 ms 16068 KB Output is correct
37 Correct 329 ms 18012 KB Output is correct
38 Correct 311 ms 16092 KB Output is correct
39 Correct 473 ms 17392 KB Output is correct
40 Correct 553 ms 17088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 89 ms 15964 KB Output is correct
12 Correct 90 ms 15972 KB Output is correct
13 Correct 113 ms 15928 KB Output is correct
14 Correct 132 ms 15932 KB Output is correct
15 Correct 96 ms 16052 KB Output is correct
16 Correct 145 ms 15972 KB Output is correct
17 Correct 139 ms 15932 KB Output is correct
18 Correct 77 ms 16192 KB Output is correct
19 Correct 85 ms 15812 KB Output is correct
20 Correct 84 ms 16060 KB Output is correct
21 Correct 106 ms 15984 KB Output is correct
22 Correct 131 ms 15972 KB Output is correct
23 Correct 93 ms 15928 KB Output is correct
24 Correct 137 ms 15940 KB Output is correct
25 Correct 133 ms 16012 KB Output is correct
26 Correct 78 ms 15796 KB Output is correct
27 Correct 92 ms 16052 KB Output is correct
28 Correct 88 ms 15844 KB Output is correct
29 Correct 105 ms 15932 KB Output is correct
30 Correct 125 ms 15924 KB Output is correct
31 Correct 95 ms 15972 KB Output is correct
32 Correct 153 ms 16028 KB Output is correct
33 Correct 132 ms 15936 KB Output is correct
34 Correct 78 ms 15892 KB Output is correct
35 Correct 120 ms 15996 KB Output is correct
36 Correct 118 ms 15936 KB Output is correct
37 Correct 118 ms 15996 KB Output is correct
38 Correct 118 ms 15940 KB Output is correct
39 Correct 120 ms 15940 KB Output is correct
40 Correct 116 ms 15976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 277 ms 15156 KB Output is correct
12 Correct 312 ms 14756 KB Output is correct
13 Correct 389 ms 13996 KB Output is correct
14 Correct 445 ms 14032 KB Output is correct
15 Correct 365 ms 15056 KB Output is correct
16 Correct 435 ms 14256 KB Output is correct
17 Correct 424 ms 14280 KB Output is correct
18 Correct 229 ms 15992 KB Output is correct
19 Correct 264 ms 13032 KB Output is correct
20 Correct 276 ms 14664 KB Output is correct
21 Correct 296 ms 18120 KB Output is correct
22 Correct 362 ms 18240 KB Output is correct
23 Correct 512 ms 17220 KB Output is correct
24 Correct 545 ms 17224 KB Output is correct
25 Correct 439 ms 19016 KB Output is correct
26 Correct 560 ms 17640 KB Output is correct
27 Correct 526 ms 17516 KB Output is correct
28 Correct 242 ms 20124 KB Output is correct
29 Correct 292 ms 16068 KB Output is correct
30 Correct 311 ms 18312 KB Output is correct
31 Correct 507 ms 18224 KB Output is correct
32 Correct 560 ms 18264 KB Output is correct
33 Correct 389 ms 16940 KB Output is correct
34 Correct 550 ms 17068 KB Output is correct
35 Correct 530 ms 17484 KB Output is correct
36 Correct 240 ms 16068 KB Output is correct
37 Correct 329 ms 18012 KB Output is correct
38 Correct 311 ms 16092 KB Output is correct
39 Correct 473 ms 17392 KB Output is correct
40 Correct 553 ms 17088 KB Output is correct
41 Correct 89 ms 15964 KB Output is correct
42 Correct 90 ms 15972 KB Output is correct
43 Correct 113 ms 15928 KB Output is correct
44 Correct 132 ms 15932 KB Output is correct
45 Correct 96 ms 16052 KB Output is correct
46 Correct 145 ms 15972 KB Output is correct
47 Correct 139 ms 15932 KB Output is correct
48 Correct 77 ms 16192 KB Output is correct
49 Correct 85 ms 15812 KB Output is correct
50 Correct 84 ms 16060 KB Output is correct
51 Correct 106 ms 15984 KB Output is correct
52 Correct 131 ms 15972 KB Output is correct
53 Correct 93 ms 15928 KB Output is correct
54 Correct 137 ms 15940 KB Output is correct
55 Correct 133 ms 16012 KB Output is correct
56 Correct 78 ms 15796 KB Output is correct
57 Correct 92 ms 16052 KB Output is correct
58 Correct 88 ms 15844 KB Output is correct
59 Correct 105 ms 15932 KB Output is correct
60 Correct 125 ms 15924 KB Output is correct
61 Correct 95 ms 15972 KB Output is correct
62 Correct 153 ms 16028 KB Output is correct
63 Correct 132 ms 15936 KB Output is correct
64 Correct 78 ms 15892 KB Output is correct
65 Correct 120 ms 15996 KB Output is correct
66 Correct 118 ms 15936 KB Output is correct
67 Correct 118 ms 15996 KB Output is correct
68 Correct 118 ms 15940 KB Output is correct
69 Correct 120 ms 15940 KB Output is correct
70 Correct 116 ms 15976 KB Output is correct
71 Correct 487 ms 21616 KB Output is correct
72 Correct 620 ms 21792 KB Output is correct
73 Correct 943 ms 20372 KB Output is correct
74 Correct 1386 ms 20664 KB Output is correct
75 Correct 698 ms 22648 KB Output is correct
76 Correct 1645 ms 20968 KB Output is correct
77 Correct 1450 ms 20880 KB Output is correct
78 Correct 375 ms 24640 KB Output is correct
79 Correct 509 ms 18640 KB Output is correct
80 Correct 497 ms 21740 KB Output is correct
81 Correct 965 ms 21596 KB Output is correct
82 Correct 1320 ms 20600 KB Output is correct
83 Correct 664 ms 19700 KB Output is correct
84 Correct 1673 ms 20664 KB Output is correct
85 Correct 1507 ms 20740 KB Output is correct
86 Correct 367 ms 18544 KB Output is correct
87 Correct 599 ms 21540 KB Output is correct
88 Correct 526 ms 18588 KB Output is correct
89 Correct 910 ms 20280 KB Output is correct
90 Correct 1294 ms 20448 KB Output is correct
91 Correct 713 ms 19600 KB Output is correct
92 Correct 1774 ms 20808 KB Output is correct
93 Correct 1460 ms 20804 KB Output is correct
94 Correct 379 ms 18336 KB Output is correct
95 Correct 1163 ms 20496 KB Output is correct
96 Correct 1180 ms 20504 KB Output is correct
97 Correct 1171 ms 20388 KB Output is correct
98 Correct 1159 ms 20088 KB Output is correct
99 Correct 1166 ms 19824 KB Output is correct
100 Correct 1178 ms 19612 KB Output is correct