답안 #790087

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
790087 2023-07-22T10:24:54 Z PanosPask Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1676 ms 42212 KB
#include <bits/stdc++.h>
#define CHECK_BIT(val, pos) ((val) & (1 << pos))

using namespace std;

int N, L, Q;

vector<int> a;

// Sum of all subsets that are fully contained inside the mask
vector<int> in_mask;
// Sum of all subsets that fully contain the mask
vector<int> at_least;
vector<int> A, B, C;

int CountNormal(int mask, int cpos)
{
    if (cpos == C.size())
        return a[mask];

    return CountNormal(mask, cpos + 1) + CountNormal(mask + (1 << C[cpos]), cpos + 1);
}

// Count by all submasks that include
int CountInclusion(int mask, int apos)
{
    if (apos == A.size()) {
        int mul = __builtin_popcount(mask) - B.size();
        if (mul % 2)
            mul = -1;
        else
            mul = 1;

        return at_least[mask] * mul;
    }

    return CountInclusion(mask, apos + 1) + CountInclusion(mask + (1 << A[apos]), apos + 1);
}

int CountExclusion(int mask, int bpos)
{
    if (bpos == B.size()) {
        int mul = B.size() - (__builtin_popcount(mask) - C.size());
        if (mul % 2)
            mul = -1;
        else
            mul = 1;

        return in_mask[mask] * mul;
    }

    return CountExclusion(mask, bpos + 1) + CountExclusion(mask + (1 << B[bpos]), bpos + 1);
}

