Submission #396892

# Submission time Handle Problem Language Result Execution time Memory
396892 2021-04-30T23:00:49 Z rqi Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1866 ms 51572 KB
#include <bits/stdc++.h>
using namespace std;

typedef vector<int> vi;

#define all(x) begin(x), end(x)

const int MOD = 1e9+7;

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int mx = 1000005;
int L, Q;
string S;

// int three_pow[15];
// int three_sub[1594323];

// void smallL(){
//     three_pow[0] = 1;
//     for(int i = 1; i <= L; i++){
//         three_pow[i] = three_pow[i-1]*3;
//     }
//     for(int i = 0; i < three_pow[L]; i++){
//         int sub = i;
//         int sum = 0;
//         for(int j = 0; j < L; j++){
//             if(sub % 3 == 2){
//                 //cout << i << " " << i-2*three_pow[j] << " " << i-1*three_pow[j] << "\n";
//                 three_sub[i] = three_sub[i-2*three_pow[j]]+three_sub[i-1*three_pow[j]];
//                 sum = -1;
//                 break;
//             }
//             if(sub % 3 == 1){
//                 sum+=(1<<j);
//             }
//             sub/=3;
//         }
//         if(sum != -1){
//             three_sub[i] = S[sum]-'0';
//             //cout << i << " " << three_sub[i] << "\n";
//         }
//     }

//     //cout << "three_sub[2]: " << three_sub[8] << "\n";

//     for(int i = 1; i <= Q; i++){
//         int sub = 0;
//         for(int j = 0; j < L; j++){
//             if(T[i][j] == '1'){
//                 sub+=three_pow[j];
//             }
//             else if(T[i][j] == '?'){
//                 sub+=2*three_pow[j];
//             }
//         }
//         cout << three_sub[sub] << "\n";
//     }
// }

int trip[3][1<<20];

void genTrip(){
    for(int i = 0; i < (1<<L); i++){
        trip[2][i] = S[i]-'0';
    }
    for(int typ = 0; typ < 2; typ++){
        array<int, 1<<20> dp;
        array<int, 1<<20> ndp;
        for(int i = 0; i < (1<<20); i++){
            dp[i] = trip[2][i];
            ndp[i] = 0;
        }
        for(int j = 0; j < L; j++){
            for(int i = 0; i < (1<<L); i++){
                if((i>>j)&1){ //put in ?
                    ndp[i] = dp[i-(1<<j)]+dp[i];
                }
                else{
                    if(typ == 1){
                        ndp[i] = dp[i];
                    }
                    else{
                        ndp[i] = dp[i+(1<<j)];    
                    }
                }
            }
            swap(dp, ndp);
            for(int i = 0; i < (1<<L); i++){
                ndp[i] = 0;
            }
        }
        for(int i = 0; i < (1<<L); i++){
            bool is_odd = 0;
            for(int j = 0; j < L; j++){
                if((i>>j)&1){
                    is_odd^=1;
                }
            }
            if(is_odd){
                trip[typ][i] = -dp[i];
            }
            else{
                trip[typ][i] = dp[i];
            }
        }
    }
}

string T;

