답안 #171471

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
171471 2019-12-28T17:31:07 Z Akashi Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1757 ms 61656 KB
#include <bits/stdc++.h>
using namespace std;

int n, q, L1, L2;
int m1[1000005], m2[1000005], ans[1000005];
int p3[25], p2[25];

char s[1048580];
char in[25];

int dp[1594330];
pair <int, int> son[1594330];

bool f[2200][130];

int main()
{
    scanf("%d%d", &n, &q);
    scanf("%s", s);

    p3[0] = 1;
    for(int i = 1; i <= 21 ; ++i) p3[i] = p3[i - 1] * 3;
    p2[0] = 1;
    for(int i = 1; i <= 21 ; ++i) p2[i] = p2[i - 1] * 2;

    if(n >= 10) L1 = 7, L2 = n - L1;
    else L1 = 1, L2 = n - L1;

    for(int mask = 0; mask < p3[L1] ; ++mask){
        for(int mask2 = 0; mask2 < p2[L1] ; ++mask2){
            bool ok = true;

            for(int i = 0; i < L1 ; ++i){
                int x = (mask % p3[i + 1]) / p3[i];
                int y = (mask2 & p2[i]) > 0;
                if(x == y || x == 2) continue ;
                ok = false; break ;
            }

            f[mask][mask2] = ok;
        }
    }

    for(int i = 1; i <= q ; ++i){
        scanf("%s", in);

        for(int j = 0; j < L1 ; ++j){
            int x;
            if(in[j] == '0') x = 0;
            else if(in[j] == '1') x = 1;
            else x = 2;
            m1[i] = m1[i] + x * p3[L1 - j - 1];
        }

        for(int j = L1; j < n ; ++j){
            int x;
            if(in[j] == '0') x = 0;
            else if(in[j] == '1') x = 1;
            else x = 2;
            m2[i] = m2[i] + x * p3[j - L1];
        }
    }

    for(int mask = 0; mask < p3[L2] ; ++mask){
        int mask2 = 0;
        bool found = false;
        for(int i = 0; i < L2 ; ++i){
            int x = (mask % p3[i + 1]) / p3[i];
            if(x == 2){
                found = true;
                son[mask] = {mask - x * p3[i], mask - x * p3[i] + p3[i]};
                break ;
            }
            else mask2 = mask2 + x * p2[L2 - i - 1];
        }

        if(!found) son[mask] = {mask2, -1};
    }

    int P = (1 << L1);
    for(int mask2 = 0; mask2 < P ; ++mask2){

        int ad = mask2 * p2[L2];
        for(int mask = 0; mask < p3[L2] ; ++mask){
            if(son[mask].second == -1) dp[mask] = s[son[mask].first + ad] - '0';
            else dp[mask] = dp[son[mask].first] + dp[son[mask].second];
        }

        for(int i = 1; i <= q ; ++i){
            if(f[m1[i]][mask2])
                ans[i] += dp[m2[i]];
        }
    }

    for(int i = 1; i <= q ; ++i)
        printf("%d\n", ans[i]);

    return 0;
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:18:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &n, &q);
     ~~~~~^~~~~~~~~~~~~~~~
snake_escaping.cpp:19:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%s", s);
     ~~~~~^~~~~~~~~
snake_escaping.cpp:45:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%s", in);
         ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 632 KB Output is correct