int main(void)
{
    scanf("%d %d", &L, &Q);
    N = (1 << L);

    a.resize(N);
    in_mask.resize(N);
    at_least.resize(N);
    for (int i = 0; i < N; i++) {
        scanf("%1d", &a[i]);
        in_mask[i] = at_least[i] = a[i];
    }

    for (int i = 0; i < L; i++) {
        for (int s = 0; s < N; s++) {
            if (!CHECK_BIT(s, i)) {
                in_mask[s + (1 << i)] += in_mask[s];
            }
        }
    }

    for (int i = 0; i < L; i++) {
        for (int s = 0; s < N; s++) {
            if (CHECK_BIT(s, i)) {
                at_least[s - (1 << i)] += at_least[s];
            }
        }
    }

    while (Q--) {
        C.clear();
        A.clear();
        B.clear();
        char c;
        for (int i = L - 1; i >= 0; i--) {
            scanf(" %c", &c);
            if (c == '?')
                C.push_back(i);
            else if (c == '1')
                B.push_back(i);
            else
                A.push_back(i);
        }

        int ans;
        if (C.size() <= 6) {
            int m = 0;
            for (auto p : B)
                m += (1 << p);
            ans = CountNormal(m, 0);
        }
        else if (A.size() <= 6) {
            int m = 0;
            for (auto p : B)
                m += (1 << p);

            ans = CountInclusion(m, 0);
        }
        else {
            int m = 0;
            for (auto p : C)
                m += (1 << p);

            ans = CountExclusion(m, 0);
        }

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

    return 0;
}

Compilation message

snake_escaping.cpp: In function 'int CountNormal(int, int)':
snake_escaping.cpp:18:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |     if (cpos == C.size())
      |         ~~~~~^~~~~~~~~~~
snake_escaping.cpp: In function 'int CountInclusion(int, int)':
snake_escaping.cpp:27:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |     if (apos == A.size()) {
      |         ~~~~~^~~~~~~~~~~
snake_escaping.cpp: In function 'int CountExclusion(int, int)':
snake_escaping.cpp:42:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |     if (bpos == B.size()) {
      |         ~~~~~^~~~~~~~~~~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:57:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |     scanf("%d %d", &L, &Q);
      |     ~~~~~^~~~~~~~~~~~~~~~~
snake_escaping.cpp:64:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |         scanf("%1d", &a[i]);
      |         ~~~~~^~~~~~~~~~~~~~
snake_escaping.cpp:90:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |             scanf(" %c", &c);
      |             ~~~~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 308 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 308 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 304 KB Output is correct
11 Correct 568 ms 15032 KB Output is correct
12 Correct 617 ms 14692 KB Output is correct
13 Correct 494 ms 13932 KB Output is correct
14 Correct 529 ms 14024 KB Output is correct
15 Correct 719 ms 15024 KB Output is correct
16 Correct 551 ms 14140 KB Output is correct
17 Correct 540 ms 14176 KB Output is correct
18 Correct 521 ms 16016 KB Output is correct
19 Correct 473 ms 12952 KB Output is correct
20 Correct 590 ms 14744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 308 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 304 KB Output is correct
11 Correct 568 ms 15032 KB Output is correct
12 Correct 617 ms 14692 KB Output is correct
13 Correct 494 ms 13932 KB Output is correct
14 Correct 529 ms 14024 KB Output is correct
15 Correct 719 ms 15024 KB Output is correct
16 Correct 551 ms 14140 KB Output is correct
17 Correct 540 ms 14176 KB Output is correct
18 Correct 521 ms 16016 KB Output is correct
19 Correct 473 ms 12952 KB Output is correct
20 Correct 590 ms 14744 KB Output is correct
21 Correct 591 ms 17976 KB Output is correct
22 Correct 761 ms 18448 KB Output is correct
23 Correct 638 ms 17144 KB Output is correct
24 Correct 644 ms 17024 KB Output is correct
25 Correct 704 ms 19056 KB Output is correct
26 Correct 684 ms 17488 KB Output is correct
27 Correct 746 ms 17504 KB Output is correct
28 Correct 521 ms 20000 KB Output is correct
29 Correct 572 ms 16016 KB Output is correct
30 Correct 655 ms 18180 KB Output is correct
31 Correct 806 ms 18080 KB Output is correct
32 Correct 696 ms 18040 KB Output is correct
33 Correct 604 ms 16956 KB Output is correct
34 Correct 701 ms 16992 KB Output is correct
35 Correct 672 ms 17428 KB Output is correct
36 Correct 517 ms 16092 KB Output is correct
37 Correct 767 ms 18104 KB Output is correct
38 Correct 599 ms 16032 KB Output is correct
39 Correct 641 ms 17200 KB Output is correct
40 Correct 678 ms 17152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 308 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 304 KB Output is correct
11 Correct 134 ms 14904 KB Output is correct
12 Correct 140 ms 14904 KB Output is correct
13 Correct 193 ms 14848 KB Output is correct
14 Correct 163 ms 14776 KB Output is correct
15 Correct 136 ms 14924 KB Output is correct
16 Correct 186 ms 14772 KB Output is correct
17 Correct 242 ms 14776 KB Output is correct
18 Correct 127 ms 15032 KB Output is correct
19 Correct 136 ms 14768 KB Output is correct
20 Correct 132 ms 14916 KB Output is correct
21 Correct 191 ms 14804 KB Output is correct
22 Correct 169 ms 14780 KB Output is correct
23 Correct 141 ms 14796 KB Output is correct
24 Correct 167 ms 14780 KB Output is correct
25 Correct 222 ms 14816 KB Output is correct
26 Correct 134 ms 14720 KB Output is correct
27 Correct 136 ms 14904 KB Output is correct
28 Correct 171 ms 14648 KB Output is correct
29 Correct 201 ms 14780 KB Output is correct
30 Correct 147 ms 14860 KB Output is correct
31 Correct 146 ms 14768 KB Output is correct
32 Correct 177 ms 14868 KB Output is correct
33 Correct 152 ms 14776 KB Output is correct
34 Correct 127 ms 14644 KB Output is correct
35 Correct 156 ms 14856 KB Output is correct
36 Correct 160 ms 14796 KB Output is correct
37 Correct 191 ms 14924 KB Output is correct
38 Correct 147 ms 14772 KB Output is correct
39 Correct 184 ms 14776 KB Output is correct
40 Correct 154 ms 14776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 308 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 304 KB Output is correct
11 Correct 568 ms 15032 KB Output is correct
12 Correct 617 ms 14692 KB Output is correct
13 Correct 494 ms 13932 KB Output is correct
14 Correct 529 ms 14024 KB Output is correct
15 Correct 719 ms 15024 KB Output is correct
16 Correct 551 ms 14140 KB Output is correct
17 Correct 540 ms 14176 KB Output is correct
18 Correct 521 ms 16016 KB Output is correct
19 Correct 473 ms 12952 KB Output is correct
20 Correct 590 ms 14744 KB Output is correct
21 Correct 591 ms 17976 KB Output is correct
22 Correct 761 ms 18448 KB Output is correct
23 Correct 638 ms 17144 KB Output is correct
24 Correct 644 ms 17024 KB Output is correct
25 Correct 704 ms 19056 KB Output is correct
26 Correct 684 ms 17488 KB Output is correct
27 Correct 746 ms 17504 KB Output is correct
28 Correct 521 ms 20000 KB Output is correct
29 Correct 572 ms 16016 KB Output is correct
30 Correct 655 ms 18180 KB Output is correct
31 Correct 806 ms 18080 KB Output is correct
32 Correct 696 ms 18040 KB Output is correct
33 Correct 604 ms 16956 KB Output is correct
34 Correct 701 ms 16992 KB Output is correct
35 Correct 672 ms 17428 KB Output is correct
36 Correct 517 ms 16092 KB Output is correct
37 Correct 767 ms 18104 KB Output is correct
38 Correct 599 ms 16032 KB Output is correct
39 Correct 641 ms 17200 KB Output is correct
40 Correct 678 ms 17152 KB Output is correct
41 Correct 134 ms 14904 KB Output is correct
42 Correct 140 ms 14904 KB Output is correct
43 Correct 193 ms 14848 KB Output is correct
44 Correct 163 ms 14776 KB Output is correct
45 Correct 136 ms 14924 KB Output is correct
46 Correct 186 ms 14772 KB Output is correct
47 Correct 242 ms 14776 KB Output is correct
48 Correct 127 ms 15032 KB Output is correct
49 Correct 136 ms 14768 KB Output is correct
50 Correct 132 ms 14916 KB Output is correct
51 Correct 191 ms 14804 KB Output is correct
52 Correct 169 ms 14780 KB Output is correct
53 Correct 141 ms 14796 KB Output is correct
54 Correct 167 ms 14780 KB Output is correct
55 Correct 222 ms 14816 KB Output is correct
56 Correct 134 ms 14720 KB Output is correct
57 Correct 136 ms 14904 KB Output is correct
58 Correct 171 ms 14648 KB Output is correct
59 Correct 201 ms 14780 KB Output is correct
60 Correct 147 ms 14860 KB Output is correct
61 Correct 146 ms 14768 KB Output is correct
62 Correct 177 ms 14868 KB Output is correct
63 Correct 152 ms 14776 KB Output is correct
64 Correct 127 ms 14644 KB Output is correct
65 Correct 156 ms 14856 KB Output is correct
66 Correct 160 ms 14796 KB Output is correct
67 Correct 191 ms 14924 KB Output is correct
68 Correct 147 ms 14772 KB Output is correct
69 Correct 184 ms 14776 KB Output is correct
70 Correct 154 ms 14776 KB Output is correct
71 Correct 1044 ms 39060 KB Output is correct
72 Correct 1036 ms 39320 KB Output is correct
73 Correct 1134 ms 37788 KB Output is correct
74 Correct 1538 ms 38096 KB Output is correct
75 Correct 1092 ms 40164 KB Output is correct
76 Correct 1586 ms 38676 KB Output is correct
77 Correct 1592 ms 38360 KB Output is correct
78 Correct 898 ms 42212 KB Output is correct
79 Correct 1002 ms 36148 KB Output is correct
80 Correct 1050 ms 39348 KB Output is correct
81 Correct 1291 ms 39164 KB Output is correct
82 Correct 1676 ms 38200 KB Output is correct
83 Correct 1161 ms 37376 KB Output is correct
84 Correct 1497 ms 38136 KB Output is correct
85 Correct 1657 ms 38300 KB Output is correct
86 Correct 881 ms 36124 KB Output is correct
87 Correct 992 ms 39176 KB Output is correct
88 Correct 971 ms 36196 KB Output is correct
89 Correct 1085 ms 37780 KB Output is correct
90 Correct 1236 ms 38196 KB Output is correct
91 Correct 978 ms 37316 KB Output is correct
92 Correct 1639 ms 38708 KB Output is correct
93 Correct 1338 ms 38400 KB Output is correct
94 Correct 837 ms 36176 KB Output is correct
95 Correct 1249 ms 38388 KB Output is correct
96 Correct 1309 ms 38420 KB Output is correct
97 Correct 1302 ms 38196 KB Output is correct
98 Correct 1316 ms 38308 KB Output is correct
99 Correct 1187 ms 38208 KB Output is correct
100 Correct 1297 ms 38196 KB Output is correct