Submission #1039301

# Submission time Handle Problem Language Result Execution time Memory
1039301 2024-07-30T16:39:57 Z phong Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
923 ms 43332 KB
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>

#define ll long long
const int nmax = 4e5 + 5, N = 1e5;
const ll oo = 1e9;
const int lg = 31, M = 2, mod = 1e6;
#define pii pair<ll, ll>
#define fi first
#define se second
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' '; cout << "\n";
#define endl "\n"
#define task "code"
using namespace std;

int L, q;
string s;
int F[1 << 20], G[1 << 20], cnt[1 << 20];
int bit(int msk, int x){
    return msk>> x & 1;
}
main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
//    freopen(task".inp", "r", stdin);
//    freopen(task".out", "w", stdout);
    cin >> L >> q;
    cin >> s;

    for(int i = 0; i < (1 << L) ; ++i){
        int c1 = 0, c2 = 0;
        for(int k = 1; k <= L; ++k){
            if(bit(i, L - k)){
                c1 += (1 << (k - 1));
            }
            else c2 += (1 << (k - 1));
        }
        cnt[c1] += s[i] - '0';
        F[c1] += s[i] - '0';
        G[c2] += s[i] - '0';
    }
    for(int i = 0; i < L; ++i){
        for(int msk = 0; msk < (1 << L); ++msk){
            if(!bit(msk, i)){
                F[msk ^ (1 << i)] += F[msk];
                G[msk ^ (1 << i)] += G[msk];
            }
        }
    }
    string tmp;
    vector<int> c0, c1, c2;
    while(q--){
        cin >> tmp;
        c0.clear(); c1.clear();c2.clear();
        for(int i = 0; i < L; ++i){
            char x = tmp[i];
            if(x == '0'){
                c0.push_back(i);
            }
            else if(x == '1'){
                c1.push_back(i);
            }
            else c2.push_back(i);
        }
        if((int)c1.size() <= 6){
//                cout << "%";
            int res = 0;
            for(auto p : c1) res += (1 << p);
            for(auto p : c2) res += (1 << p);
            ll ans = 0;
            for(int msk = 0; msk < (1 << c1.size()); ++msk){
                int cur = res;
                for(int i = 0; i < c1.size(); ++i){
                    if(bit(msk, i)){
                        cur ^= (1 << c1[i]);
                    }
                }
                int s = __builtin_popcount(msk);
                if(s & 1) ans -= F[cur];
                else ans += F[cur];
            }
            cout << ans << endl;
        }
        else if((int)c0.size() <= 6){
//                cout << "?";
            int res = 0;
            for(auto p : c0) res += (1 << p);
            for(auto p : c2) res += (1 << p);
            ll ans = 0;
            for(int msk = 0; msk < (1 << c0.size()); ++msk){
                int cur = res;
                for(int i = 0; i < c0.size(); ++i){
                    if(bit(msk, i)){
                        cur ^= (1 << c0[i]);
                    }
                }
                int s = __builtin_popcount(msk);
                if(s & 1) ans -= G[cur];
                else ans += G[cur];
            }
            cout << ans << endl;
        }
        else{
//            cout << "#";
            int res = 0;
            for(auto p : c1) res += (1 << p);
            int ans = 0;
            for(int msk = 0; msk < (1 << c2.size()); ++msk){
                int cur = res;
                for(int i = 0; i < c2.size(); ++i){
                    if(bit(msk, i)){
                        cur ^= (1 << c2[i]);
                    }
                }
                ans += cnt[cur];
            }
            cout << ans << endl;
        }
    }
}
/*
3 5
12345678
000
0??
1?0
?11
???

3 1
12345678
???
*/

Compilation message

