답안 #110131

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
110131 2019-05-09T14:09:30 Z popovicirobert Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1953 ms 36344 KB
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44

using namespace std;

int dp0[1 << 20], dp1[1 << 20];
char str[1 << 20];
int bit[20], sz;
int ans;

void bktQ(int p, int cnf) {
    if(p == sz) {
        ans += str[cnf] - '0';
    }
    else {
        bktQ(p + 1, cnf);
        bktQ(p + 1, cnf + (1 << bit[p]));
    }
}

void bkt0(int p, int cnf, int cnt) {
    if(p == sz) {
        if(cnt & 1) {
            ans -= dp0[cnf];
        }
        else {
            ans += dp0[cnf];
        }
    }
    else {
        bkt0(p + 1, cnf, cnt);
        bkt0(p + 1, cnf ^ (1 << bit[p]), cnt + 1);
    }
}

void bkt1(int p, int cnf, int cnt) {
    if(p == sz) {
        if(cnt & 1) {
            ans -= dp1[cnf];
        }
        else {
            ans += dp1[cnf];
        }
    }
    else {
        bkt1(p + 1, cnf, cnt);
        bkt1(p + 1, cnf ^ (1 << bit[p]), cnt + 1);
    }
}

int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    int i, l, q;
    //ios::sync_with_stdio(false);
    //cin.tie(0), cout.tie(0);

    scanf("%d%d%s" ,&l,&q,str);

    for(int conf = 0; conf < (1 << l); conf++) {
        dp0[conf ^ ((1 << l) - 1)] = str[conf] - '0';
        dp1[conf] = str[conf] - '0';
    }

    for(int bit = 0; bit < l; bit++) {
        for(int conf = 0; conf < (1 << (l - 1)); conf++) {
            int cur = (conf & ((1 << bit) - 1)) + (1 << bit) + ((conf >> bit) << (bit + 1));
            dp0[cur] += dp0[cur ^ (1 << bit)];
            dp1[cur] += dp1[cur ^ (1 << bit)];
        }
    }

    while(q > 0) {
        q--;

        static char cur[20];
        scanf("%s" ,cur);
        reverse(cur, cur + l);

        int numq = 0, num0 = 0, num1 = 0;
        for(i = 0; i < l; i++) {
            if(cur[i] == '0') {
                num0++;
            }
            else if(cur[i] == '1') {
                num1++;
            }
            else {
                numq++;
            }
        }

        int mn = min(numq, min(num0, num1));
        int cnf = 0;
        ans = sz = 0;

        if(numq == mn) {

            for(i = 0; i < l; i++) {
                if(cur[i] == '?') {
                    bit[sz++] = i;
                }
                else {
                    cnf += (1 << i) * (cur[i] == '1');
                }
            }

            bktQ(0, cnf);
        }
        else if(num0 == mn) {

            for(i = 0; i < l; i++) {
                if(cur[i] == '0') {
                    bit[sz++] = i;
                }
                if(cur[i] != '1') {
                    cnf += (1 << i);
                }
            }

            bkt0(0, cnf, 0);
        }
        else if(num1 == mn) {

             for(i = 0; i < l; i++) {
                if(cur[i] == '1') {
                    bit[sz++] = i;
                }
                if(cur[i] != '0') {
                    cnf += (1 << i);
                }
            }

            bkt1(0, cnf, 0);
        }

        printf("%d\n" ,ans);

    }

    //cin.close();
    //cout.close();
    return 0;
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:62:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%s" ,&l,&q,str);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:81:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%s" ,cur);
         ~~~~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 349 ms 5000 KB Output is correct