2 Correct 10 ms 632 KB Output is correct
3 Correct 10 ms 632 KB Output is correct
4 Correct 10 ms 632 KB Output is correct
5 Correct 11 ms 632 KB Output is correct
6 Correct 10 ms 632 KB Output is correct
7 Correct 10 ms 632 KB Output is correct
8 Correct 10 ms 632 KB Output is correct
9 Correct 10 ms 632 KB Output is correct
10 Correct 10 ms 632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 632 KB Output is correct
2 Correct 10 ms 632 KB Output is correct
3 Correct 10 ms 632 KB Output is correct
4 Correct 10 ms 632 KB Output is correct
5 Correct 11 ms 632 KB Output is correct
6 Correct 10 ms 632 KB Output is correct
7 Correct 10 ms 632 KB Output is correct
8 Correct 10 ms 632 KB Output is correct
9 Correct 10 ms 632 KB Output is correct
10 Correct 10 ms 632 KB Output is correct
11 Correct 581 ms 17940 KB Output is correct
12 Correct 723 ms 17400 KB Output is correct
13 Correct 591 ms 16824 KB Output is correct
14 Correct 611 ms 16760 KB Output is correct
15 Correct 809 ms 17916 KB Output is correct
16 Correct 648 ms 17144 KB Output is correct
17 Correct 720 ms 17016 KB Output is correct
18 Correct 421 ms 18936 KB Output is correct
19 Correct 462 ms 15844 KB Output is correct
20 Correct 661 ms 17568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 632 KB Output is correct
2 Correct 10 ms 632 KB Output is correct
3 Correct 10 ms 632 KB Output is correct
4 Correct 10 ms 632 KB Output is correct
5 Correct 11 ms 632 KB Output is correct
6 Correct 10 ms 632 KB Output is correct
7 Correct 10 ms 632 KB Output is correct
8 Correct 10 ms 632 KB Output is correct
9 Correct 10 ms 632 KB Output is correct
10 Correct 10 ms 632 KB Output is correct
11 Correct 581 ms 17940 KB Output is correct
12 Correct 723 ms 17400 KB Output is correct
13 Correct 591 ms 16824 KB Output is correct
14 Correct 611 ms 16760 KB Output is correct
15 Correct 809 ms 17916 KB Output is correct
16 Correct 648 ms 17144 KB Output is correct
17 Correct 720 ms 17016 KB Output is correct
18 Correct 421 ms 18936 KB Output is correct
19 Correct 462 ms 15844 KB Output is correct
20 Correct 661 ms 17568 KB Output is correct
21 Correct 663 ms 17912 KB Output is correct
22 Correct 752 ms 17972 KB Output is correct
23 Correct 688 ms 17016 KB Output is correct
24 Correct 742 ms 16888 KB Output is correct
25 Correct 868 ms 18896 KB Output is correct
26 Correct 750 ms 17188 KB Output is correct
27 Correct 724 ms 17092 KB Output is correct
28 Correct 456 ms 19812 KB Output is correct
29 Correct 534 ms 15760 KB Output is correct
30 Correct 685 ms 17784 KB Output is correct
31 Correct 815 ms 17656 KB Output is correct
32 Correct 755 ms 17532 KB Output is correct
33 Correct 646 ms 16384 KB Output is correct
34 Correct 702 ms 16504 KB Output is correct
35 Correct 968 ms 16892 KB Output is correct
36 Correct 392 ms 15216 KB Output is correct
37 Correct 700 ms 17400 KB Output is correct
38 Correct 510 ms 15200 KB Output is correct
39 Correct 833 ms 16412 KB Output is correct
40 Correct 725 ms 16028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 632 KB Output is correct
2 Correct 10 ms 632 KB Output is correct
3 Correct 10 ms 632 KB Output is correct
4 Correct 10 ms 632 KB Output is correct
5 Correct 11 ms 632 KB Output is correct
6 Correct 10 ms 632 KB Output is correct
7 Correct 10 ms 632 KB Output is correct
8 Correct 10 ms 632 KB Output is correct
9 Correct 10 ms 632 KB Output is correct
10 Correct 10 ms 632 KB Output is correct
11 Correct 394 ms 21980 KB Output is correct
12 Correct 383 ms 23576 KB Output is correct
13 Correct 390 ms 23236 KB Output is correct
14 Correct 389 ms 23288 KB Output is correct
15 Correct 402 ms 23312 KB Output is correct
16 Correct 410 ms 23268 KB Output is correct
17 Correct 429 ms 23196 KB Output is correct
18 Correct 356 ms 23524 KB Output is correct
19 Correct 380 ms 23092 KB Output is correct
20 Correct 425 ms 23416 KB Output is correct
21 Correct 418 ms 23388 KB Output is correct
22 Correct 404 ms 23416 KB Output is correct
23 Correct 386 ms 23160 KB Output is correct
24 Correct 399 ms 23288 KB Output is correct
25 Correct 406 ms 23188 KB Output is correct
26 Correct 359 ms 23164 KB Output is correct
27 Correct 388 ms 23388 KB Output is correct
28 Correct 460 ms 23164 KB Output is correct
29 Correct 382 ms 23316 KB Output is correct
30 Correct 393 ms 23192 KB Output is correct
31 Correct 385 ms 23264 KB Output is correct
32 Correct 391 ms 23260 KB Output is correct
33 Correct 436 ms 23252 KB Output is correct
34 Correct 366 ms 23160 KB Output is correct
35 Correct 416 ms 23288 KB Output is correct
36 Correct 407 ms 23288 KB Output is correct
37 Correct 411 ms 23292 KB Output is correct
38 Correct 392 ms 23160 KB Output is correct
39 Correct 406 ms 23292 KB Output is correct
40 Correct 393 ms 23288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 632 KB Output is correct
2 Correct 10 ms 632 KB Output is correct
3 Correct 10 ms 632 KB Output is correct
4 Correct 10 ms 632 KB Output is correct
5 Correct 11 ms 632 KB Output is correct
6 Correct 10 ms 632 KB Output is correct
7 Correct 10 ms 632 KB Output is correct
8 Correct 10 ms 632 KB Output is correct
9 Correct 10 ms 632 KB Output is correct
10 Correct 10 ms 632 KB Output is correct
11 Correct 581 ms 17940 KB Output is correct
12 Correct 723 ms 17400 KB Output is correct
13 Correct 591 ms 16824 KB Output is correct
14 Correct 611 ms 16760 KB Output is correct
15 Correct 809 ms 17916 KB Output is correct
16 Correct 648 ms 17144 KB Output is correct
17 Correct 720 ms 17016 KB Output is correct
18 Correct 421 ms 18936 KB Output is correct
19 Correct 462 ms 15844 KB Output is correct
20 Correct 661 ms 17568 KB Output is correct
21 Correct 663 ms 17912 KB Output is correct
22 Correct 752 ms 17972 KB Output is correct
23 Correct 688 ms 17016 KB Output is correct
24 Correct 742 ms 16888 KB Output is correct
25 Correct 868 ms 18896 KB Output is correct
26 Correct 750 ms 17188 KB Output is correct
27 Correct 724 ms 17092 KB Output is correct
28 Correct 456 ms 19812 KB Output is correct
29 Correct 534 ms 15760 KB Output is correct
30 Correct 685 ms 17784 KB Output is correct
31 Correct 815 ms 17656 KB Output is correct
32 Correct 755 ms 17532 KB Output is correct
33 Correct 646 ms 16384 KB Output is correct
34 Correct 702 ms 16504 KB Output is correct
35 Correct 968 ms 16892 KB Output is correct
36 Correct 392 ms 15216 KB Output is correct
37 Correct 700 ms 17400 KB Output is correct
38 Correct 510 ms 15200 KB Output is correct
39 Correct 833 ms 16412 KB Output is correct
40 Correct 725 ms 16028 KB Output is correct
41 Correct 394 ms 21980 KB Output is correct
42 Correct 383 ms 23576 KB Output is correct
43 Correct 390 ms 23236 KB Output is correct
44 Correct 389 ms 23288 KB Output is correct
45 Correct 402 ms 23312 KB Output is correct
46 Correct 410 ms 23268 KB Output is correct
47 Correct 429 ms 23196 KB Output is correct
48 Correct 356 ms 23524 KB Output is correct
49 Correct 380 ms 23092 KB Output is correct
50 Correct 425 ms 23416 KB Output is correct
51 Correct 418 ms 23388 KB Output is correct
52 Correct 404 ms 23416 KB Output is correct
53 Correct 386 ms 23160 KB Output is correct
54 Correct 399 ms 23288 KB Output is correct
55 Correct 406 ms 23188 KB Output is correct
56 Correct 359 ms 23164 KB Output is correct
57 Correct 388 ms 23388 KB Output is correct
58 Correct 460 ms 23164 KB Output is correct
59 Correct 382 ms 23316 KB Output is correct
60 Correct 393 ms 23192 KB Output is correct
61 Correct 385 ms 23264 KB Output is correct
62 Correct 391 ms 23260 KB Output is correct
63 Correct 436 ms 23252 KB Output is correct
64 Correct 366 ms 23160 KB Output is correct
65 Correct 416 ms 23288 KB Output is correct
66 Correct 407 ms 23288 KB Output is correct
67 Correct 411 ms 23292 KB Output is correct
68 Correct 392 ms 23160 KB Output is correct
69 Correct 406 ms 23292 KB Output is correct
70 Correct 393 ms 23288 KB Output is correct
71 Correct 1213 ms 58772 KB Output is correct
72 Correct 1500 ms 58872 KB Output is correct
73 Correct 1470 ms 57484 KB Output is correct
74 Correct 1630 ms 57700 KB Output is correct
75 Correct 1676 ms 59624 KB Output is correct
76 Correct 1663 ms 58232 KB Output is correct
77 Correct 1710 ms 57956 KB Output is correct
78 Correct 899 ms 61656 KB Output is correct
79 Correct 1061 ms 55660 KB Output is correct
80 Correct 1304 ms 58872 KB Output is correct
81 Correct 1757 ms 58640 KB Output is correct
82 Correct 1651 ms 57736 KB Output is correct
83 Correct 1130 ms 56932 KB Output is correct
84 Correct 1552 ms 57668 KB Output is correct
85 Correct 1703 ms 57932 KB Output is correct
86 Correct 860 ms 55868 KB Output is correct
87 Correct 1350 ms 58712 KB Output is correct
88 Correct 1060 ms 55736 KB Output is correct
89 Correct 1588 ms 57588 KB Output is correct
90 Correct 1617 ms 57676 KB Output is correct
91 Correct 1177 ms 56840 KB Output is correct
92 Correct 1689 ms 58092 KB Output is correct
93 Correct 1641 ms 57932 KB Output is correct
94 Correct 794 ms 55760 KB Output is correct
95 Correct 1639 ms 57912 KB Output is correct
96 Correct 1654 ms 57820 KB Output is correct
97 Correct 1592 ms 57836 KB Output is correct
98 Correct 1605 ms 57820 KB Output is correct
99 Correct 1719 ms 57676 KB Output is correct
100 Correct 1622 ms 57720 KB Output is correct