snake_escaping.cpp:24:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   24 | main(){
      | ^~~~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:75:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |                 for(int i = 0; i < c1.size(); ++i){
      |                                ~~^~~~~~~~~~~
snake_escaping.cpp:94:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |                 for(int i = 0; i < c0.size(); ++i){
      |                                ~~^~~~~~~~~~~
snake_escaping.cpp:112:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |                 for(int i = 0; i < c2.size(); ++i){
      |                                ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 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 1 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 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 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 1 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 1 ms 348 KB Output is correct
11 Correct 301 ms 4432 KB Output is correct
12 Correct 153 ms 3924 KB Output is correct
13 Correct 194 ms 3392 KB Output is correct
14 Correct 204 ms 3664 KB Output is correct
15 Correct 183 ms 4432 KB Output is correct
16 Correct 283 ms 3572 KB Output is correct
17 Correct 261 ms 3412 KB Output is correct
18 Correct 103 ms 13416 KB Output is correct
19 Correct 312 ms 13036 KB Output is correct
20 Correct 325 ms 14660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 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 1 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 1 ms 348 KB Output is correct
11 Correct 301 ms 4432 KB Output is correct
12 Correct 153 ms 3924 KB Output is correct
13 Correct 194 ms 3392 KB Output is correct
14 Correct 204 ms 3664 KB Output is correct
15 Correct 183 ms 4432 KB Output is correct
16 Correct 283 ms 3572 KB Output is correct
17 Correct 261 ms 3412 KB Output is correct
18 Correct 103 ms 13416 KB Output is correct
19 Correct 312 ms 13036 KB Output is correct
20 Correct 325 ms 14660 KB Output is correct
21 Correct 527 ms 18184 KB Output is correct
22 Correct 194 ms 18260 KB Output is correct
23 Correct 310 ms 17232 KB Output is correct
24 Correct 251 ms 17232 KB Output is correct
25 Correct 218 ms 19028 KB Output is correct
26 Correct 364 ms 17748 KB Output is correct
27 Correct 364 ms 17536 KB Output is correct
28 Correct 121 ms 20052 KB Output is correct
29 Correct 486 ms 16212 KB Output is correct
30 Correct 334 ms 18268 KB Output is correct
31 Correct 277 ms 18256 KB Output is correct
32 Correct 268 ms 18260 KB Output is correct
33 Correct 198 ms 16936 KB Output is correct
34 Correct 340 ms 17236 KB Output is correct
35 Correct 363 ms 17500 KB Output is correct
36 Correct 105 ms 16212 KB Output is correct
37 Correct 157 ms 18260 KB Output is correct
38 Correct 478 ms 16212 KB Output is correct
39 Correct 561 ms 17488 KB Output is correct
40 Correct 360 ms 17256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 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 1 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 1 ms 348 KB Output is correct
11 Correct 75 ms 13912 KB Output is correct
12 Correct 79 ms 14580 KB Output is correct
13 Correct 89 ms 15940 KB Output is correct
14 Correct 99 ms 16116 KB Output is correct
15 Correct 82 ms 16192 KB Output is correct
16 Correct 104 ms 16108 KB Output is correct
17 Correct 101 ms 15952 KB Output is correct
18 Correct 73 ms 16216 KB Output is correct
19 Correct 82 ms 15972 KB Output is correct
20 Correct 78 ms 16120 KB Output is correct
21 Correct 92 ms 16176 KB Output is correct
22 Correct 101 ms 16116 KB Output is correct
23 Correct 80 ms 16116 KB Output is correct
24 Correct 106 ms 16224 KB Output is correct
25 Correct 95 ms 16116 KB Output is correct
26 Correct 74 ms 15860 KB Output is correct
27 Correct 77 ms 16116 KB Output is correct
28 Correct 80 ms 15856 KB Output is correct
29 Correct 88 ms 16120 KB Output is correct
30 Correct 95 ms 16112 KB Output is correct
31 Correct 81 ms 15956 KB Output is correct
32 Correct 96 ms 16116 KB Output is correct
33 Correct 96 ms 16116 KB Output is correct
34 Correct 79 ms 15856 KB Output is correct
35 Correct 89 ms 15940 KB Output is correct
36 Correct 92 ms 16112 KB Output is correct
37 Correct 91 ms 16116 KB Output is correct
38 Correct 91 ms 16116 KB Output is correct
39 Correct 92 ms 15940 KB Output is correct
40 Correct 89 ms 16116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 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 1 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 1 ms 348 KB Output is correct
11 Correct 301 ms 4432 KB Output is correct
12 Correct 153 ms 3924 KB Output is correct
13 Correct 194 ms 3392 KB Output is correct
14 Correct 204 ms 3664 KB Output is correct
15 Correct 183 ms 4432 KB Output is correct
16 Correct 283 ms 3572 KB Output is correct
17 Correct 261 ms 3412 KB Output is correct
18 Correct 103 ms 13416 KB Output is correct
19 Correct 312 ms 13036 KB Output is correct
20 Correct 325 ms 14660 KB Output is correct
21 Correct 527 ms 18184 KB Output is correct
22 Correct 194 ms 18260 KB Output is correct
23 Correct 310 ms 17232 KB Output is correct
24 Correct 251 ms 17232 KB Output is correct
25 Correct 218 ms 19028 KB Output is correct
26 Correct 364 ms 17748 KB Output is correct
27 Correct 364 ms 17536 KB Output is correct
28 Correct 121 ms 20052 KB Output is correct
29 Correct 486 ms 16212 KB Output is correct
30 Correct 334 ms 18268 KB Output is correct
31 Correct 277 ms 18256 KB Output is correct
32 Correct 268 ms 18260 KB Output is correct
33 Correct 198 ms 16936 KB Output is correct
34 Correct 340 ms 17236 KB Output is correct
35 Correct 363 ms 17500 KB Output is correct
36 Correct 105 ms 16212 KB Output is correct
37 Correct 157 ms 18260 KB Output is correct
38 Correct 478 ms 16212 KB Output is correct
39 Correct 561 ms 17488 KB Output is correct
40 Correct 360 ms 17256 KB Output is correct
41 Correct 75 ms 13912 KB Output is correct
42 Correct 79 ms 14580 KB Output is correct
43 Correct 89 ms 15940 KB Output is correct
44 Correct 99 ms 16116 KB Output is correct
45 Correct 82 ms 16192 KB Output is correct
46 Correct 104 ms 16108 KB Output is correct
47 Correct 101 ms 15952 KB Output is correct
48 Correct 73 ms 16216 KB Output is correct
49 Correct 82 ms 15972 KB Output is correct
50 Correct 78 ms 16120 KB Output is correct
51 Correct 92 ms 16176 KB Output is correct
52 Correct 101 ms 16116 KB Output is correct
53 Correct 80 ms 16116 KB Output is correct
54 Correct 106 ms 16224 KB Output is correct
55 Correct 95 ms 16116 KB Output is correct
56 Correct 74 ms 15860 KB Output is correct
57 Correct 77 ms 16116 KB Output is correct
58 Correct 80 ms 15856 KB Output is correct
59 Correct 88 ms 16120 KB Output is correct
60 Correct 95 ms 16112 KB Output is correct
61 Correct 81 ms 15956 KB Output is correct
62 Correct 96 ms 16116 KB Output is correct
63 Correct 96 ms 16116 KB Output is correct
64 Correct 79 ms 15856 KB Output is correct
65 Correct 89 ms 15940 KB Output is correct
66 Correct 92 ms 16112 KB Output is correct
67 Correct 91 ms 16116 KB Output is correct
68 Correct 91 ms 16116 KB Output is correct
69 Correct 92 ms 15940 KB Output is correct
70 Correct 89 ms 16116 KB Output is correct
71 Correct 288 ms 40292 KB Output is correct
72 Correct 280 ms 40436 KB Output is correct
73 Correct 484 ms 38904 KB Output is correct
74 Correct 754 ms 39428 KB Output is correct
75 Correct 385 ms 41284 KB Output is correct
76 Correct 757 ms 39680 KB Output is correct
77 Correct 605 ms 39508 KB Output is correct
78 Correct 185 ms 43332 KB Output is correct
79 Correct 254 ms 37360 KB Output is correct
80 Correct 308 ms 40440 KB Output is correct
81 Correct 520 ms 40436 KB Output is correct
82 Correct 758 ms 39412 KB Output is correct
83 Correct 352 ms 38384 KB Output is correct
84 Correct 701 ms 39500 KB Output is correct
85 Correct 599 ms 39556 KB Output is correct
86 Correct 161 ms 37360 KB Output is correct
87 Correct 277 ms 40440 KB Output is correct
88 Correct 338 ms 37420 KB Output is correct
89 Correct 477 ms 38996 KB Output is correct
90 Correct 649 ms 39240 KB Output is correct
91 Correct 410 ms 38644 KB Output is correct
92 Correct 923 ms 39760 KB Output is correct
93 Correct 772 ms 39416 KB Output is correct
94 Correct 223 ms 37448 KB Output is correct
95 Correct 601 ms 39508 KB Output is correct
96 Correct 610 ms 39504 KB Output is correct
97 Correct 560 ms 39408 KB Output is correct
98 Correct 550 ms 39408 KB Output is correct
99 Correct 629 ms 39460 KB Output is correct
100 Correct 633 ms 39492 KB Output is correct