12 Correct 352 ms 4728 KB Output is correct
13 Correct 377 ms 3976 KB Output is correct
14 Correct 377 ms 4096 KB Output is correct
15 Correct 385 ms 5200 KB Output is correct
16 Correct 471 ms 4212 KB Output is correct
17 Correct 442 ms 4232 KB Output is correct
18 Correct 267 ms 5984 KB Output is correct
19 Correct 291 ms 3064 KB Output is correct
20 Correct 364 ms 4600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 349 ms 5000 KB Output is correct
12 Correct 352 ms 4728 KB Output is correct
13 Correct 377 ms 3976 KB Output is correct
14 Correct 377 ms 4096 KB Output is correct
15 Correct 385 ms 5200 KB Output is correct
16 Correct 471 ms 4212 KB Output is correct
17 Correct 442 ms 4232 KB Output is correct
18 Correct 267 ms 5984 KB Output is correct
19 Correct 291 ms 3064 KB Output is correct
20 Correct 364 ms 4600 KB Output is correct
21 Correct 427 ms 5184 KB Output is correct
22 Correct 457 ms 5368 KB Output is correct
23 Correct 511 ms 4344 KB Output is correct
24 Correct 468 ms 4088 KB Output is correct
25 Correct 481 ms 6008 KB Output is correct
26 Correct 552 ms 4600 KB Output is correct
27 Correct 464 ms 4656 KB Output is correct
28 Correct 234 ms 7032 KB Output is correct
29 Correct 345 ms 3192 KB Output is correct
30 Correct 520 ms 5240 KB Output is correct
31 Correct 474 ms 5288 KB Output is correct
32 Correct 569 ms 5352 KB Output is correct
33 Correct 416 ms 3960 KB Output is correct
34 Correct 507 ms 4216 KB Output is correct
35 Correct 524 ms 4600 KB Output is correct
36 Correct 248 ms 3056 KB Output is correct
37 Correct 359 ms 5112 KB Output is correct
38 Correct 431 ms 3144 KB Output is correct
39 Correct 446 ms 4344 KB Output is correct
40 Correct 506 ms 4204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 82 ms 10192 KB Output is correct
12 Correct 81 ms 10232 KB Output is correct
13 Correct 104 ms 10164 KB Output is correct
14 Correct 113 ms 10200 KB Output is correct
15 Correct 87 ms 10264 KB Output is correct
16 Correct 141 ms 10208 KB Output is correct
17 Correct 108 ms 10204 KB Output is correct
18 Correct 78 ms 10360 KB Output is correct
19 Correct 90 ms 10104 KB Output is correct
20 Correct 136 ms 10232 KB Output is correct
21 Correct 102 ms 10232 KB Output is correct
22 Correct 142 ms 10232 KB Output is correct
23 Correct 112 ms 10104 KB Output is correct
24 Correct 121 ms 10104 KB Output is correct
25 Correct 107 ms 10156 KB Output is correct
26 Correct 68 ms 10092 KB Output is correct
27 Correct 114 ms 10360 KB Output is correct
28 Correct 93 ms 10060 KB Output is correct
29 Correct 104 ms 10188 KB Output is correct
30 Correct 140 ms 10156 KB Output is correct
31 Correct 96 ms 10116 KB Output is correct
32 Correct 151 ms 10176 KB Output is correct
33 Correct 152 ms 10236 KB Output is correct
34 Correct 82 ms 10136 KB Output is correct
35 Correct 123 ms 10152 KB Output is correct
36 Correct 131 ms 10204 KB Output is correct
37 Correct 117 ms 10224 KB Output is correct
38 Correct 130 ms 10360 KB Output is correct
39 Correct 144 ms 10104 KB Output is correct
40 Correct 118 ms 10204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 349 ms 5000 KB Output is correct
12 Correct 352 ms 4728 KB Output is correct
13 Correct 377 ms 3976 KB Output is correct
14 Correct 377 ms 4096 KB Output is correct
15 Correct 385 ms 5200 KB Output is correct
16 Correct 471 ms 4212 KB Output is correct
17 Correct 442 ms 4232 KB Output is correct
18 Correct 267 ms 5984 KB Output is correct
19 Correct 291 ms 3064 KB Output is correct
20 Correct 364 ms 4600 KB Output is correct
21 Correct 427 ms 5184 KB Output is correct
22 Correct 457 ms 5368 KB Output is correct
23 Correct 511 ms 4344 KB Output is correct
24 Correct 468 ms 4088 KB Output is correct
25 Correct 481 ms 6008 KB Output is correct
26 Correct 552 ms 4600 KB Output is correct
27 Correct 464 ms 4656 KB Output is correct
28 Correct 234 ms 7032 KB Output is correct
29 Correct 345 ms 3192 KB Output is correct
30 Correct 520 ms 5240 KB Output is correct
31 Correct 474 ms 5288 KB Output is correct
32 Correct 569 ms 5352 KB Output is correct
33 Correct 416 ms 3960 KB Output is correct
34 Correct 507 ms 4216 KB Output is correct
35 Correct 524 ms 4600 KB Output is correct
36 Correct 248 ms 3056 KB Output is correct
37 Correct 359 ms 5112 KB Output is correct
38 Correct 431 ms 3144 KB Output is correct
39 Correct 446 ms 4344 KB Output is correct
40 Correct 506 ms 4204 KB Output is correct
41 Correct 82 ms 10192 KB Output is correct
42 Correct 81 ms 10232 KB Output is correct
43 Correct 104 ms 10164 KB Output is correct
44 Correct 113 ms 10200 KB Output is correct
45 Correct 87 ms 10264 KB Output is correct
46 Correct 141 ms 10208 KB Output is correct
47 Correct 108 ms 10204 KB Output is correct
48 Correct 78 ms 10360 KB Output is correct
49 Correct 90 ms 10104 KB Output is correct
50 Correct 136 ms 10232 KB Output is correct
51 Correct 102 ms 10232 KB Output is correct
52 Correct 142 ms 10232 KB Output is correct
53 Correct 112 ms 10104 KB Output is correct
54 Correct 121 ms 10104 KB Output is correct
55 Correct 107 ms 10156 KB Output is correct
56 Correct 68 ms 10092 KB Output is correct
57 Correct 114 ms 10360 KB Output is correct
58 Correct 93 ms 10060 KB Output is correct
59 Correct 104 ms 10188 KB Output is correct
60 Correct 140 ms 10156 KB Output is correct
61 Correct 96 ms 10116 KB Output is correct
62 Correct 151 ms 10176 KB Output is correct
63 Correct 152 ms 10236 KB Output is correct
64 Correct 82 ms 10136 KB Output is correct
65 Correct 123 ms 10152 KB Output is correct
66 Correct 131 ms 10204 KB Output is correct
67 Correct 117 ms 10224 KB Output is correct
68 Correct 130 ms 10360 KB Output is correct
69 Correct 144 ms 10104 KB Output is correct
70 Correct 118 ms 10204 KB Output is correct
71 Correct 694 ms 14548 KB Output is correct
72 Correct 657 ms 14792 KB Output is correct
73 Correct 825 ms 13364 KB Output is correct
74 Correct 1840 ms 14656 KB Output is correct
75 Correct 782 ms 15488 KB Output is correct
76 Correct 1750 ms 14012 KB Output is correct
77 Correct 1380 ms 13880 KB Output is correct
78 Correct 380 ms 17648 KB Output is correct
79 Correct 527 ms 11644 KB Output is correct
80 Correct 716 ms 14776 KB Output is correct
81 Correct 1053 ms 14564 KB Output is correct
82 Correct 1953 ms 14592 KB Output is correct
83 Correct 720 ms 34392 KB Output is correct
84 Correct 1422 ms 35128 KB Output is correct
85 Correct 1340 ms 35364 KB Output is correct
86 Correct 414 ms 33212 KB Output is correct
87 Correct 733 ms 36344 KB Output is correct
88 Correct 619 ms 33388 KB Output is correct
89 Correct 775 ms 34936 KB Output is correct
90 Correct 975 ms 35252 KB Output is correct
91 Correct 729 ms 34424 KB Output is correct
92 Correct 1803 ms 35544 KB Output is correct
93 Correct 1521 ms 35240 KB Output is correct
94 Correct 433 ms 33268 KB Output is correct
95 Correct 1071 ms 35196 KB Output is correct
96 Correct 1122 ms 35192 KB Output is correct
97 Correct 1251 ms 35272 KB Output is correct
98 Correct 1161 ms 35408 KB Output is correct
99 Correct 1169 ms 35320 KB Output is correct
100 Correct 1212 ms 35192 KB Output is correct