답안 #396888

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
396888 2021-04-30T22:54:29 Z rqi Snake Escaping (JOI18_snake_escaping) C++14
75 / 100
840 ms 65540 KB
#include <bits/stdc++.h>
using namespace std;

typedef vector<int> vi;

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

#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;
string T[mx];

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];
            }
        }
    }
}

void printTrip(){
    map<char, int> from_char;
    from_char['0'] = 0;
    from_char['1'] = 1;
    from_char['?'] = 2;
    for(int i = 1; i <= Q; i++){
        vi typ_count(3, 0);
        for(auto u: T[i]){
            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[i][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(i == 0) break;
        }

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

        cout << res << "\n";

    }
    

    
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    cin >> L >> Q;
    cin >> S;
    for(int i = 1; i <= Q; i++){
        cin >> T[i];
        reverse(all(T[i]));
    }

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

    genTrip();
    printTrip();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 39828 KB Output is correct
2 Correct 51 ms 39848 KB Output is correct
3 Correct 55 ms 39864 KB Output is correct
4 Correct 52 ms 39792 KB Output is correct
5 Correct 57 ms 39876 KB Output is correct
6 Correct 60 ms 39856 KB Output is correct
7 Correct 50 ms 39864 KB Output is correct
8 Correct 50 ms 39844 KB Output is correct
9 Correct 50 ms 39860 KB Output is correct
10 Correct 60 ms 39856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 39828 KB Output is correct
2 Correct 51 ms 39848 KB Output is correct
3 Correct 55 ms 39864 KB Output is correct
4 Correct 52 ms 39792 KB Output is correct
5 Correct 57 ms 39876 KB Output is correct
6 Correct 60 ms 39856 KB Output is correct
7 Correct 50 ms 39864 KB Output is correct
8 Correct 50 ms 39844 KB Output is correct
9 Correct 50 ms 39860 KB Output is correct
10 Correct 60 ms 39856 KB Output is correct
11 Correct 570 ms 54668 KB Output is correct
12 Correct 629 ms 54280 KB Output is correct
13 Correct 589 ms 53444 KB Output is correct
14 Correct 672 ms 53572 KB Output is correct
15 Correct 657 ms 54736 KB Output is correct
16 Correct 680 ms 53724 KB Output is correct
17 Correct 691 ms 53748 KB Output is correct
18 Correct 483 ms 55616 KB Output is correct
19 Correct 481 ms 52540 KB Output is correct
20 Correct 599 ms 54212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 39828 KB Output is correct
2 Correct 51 ms 39848 KB Output is correct
3 Correct 55 ms 39864 KB Output is correct
4 Correct 52 ms 39792 KB Output is correct
5 Correct 57 ms 39876 KB Output is correct
6 Correct 60 ms 39856 KB Output is correct
7 Correct 50 ms 39864 KB Output is correct
8 Correct 50 ms 39844 KB Output is correct
9 Correct 50 ms 39860 KB Output is correct
10 Correct 60 ms 39856 KB Output is correct
11 Correct 570 ms 54668 KB Output is correct
12 Correct 629 ms 54280 KB Output is correct
13 Correct 589 ms 53444 KB Output is correct
14 Correct 672 ms 53572 KB Output is correct
15 Correct 657 ms 54736 KB Output is correct
16 Correct 680 ms 53724 KB Output is correct
17 Correct 691 ms 53748 KB Output is correct
18 Correct 483 ms 55616 KB Output is correct
19 Correct 481 ms 52540 KB Output is correct
20 Correct 599 ms 54212 KB Output is correct
21 Correct 678 ms 57668 KB Output is correct
22 Correct 741 ms 57868 KB Output is correct
23 Correct 711 ms 56740 KB Output is correct
24 Correct 815 ms 56580 KB Output is correct
25 Correct 787 ms 58752 KB Output is correct
26 Correct 836 ms 57208 KB Output is correct
27 Correct 821 ms 57028 KB Output is correct
28 Correct 514 ms 59712 KB Output is correct
29 Correct 557 ms 55608 KB Output is correct
30 Correct 739 ms 57832 KB Output is correct
31 Correct 840 ms 57620 KB Output is correct
32 Correct 831 ms 57588 KB Output is correct
33 Correct 629 ms 56576 KB Output is correct
34 Correct 826 ms 56608 KB Output is correct
35 Correct 813 ms 57028 KB Output is correct
36 Correct 453 ms 55584 KB Output is correct
37 Correct 729 ms 57632 KB Output is correct
38 Correct 575 ms 55556 KB Output is correct
39 Correct 720 ms 56724 KB Output is correct
40 Correct 814 ms 56624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 39828 KB Output is correct
2 Correct 51 ms 39848 KB Output is correct
3 Correct 55 ms 39864 KB Output is correct
4 Correct 52 ms 39792 KB Output is correct
5 Correct 57 ms 39876 KB Output is correct
6 Correct 60 ms 39856 KB Output is correct
7 Correct 50 ms 39864 KB Output is correct
8 Correct 50 ms 39844 KB Output is correct
9 Correct 50 ms 39860 KB Output is correct
10 Correct 60 ms 39856 KB Output is correct
11 Correct 248 ms 57796 KB Output is correct
12 Correct 255 ms 57808 KB Output is correct
13 Correct 266 ms 57828 KB Output is correct
14 Correct 266 ms 57736 KB Output is correct
15 Correct 260 ms 57828 KB Output is correct
16 Correct 271 ms 57756 KB Output is correct
17 Correct 272 ms 57824 KB Output is correct
18 Correct 246 ms 57944 KB Output is correct
19 Correct 242 ms 57576 KB Output is correct
20 Correct 283 ms 57844 KB Output is correct
21 Correct 282 ms 57800 KB Output is correct
22 Correct 263 ms 57844 KB Output is correct
23 Correct 259 ms 57844 KB Output is correct
24 Correct 272 ms 57740 KB Output is correct
25 Correct 303 ms 57756 KB Output is correct
26 Correct 237 ms 57584 KB Output is correct
27 Correct 257 ms 57836 KB Output is correct
28 Correct 240 ms 57640 KB Output is correct
29 Correct 255 ms 57684 KB Output is correct
30 Correct 265 ms 57708 KB Output is correct
31 Correct 241 ms 57716 KB Output is correct
32 Correct 279 ms 57804 KB Output is correct
33 Correct 271 ms 57848 KB Output is correct
34 Correct 237 ms 57608 KB Output is correct
35 Correct 261 ms 57712 KB Output is correct
36 Correct 263 ms 57696 KB Output is correct
37 Correct 290 ms 57684 KB Output is correct
38 Correct 263 ms 57720 KB Output is correct
39 Correct 266 ms 57828 KB Output is correct
40 Correct 263 ms 57744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 39828 KB Output is correct
2 Correct 51 ms 39848 KB Output is correct
3 Correct 55 ms 39864 KB Output is correct
4 Correct 52 ms 39792 KB Output is correct
5 Correct 57 ms 39876 KB Output is correct
6 Correct 60 ms 39856 KB Output is correct
7 Correct 50 ms 39864 KB Output is correct
8 Correct 50 ms 39844 KB Output is correct
9 Correct 50 ms 39860 KB Output is correct
10 Correct 60 ms 39856 KB Output is correct
11 Correct 570 ms 54668 KB Output is correct
12 Correct 629 ms 54280 KB Output is correct
13 Correct 589 ms 53444 KB Output is correct
14 Correct 672 ms 53572 KB Output is correct
15 Correct 657 ms 54736 KB Output is correct
16 Correct 680 ms 53724 KB Output is correct
17 Correct 691 ms 53748 KB Output is correct
18 Correct 483 ms 55616 KB Output is correct
19 Correct 481 ms 52540 KB Output is correct
20 Correct 599 ms 54212 KB Output is correct
21 Correct 678 ms 57668 KB Output is correct
22 Correct 741 ms 57868 KB Output is correct
23 Correct 711 ms 56740 KB Output is correct
24 Correct 815 ms 56580 KB Output is correct
25 Correct 787 ms 58752 KB Output is correct
26 Correct 836 ms 57208 KB Output is correct
27 Correct 821 ms 57028 KB Output is correct
28 Correct 514 ms 59712 KB Output is correct
29 Correct 557 ms 55608 KB Output is correct
30 Correct 739 ms 57832 KB Output is correct
31 Correct 840 ms 57620 KB Output is correct
32 Correct 831 ms 57588 KB Output is correct
33 Correct 629 ms 56576 KB Output is correct
34 Correct 826 ms 56608 KB Output is correct
35 Correct 813 ms 57028 KB Output is correct
36 Correct 453 ms 55584 KB Output is correct
37 Correct 729 ms 57632 KB Output is correct
38 Correct 575 ms 55556 KB Output is correct
39 Correct 720 ms 56724 KB Output is correct
40 Correct 814 ms 56624 KB Output is correct
41 Correct 248 ms 57796 KB Output is correct
42 Correct 255 ms 57808 KB Output is correct
43 Correct 266 ms 57828 KB Output is correct
44 Correct 266 ms 57736 KB Output is correct
45 Correct 260 ms 57828 KB Output is correct
46 Correct 271 ms 57756 KB Output is correct
47 Correct 272 ms 57824 KB Output is correct
48 Correct 246 ms 57944 KB Output is correct
49 Correct 242 ms 57576 KB Output is correct
50 Correct 283 ms 57844 KB Output is correct
51 Correct 282 ms 57800 KB Output is correct
52 Correct 263 ms 57844 KB Output is correct
53 Correct 259 ms 57844 KB Output is correct
54 Correct 272 ms 57740 KB Output is correct
55 Correct 303 ms 57756 KB Output is correct
56 Correct 237 ms 57584 KB Output is correct
57 Correct 257 ms 57836 KB Output is correct
58 Correct 240 ms 57640 KB Output is correct
59 Correct 255 ms 57684 KB Output is correct
60 Correct 265 ms 57708 KB Output is correct
61 Correct 241 ms 57716 KB Output is correct
62 Correct 279 ms 57804 KB Output is correct
63 Correct 271 ms 57848 KB Output is correct
64 Correct 237 ms 57608 KB Output is correct
65 Correct 261 ms 57712 KB Output is correct
66 Correct 263 ms 57696 KB Output is correct
67 Correct 290 ms 57684 KB Output is correct
68 Correct 263 ms 57720 KB Output is correct
69 Correct 266 ms 57828 KB Output is correct
70 Correct 263 ms 57744 KB Output is correct
71 Runtime error 163 ms 65540 KB Execution killed with signal 9
72 Halted 0 ms 0 KB -