답안 #285764

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
285764 2020-08-29T14:50:08 Z glikcakbn Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1886 ms 42488 KB
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int L, T; cin >> L >> T;
    int n = (1 << L);

    int A[n];
    for(int i = 0; i < n; ++i)
    {
        char c; cin >> c;
        A[i] = c - '0';
    }

    int B[n]; for(int i = 0; i < n; ++i) B[i] = A[i];
    for(int i = 0; i < L; ++i)
    {
        for(int j = 0; j < n; ++j)
            if(j >> i & 1) B[j] += B[j ^ (1 << i)];
    }

    int C[n]; for(int i = 0; i < n; ++i) C[i] = A[i];
    for(int i = 0; i < L; ++i)
    {
        for(int j = 0; j < n; ++j)
            if(j >> i & 1) C[j ^ (1 << i)] += C[j];
    }

//    for(auto i : A) cout << i << ' '; cout << endl;
//    for(auto i : B) cout << i << ' '; cout << endl;
//    for(auto i : C) cout << i << ' '; cout << endl;

    while(T--)
    {
        int a = 0, b = 0, c = 0;
        int arr[L];
        for(int i = L - 1; i >= 0; --i)
        {
            char t; cin >> t;
            if(t == '0') arr[i] = 0, ++c;
            else if(t == '1') arr[i] = 1, ++b;
            else arr[i] = 2, ++a;
        }

        if(a <= b && a <= c)
        {
            int N = 1 << a;
            vector<int> bt; int bs = 0;
            for(int i = 0; i < L; ++i)
            {
                if(arr[i] == 2) bt.push_back(1 << i);
                else if(arr[i] == 1) bs |= (1 << i);
            }

            int ans = A[bs];
            for(int i = 1; i < N; ++i)
            {
                int t = __builtin_ctz(i);
                for(int j = 0; j < t; ++j) bs ^= bt[j];
                bs ^= bt[t];
                ans += A[bs];
            }
            cout << ans << '\n';
        }

        else if(b <= a && b <= c)
        {
            int N = 1 << b;
            vector<int> bt; int bs = 0;
            for(int i = 0; i < L; ++i)
            {
                if(arr[i] == 1) bt.push_back(1 << i), bs |= (1 << i);
                else if(arr[i] == 2) bs |= (1 << i);
            }

            long long ans = B[bs];
            for(int i = 1; i < N; ++i)
            {
                int t = __builtin_ctz(i);
                for(int j = 0; j < t; ++j) bs ^= bt[j];
                bs ^= bt[t];
                if(__builtin_parity(i)) ans -= B[bs];
                else ans += B[bs];
            }
            cout << ans << '\n';
        }

        else
        {
            int N = 1 << c;
            vector<int> bt; int bs = 0;
            for(int i = 0; i < L; ++i)
            {
                if(arr[i] == 0) bt.push_back(1 << i);
                else if(arr[i] == 1) bs |= (1 << i);
            }

            long long ans = C[bs];
            for(int i = 1; i < N; ++i)
            {
                int t = __builtin_ctz(i);
                for(int j = 0; j < t; ++j) bs ^= bt[j];
                bs ^= bt[t];
                if(__builtin_parity(i)) ans -= C[bs];
                else ans += C[bs];
            }
            cout << ans << '\n';
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 308 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 308 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 344 ms 15096 KB Output is correct
12 Correct 357 ms 14968 KB Output is correct
13 Correct 481 ms 14116 KB Output is correct
14 Correct 554 ms 14200 KB Output is correct
15 Correct 409 ms 15224 KB Output is correct
16 Correct 530 ms 14328 KB Output is correct
17 Correct 505 ms 14456 KB Output is correct
18 Correct 284 ms 16124 KB Output is correct
19 Correct 337 ms 13176 KB Output is correct
20 Correct 344 ms 14840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 308 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 344 ms 15096 KB Output is correct
12 Correct 357 ms 14968 KB Output is correct
13 Correct 481 ms 14116 KB Output is correct
14 Correct 554 ms 14200 KB Output is correct
15 Correct 409 ms 15224 KB Output is correct
16 Correct 530 ms 14328 KB Output is correct
17 Correct 505 ms 14456 KB Output is correct
18 Correct 284 ms 16124 KB Output is correct
19 Correct 337 ms 13176 KB Output is correct
20 Correct 344 ms 14840 KB Output is correct
21 Correct 460 ms 18232 KB Output is correct
22 Correct 428 ms 18400 KB Output is correct
23 Correct 636 ms 17448 KB Output is correct
24 Correct 686 ms 17144 KB Output is correct
25 Correct 502 ms 19320 KB Output is correct
26 Correct 662 ms 17696 KB Output is correct
27 Correct 629 ms 17656 KB Output is correct
28 Correct 335 ms 20216 KB Output is correct
29 Correct 418 ms 16208 KB Output is correct
30 Correct 422 ms 18424 KB Output is correct
31 Correct 607 ms 18204 KB Output is correct
32 Correct 697 ms 18296 KB Output is correct
33 Correct 514 ms 17032 KB Output is correct
34 Correct 659 ms 17272 KB Output is correct
35 Correct 638 ms 17596 KB Output is correct
36 Correct 322 ms 16120 KB Output is correct
37 Correct 426 ms 18164 KB Output is correct
38 Correct 415 ms 16248 KB Output is correct
39 Correct 612 ms 17404 KB Output is correct
40 Correct 685 ms 17104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 308 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 117 ms 15096 KB Output is correct
12 Correct 128 ms 14992 KB Output is correct
13 Correct 141 ms 14932 KB Output is correct
14 Correct 162 ms 14968 KB Output is correct
15 Correct 121 ms 15024 KB Output is correct
16 Correct 180 ms 14968 KB Output is correct
17 Correct 160 ms 14968 KB Output is correct
18 Correct 103 ms 15096 KB Output is correct
19 Correct 118 ms 14792 KB Output is correct
20 Correct 122 ms 14968 KB Output is correct
21 Correct 136 ms 14968 KB Output is correct
22 Correct 160 ms 14916 KB Output is correct
23 Correct 123 ms 14968 KB Output is correct
24 Correct 164 ms 14968 KB Output is correct
25 Correct 160 ms 14968 KB Output is correct
26 Correct 105 ms 14840 KB Output is correct
27 Correct 117 ms 14968 KB Output is correct
28 Correct 115 ms 14840 KB Output is correct
29 Correct 134 ms 14840 KB Output is correct
30 Correct 154 ms 14888 KB Output is correct
31 Correct 126 ms 14968 KB Output is correct
32 Correct 167 ms 14968 KB Output is correct
33 Correct 157 ms 14968 KB Output is correct
34 Correct 106 ms 14840 KB Output is correct
35 Correct 144 ms 14968 KB Output is correct
36 Correct 141 ms 15096 KB Output is correct
37 Correct 145 ms 14968 KB Output is correct
38 Correct 145 ms 14968 KB Output is correct
39 Correct 145 ms 14968 KB Output is correct
40 Correct 141 ms 15044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 308 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 344 ms 15096 KB Output is correct
12 Correct 357 ms 14968 KB Output is correct
13 Correct 481 ms 14116 KB Output is correct
14 Correct 554 ms 14200 KB Output is correct
15 Correct 409 ms 15224 KB Output is correct
16 Correct 530 ms 14328 KB Output is correct
17 Correct 505 ms 14456 KB Output is correct
18 Correct 284 ms 16124 KB Output is correct
19 Correct 337 ms 13176 KB Output is correct
20 Correct 344 ms 14840 KB Output is correct
21 Correct 460 ms 18232 KB Output is correct
22 Correct 428 ms 18400 KB Output is correct
23 Correct 636 ms 17448 KB Output is correct
24 Correct 686 ms 17144 KB Output is correct
25 Correct 502 ms 19320 KB Output is correct
26 Correct 662 ms 17696 KB Output is correct
27 Correct 629 ms 17656 KB Output is correct
28 Correct 335 ms 20216 KB Output is correct
29 Correct 418 ms 16208 KB Output is correct
30 Correct 422 ms 18424 KB Output is correct
31 Correct 607 ms 18204 KB Output is correct
32 Correct 697 ms 18296 KB Output is correct
33 Correct 514 ms 17032 KB Output is correct
34 Correct 659 ms 17272 KB Output is correct
35 Correct 638 ms 17596 KB Output is correct
36 Correct 322 ms 16120 KB Output is correct
37 Correct 426 ms 18164 KB Output is correct
38 Correct 415 ms 16248 KB Output is correct
39 Correct 612 ms 17404 KB Output is correct
40 Correct 685 ms 17104 KB Output is correct
41 Correct 117 ms 15096 KB Output is correct
42 Correct 128 ms 14992 KB Output is correct
43 Correct 141 ms 14932 KB Output is correct
44 Correct 162 ms 14968 KB Output is correct
45 Correct 121 ms 15024 KB Output is correct
46 Correct 180 ms 14968 KB Output is correct
47 Correct 160 ms 14968 KB Output is correct
48 Correct 103 ms 15096 KB Output is correct
49 Correct 118 ms 14792 KB Output is correct
50 Correct 122 ms 14968 KB Output is correct
51 Correct 136 ms 14968 KB Output is correct
52 Correct 160 ms 14916 KB Output is correct
53 Correct 123 ms 14968 KB Output is correct
54 Correct 164 ms 14968 KB Output is correct
55 Correct 160 ms 14968 KB Output is correct
56 Correct 105 ms 14840 KB Output is correct
57 Correct 117 ms 14968 KB Output is correct
58 Correct 115 ms 14840 KB Output is correct
59 Correct 134 ms 14840 KB Output is correct
60 Correct 154 ms 14888 KB Output is correct
61 Correct 126 ms 14968 KB Output is correct
62 Correct 167 ms 14968 KB Output is correct
63 Correct 157 ms 14968 KB Output is correct
64 Correct 106 ms 14840 KB Output is correct
65 Correct 144 ms 14968 KB Output is correct
66 Correct 141 ms 15096 KB Output is correct
67 Correct 145 ms 14968 KB Output is correct
68 Correct 145 ms 14968 KB Output is correct
69 Correct 145 ms 14968 KB Output is correct
70 Correct 141 ms 15044 KB Output is correct
71 Correct 703 ms 39188 KB Output is correct
72 Correct 742 ms 39424 KB Output is correct
73 Correct 1218 ms 37880 KB Output is correct
74 Correct 1630 ms 38392 KB Output is correct
75 Correct 889 ms 40316 KB Output is correct
76 Correct 1886 ms 38632 KB Output is correct
77 Correct 1589 ms 38520 KB Output is correct
78 Correct 528 ms 42488 KB Output is correct
79 Correct 721 ms 36472 KB Output is correct
80 Correct 768 ms 39608 KB Output is correct
81 Correct 1243 ms 39416 KB Output is correct
82 Correct 1860 ms 38356 KB Output is correct
83 Correct 866 ms 37632 KB Output is correct
84 Correct 1625 ms 38420 KB Output is correct
85 Correct 1571 ms 38520 KB Output is correct
86 Correct 515 ms 36344 KB Output is correct
87 Correct 742 ms 39268 KB Output is correct
88 Correct 743 ms 36216 KB Output is correct
89 Correct 1131 ms 38068 KB Output is correct
90 Correct 1391 ms 38372 KB Output is correct
91 Correct 930 ms 37544 KB Output is correct
92 Correct 1798 ms 38520 KB Output is correct
93 Correct 1581 ms 38392 KB Output is correct
94 Correct 513 ms 36248 KB Output is correct
95 Correct 1284 ms 38264 KB Output is correct
96 Correct 1337 ms 38648 KB Output is correct
97 Correct 1323 ms 38392 KB Output is correct
98 Correct 1290 ms 38264 KB Output is correct
99 Correct 1277 ms 38408 KB Output is correct
100 Correct 1292 ms 38408 KB Output is correct