void printTrip(){
    map<char, int> from_char;
    from_char['0'] = 0;
    from_char['1'] = 1;
    from_char['?'] = 2;
        vi typ_count(3, 0);
        for(auto u: T){
            typ_count[from_char[u]]++;
        }
        int typ = -1;

        int min_typ_count = min(typ_count[0], min(typ_count[1], typ_count[2]));

        for(int i = 0; i < 3; i++){
            if(typ_count[i] == min_typ_count){
                typ = i;
                break;
            }
        }

        // cout << "typ: " << typ << "\n";

        // cout << trip[1][0] << "\n";

        int sub1 = 0; //positions of typ
        int sub2 = 0; //

        for(int j = 0; j < L; j++){
            int char_type = from_char[T[j]];
            if(char_type == typ){
                sub1+=(1<<j);
            }
            else{
                if(typ == 2){
                    if(char_type == 1){
                        sub2+=(1<<j);
                    }
                }
                else{
                    if(char_type == 2){
                        sub2+=(1<<j);
                    }
                }
            }
        }

        int res = 0;
        for(int i = sub1;;i = ((i-1)&sub1)){
            res+=trip[typ][i+sub2];
            // if(res >= MOD) res-=MOD;
            // if(res < 0) res+=MOD;
            if(i == 0) break;
        }

        if(typ != 2){
            bool is_odd = 0;
            for(auto u: T){
                if(from_char[u] == 2 || from_char[u] == typ){
                    is_odd^=1;
                }
            }
            if(is_odd){
                res = -res;
                // if(res >= MOD) res-=MOD;
                // if(res < 0) res+=MOD;
            }
        }
        

        cout << res << "\n";

    
    

    
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    cin >> L >> Q;
    cin >> S;

    genTrip();
    for(int i = 1; i <= Q; i++){
        cin >> T;
        reverse(all(T));
        printTrip();
    }

    // if(L <= 13){
    //     smallL();
    //     exit(0);
    // }

    
    
}
# Verdict Execution time Memory Grader output
1 Correct 32 ms 8524 KB Output is correct
2 Correct 31 ms 8544 KB Output is correct
3 Correct 39 ms 8544 KB Output is correct
4 Correct 32 ms 8544 KB Output is correct
5 Correct 31 ms 8524 KB Output is correct
6 Correct 31 ms 8524 KB Output is correct
7 Correct 33 ms 8544 KB Output is correct
8 Correct 32 ms 8536 KB Output is correct
9 Correct 38 ms 8524 KB Output is correct
10 Correct 33 ms 8544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 8524 KB Output is correct
2 Correct 31 ms 8544 KB Output is correct
3 Correct 39 ms 8544 KB Output is correct
4 Correct 32 ms 8544 KB Output is correct
5 Correct 31 ms 8524 KB Output is correct
6 Correct 31 ms 8524 KB Output is correct
7 Correct 33 ms 8544 KB Output is correct
8 Correct 32 ms 8536 KB Output is correct
9 Correct 38 ms 8524 KB Output is correct
10 Correct 33 ms 8544 KB Output is correct
11 Correct 697 ms 12612 KB Output is correct
12 Correct 757 ms 12376 KB Output is correct
13 Correct 742 ms 11416 KB Output is correct
14 Correct 800 ms 11460 KB Output is correct
15 Correct 794 ms 12572 KB Output is correct
16 Correct 817 ms 11716 KB Output is correct
17 Correct 811 ms 11656 KB Output is correct
18 Correct 585 ms 13572 KB Output is correct
19 Correct 610 ms 10656 KB Output is correct
20 Correct 734 ms 12216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 8524 KB Output is correct
2 Correct 31 ms 8544 KB Output is correct
3 Correct 39 ms 8544 KB Output is correct
4 Correct 32 ms 8544 KB Output is correct
5 Correct 31 ms 8524 KB Output is correct
6 Correct 31 ms 8524 KB Output is correct
7 Correct 33 ms 8544 KB Output is correct
8 Correct 32 ms 8536 KB Output is correct
9 Correct 38 ms 8524 KB Output is correct
10 Correct 33 ms 8544 KB Output is correct
11 Correct 697 ms 12612 KB Output is correct
12 Correct 757 ms 12376 KB Output is correct
13 Correct 742 ms 11416 KB Output is correct
14 Correct 800 ms 11460 KB Output is correct
15 Correct 794 ms 12572 KB Output is correct
16 Correct 817 ms 11716 KB Output is correct
17 Correct 811 ms 11656 KB Output is correct
18 Correct 585 ms 13572 KB Output is correct
19 Correct 610 ms 10656 KB Output is correct
20 Correct 734 ms 12216 KB Output is correct
21 Correct 825 ms 12788 KB Output is correct
22 Correct 928 ms 12796 KB Output is correct
23 Correct 836 ms 11784 KB Output is correct
24 Correct 946 ms 11716 KB Output is correct
25 Correct 926 ms 13692 KB Output is correct
26 Correct 967 ms 12140 KB Output is correct
27 Correct 941 ms 12200 KB Output is correct
28 Correct 701 ms 14592 KB Output is correct
29 Correct 691 ms 10712 KB Output is correct
30 Correct 840 ms 12712 KB Output is correct
31 Correct 1018 ms 12548 KB Output is correct
32 Correct 1032 ms 12676 KB Output is correct
33 Correct 757 ms 11476 KB Output is correct
34 Correct 989 ms 11664 KB Output is correct
35 Correct 949 ms 12056 KB Output is correct
36 Correct 591 ms 10668 KB Output is correct
37 Correct 934 ms 12572 KB Output is correct
38 Correct 730 ms 10648 KB Output is correct
39 Correct 842 ms 11900 KB Output is correct
40 Correct 963 ms 11748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 8524 KB Output is correct
2 Correct 31 ms 8544 KB Output is correct
3 Correct 39 ms 8544 KB Output is correct
4 Correct 32 ms 8544 KB Output is correct
5 Correct 31 ms 8524 KB Output is correct
6 Correct 31 ms 8524 KB Output is correct
7 Correct 33 ms 8544 KB Output is correct
8 Correct 32 ms 8536 KB Output is correct
9 Correct 38 ms 8524 KB Output is correct
10 Correct 33 ms 8544 KB Output is correct
11 Correct 239 ms 22224 KB Output is correct
12 Correct 239 ms 22224 KB Output is correct
13 Correct 240 ms 22120 KB Output is correct
14 Correct 274 ms 22096 KB Output is correct
15 Correct 239 ms 22224 KB Output is correct
16 Correct 321 ms 22144 KB Output is correct
17 Correct 252 ms 22116 KB Output is correct
18 Correct 222 ms 22364 KB Output is correct
19 Correct 223 ms 22020 KB Output is correct
20 Correct 235 ms 22192 KB Output is correct
21 Correct 250 ms 22220 KB Output is correct
22 Correct 261 ms 22092 KB Output is correct
23 Correct 240 ms 22096 KB Output is correct
24 Correct 256 ms 22104 KB Output is correct
25 Correct 263 ms 22228 KB Output is correct
26 Correct 227 ms 22024 KB Output is correct
27 Correct 239 ms 22220 KB Output is correct
28 Correct 231 ms 22112 KB Output is correct
29 Correct 249 ms 22116 KB Output is correct
30 Correct 235 ms 22096 KB Output is correct
31 Correct 239 ms 22116 KB Output is correct
32 Correct 268 ms 22100 KB Output is correct
33 Correct 257 ms 22180 KB Output is correct
34 Correct 228 ms 21968 KB Output is correct
35 Correct 244 ms 22112 KB Output is correct
36 Correct 268 ms 22092 KB Output is correct
37 Correct 249 ms 22164 KB Output is correct
38 Correct 245 ms 22172 KB Output is correct
39 Correct 246 ms 22108 KB Output is correct
40 Correct 250 ms 22100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 8524 KB Output is correct
2 Correct 31 ms 8544 KB Output is correct
3 Correct 39 ms 8544 KB Output is correct
4 Correct 32 ms 8544 KB Output is correct
5 Correct 31 ms 8524 KB Output is correct
6 Correct 31 ms 8524 KB Output is correct
7 Correct 33 ms 8544 KB Output is correct
8 Correct 32 ms 8536 KB Output is correct
9 Correct 38 ms 8524 KB Output is correct
10 Correct 33 ms 8544 KB Output is correct
11 Correct 697 ms 12612 KB Output is correct
12 Correct 757 ms 12376 KB Output is correct
13 Correct 742 ms 11416 KB Output is correct
14 Correct 800 ms 11460 KB Output is correct
15 Correct 794 ms 12572 KB Output is correct
16 Correct 817 ms 11716 KB Output is correct
17 Correct 811 ms 11656 KB Output is correct
18 Correct 585 ms 13572 KB Output is correct
19 Correct 610 ms 10656 KB Output is correct
20 Correct 734 ms 12216 KB Output is correct
21 Correct 825 ms 12788 KB Output is correct
22 Correct 928 ms 12796 KB Output is correct
23 Correct 836 ms 11784 KB Output is correct
24 Correct 946 ms 11716 KB Output is correct
25 Correct 926 ms 13692 KB Output is correct
26 Correct 967 ms 12140 KB Output is correct
27 Correct 941 ms 12200 KB Output is correct
28 Correct 701 ms 14592 KB Output is correct
29 Correct 691 ms 10712 KB Output is correct
30 Correct 840 ms 12712 KB Output is correct
31 Correct 1018 ms 12548 KB Output is correct
32 Correct 1032 ms 12676 KB Output is correct
33 Correct 757 ms 11476 KB Output is correct
34 Correct 989 ms 11664 KB Output is correct
35 Correct 949 ms 12056 KB Output is correct
36 Correct 591 ms 10668 KB Output is correct
37 Correct 934 ms 12572 KB Output is correct
38 Correct 730 ms 10648 KB Output is correct
39 Correct 842 ms 11900 KB Output is correct
40 Correct 963 ms 11748 KB Output is correct
41 Correct 239 ms 22224 KB Output is correct
42 Correct 239 ms 22224 KB Output is correct
43 Correct 240 ms 22120 KB Output is correct
44 Correct 274 ms 22096 KB Output is correct
45 Correct 239 ms 22224 KB Output is correct
46 Correct 321 ms 22144 KB Output is correct
47 Correct 252 ms 22116 KB Output is correct
48 Correct 222 ms 22364 KB Output is correct
49 Correct 223 ms 22020 KB Output is correct
50 Correct 235 ms 22192 KB Output is correct
51 Correct 250 ms 22220 KB Output is correct
52 Correct 261 ms 22092 KB Output is correct
53 Correct 240 ms 22096 KB Output is correct
54 Correct 256 ms 22104 KB Output is correct
55 Correct 263 ms 22228 KB Output is correct
56 Correct 227 ms 22024 KB Output is correct
57 Correct 239 ms 22220 KB Output is correct
58 Correct 231 ms 22112 KB Output is correct
59 Correct 249 ms 22116 KB Output is correct
60 Correct 235 ms 22096 KB Output is correct
61 Correct 239 ms 22116 KB Output is correct
62 Correct 268 ms 22100 KB Output is correct
63 Correct 257 ms 22180 KB Output is correct
64 Correct 228 ms 21968 KB Output is correct
65 Correct 244 ms 22112 KB Output is correct
66 Correct 268 ms 22092 KB Output is correct
67 Correct 249 ms 22164 KB Output is correct
68 Correct 245 ms 22172 KB Output is correct
69 Correct 246 ms 22108 KB Output is correct
70 Correct 250 ms 22100 KB Output is correct
71 Correct 1383 ms 48620 KB Output is correct
72 Correct 1355 ms 48740 KB Output is correct
73 Correct 1345 ms 47172 KB Output is correct
74 Correct 1584 ms 47460 KB Output is correct
75 Correct 1463 ms 49584 KB Output is correct
76 Correct 1805 ms 47904 KB Output is correct
77 Correct 1679 ms 47752 KB Output is correct
78 Correct 997 ms 51572 KB Output is correct
79 Correct 1068 ms 45568 KB Output is correct
80 Correct 1312 ms 48712 KB Output is correct
81 Correct 1646 ms 48572 KB Output is correct
82 Correct 1602 ms 47604 KB Output is correct
83 Correct 1243 ms 46744 KB Output is correct
84 Correct 1866 ms 47640 KB Output is correct
85 Correct 1714 ms 47852 KB Output is correct
86 Correct 869 ms 45516 KB Output is correct
87 Correct 1355 ms 48608 KB Output is correct
88 Correct 1090 ms 45516 KB Output is correct
89 Correct 1382 ms 47312 KB Output is correct
90 Correct 1330 ms 47548 KB Output is correct
91 Correct 1172 ms 46684 KB Output is correct
92 Correct 1802 ms 47872 KB Output is correct
93 Correct 1755 ms 47676 KB Output is correct
94 Correct 985 ms 45452 KB Output is correct
95 Correct 1502 ms 47796 KB Output is correct
96 Correct 1523 ms 47652 KB Output is correct
97 Correct 1513 ms 47568 KB Output is correct
98 Correct 1493 ms 47616 KB Output is correct
99 Correct 1519 ms 47672 KB Output is correct
100 Correct 1546 ms 47760 KB Output is correct