Submission #1106006

# Submission time Handle Problem Language Result Execution time Memory
1106006 2024-10-28T17:50:20 Z 0x34c Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1282 ms 52712 KB
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define endl '\n'

using namespace std;

const int MX_L = 20;
int bm0[1 << MX_L][2], bm1[1 << MX_L][2], val[1 << MX_L];

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int L, Q;
    cin >> L >> Q;

    string s;
    cin >> s;

    // in bm0, 1 = ?; in bm1, 0 = ?
    int prv = 0, cur = 1;
    for (int i = 0; i < s.size(); i++)
    {
        val[i] = s[i] - '0';
        bm0[i][prv] = bm1[i][prv] = val[i];
    }

    for (int j = 1; j <= L; j++)
    {
        for (int i = 0; i < (1 << L); i++)
        {
            if (i & (1 << (j - 1)))
            {
                bm0[i][cur] = bm0[i][prv] + bm0[i ^ (1 << (j - 1))][prv];
                bm1[i][cur] = bm1[i][prv];
            }
            else
            {
                bm0[i][cur] = bm0[i][prv];
                bm1[i][cur] = bm1[i][prv] + bm1[i ^ (1 << (j - 1))][prv];
            }
        }
        swap(cur, prv);
    }
    swap(cur, prv);

    while (Q--)
    {
        string t;
        cin >> t;

        int sz = t.size() - 1;

        int cnt_0 = 0, cnt_1 = 0, cnt_q = 0;
        vector<int> p0, p1, pq;
        for (int i = 0; i < t.size(); i++)
            if (t[i] == '0')
            {
                cnt_0++;
                p0.push_back(i);
            }
            else if (t[i] == '1')
            {
                cnt_1++;
                p1.push_back(i);
            }
            else
            {
                cnt_q++;
                pq.push_back(i);
            }

        int mn = min({cnt_0, cnt_1, cnt_q});
        if (mn == cnt_q)
        {
            int bit = 0;
            for (int j = 0; j < t.size(); j++)
            {
                if (t[j] == '1')
                    bit |= (1 << (sz - j));
            }

            int res = 0;
            for (int add = 0; add < (1 << cnt_q); add++)
            {
                int bit2 = bit;
                for (int j = 0; j < cnt_q; j++)
                {
                    if (add & (1 << j))
                        bit2 |= (1 << (sz - pq[j]));
                }

                res += val[bit2];
            }

            cout << res << endl;
        }
        else if (mn == cnt_0)
        {
            // we do PIE
            int bit = 0;
            for (int j = 0; j < t.size(); j++)
            {
                if (t[j] == '1')
                    bit |= (1 << (sz - j));
            }

            int res = 0;
            for (int add = 0; add < (1 << cnt_0); add++)
            {
                int bit2 = bit;
                int cnt = __builtin_popcount(add);
                for (int j = 0; j < cnt_0; j++)
                {
                    if (add & (1 << j))
                        bit2 |= (1 << (sz - p0[j]));
                }

                res += (cnt % 2 == 1 ? -1 : 1) * bm1[bit2][cur];
            }

            cout << res << endl;
        }
        else
        {
            // we do PIE too
            int bit = 0;
            for (int j = 0; j < t.size(); j++)
            {
                if (t[j] == '1' || t[j] == '?')
                    bit |= (1 << (sz - j));
            }

            int res = 0;
            for (int add = 0; add < (1 << cnt_1); add++)
            {
                int bit2 = bit;
                int cnt = __builtin_popcount(add);
                for (int j = 0; j < cnt_1; j++)
                {
                    if (add & (1 << j))
                        bit2 ^= (1 << (sz - p1[j]));
                }

                res += (cnt % 2 == 1 ? -1 : 1) * bm0[bit2][cur];
            }

            cout << res << endl;
        }
    }
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:24:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |     for (int i = 0; i < s.size(); i++)
      |                     ~~^~~~~~~~~~
snake_escaping.cpp:58:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |         for (int i = 0; i < t.size(); i++)
      |                         ~~^~~~~~~~~~
snake_escaping.cpp:79:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |             for (int j = 0; j < t.size(); j++)
      |                             ~~^~~~~~~~~~
snake_escaping.cpp:104:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  104 |             for (int j = 0; j < t.size(); j++)
      |                             ~~^~~~~~~~~~
snake_escaping.cpp:130:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  130 |             for (int j = 0; j < t.size(); j++)
      |                             ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Correct 1 ms 4432 KB Output is correct
3 Correct 1 ms 4432 KB Output is correct
4 Correct 1 ms 4568 KB Output is correct
5 Correct 1 ms 4432 KB Output is correct
6 Correct 2 ms 4432 KB Output is correct
7 Correct 2 ms 4432 KB Output is correct
8 Correct 1 ms 4432 KB Output is correct
9 Correct 1 ms 4432 KB Output is correct
10 Correct 2 ms 4432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Correct 1 ms 4432 KB Output is correct
3 Correct 1 ms 4432 KB Output is correct
4 Correct 1 ms 4568 KB Output is correct
5 Correct 1 ms 4432 KB Output is correct
6 Correct 2 ms 4432 KB Output is correct
7 Correct 2 ms 4432 KB Output is correct
8 Correct 1 ms 4432 KB Output is correct
9 Correct 1 ms 4432 KB Output is correct
10 Correct 2 ms 4432 KB Output is correct
11 Correct 320 ms 8548 KB Output is correct
12 Correct 323 ms 8008 KB Output is correct
13 Correct 389 ms 7496 KB Output is correct
14 Correct 377 ms 7412 KB Output is correct
15 Correct 354 ms 8520 KB Output is correct
16 Correct 405 ms 7752 KB Output is correct
17 Correct 413 ms 7496 KB Output is correct
18 Correct 203 ms 9360 KB Output is correct
19 Correct 290 ms 6472 KB Output is correct
20 Correct 304 ms 8036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Correct 1 ms 4432 KB Output is correct
3 Correct 1 ms 4432 KB Output is correct
4 Correct 1 ms 4568 KB Output is correct
5 Correct 1 ms 4432 KB Output is correct
6 Correct 2 ms 4432 KB Output is correct
7 Correct 2 ms 4432 KB Output is correct
8 Correct 1 ms 4432 KB Output is correct
9 Correct 1 ms 4432 KB Output is correct
10 Correct 2 ms 4432 KB Output is correct
11 Correct 320 ms 8548 KB Output is correct
12 Correct 323 ms 8008 KB Output is correct
13 Correct 389 ms 7496 KB Output is correct
14 Correct 377 ms 7412 KB Output is correct
15 Correct 354 ms 8520 KB Output is correct
16 Correct 405 ms 7752 KB Output is correct
17 Correct 413 ms 7496 KB Output is correct
18 Correct 203 ms 9360 KB Output is correct
19 Correct 290 ms 6472 KB Output is correct
20 Correct 304 ms 8036 KB Output is correct
21 Correct 375 ms 8520 KB Output is correct
22 Correct 401 ms 8776 KB Output is correct
23 Correct 506 ms 7768 KB Output is correct
24 Correct 508 ms 7496 KB Output is correct
25 Correct 421 ms 9552 KB Output is correct
26 Correct 555 ms 7920 KB Output is correct
27 Correct 529 ms 7840 KB Output is correct
28 Correct 222 ms 10568 KB Output is correct
29 Correct 358 ms 6472 KB Output is correct
30 Correct 412 ms 8776 KB Output is correct
31 Correct 503 ms 8520 KB Output is correct
32 Correct 544 ms 8520 KB Output is correct
33 Correct 402 ms 7496 KB Output is correct
34 Correct 515 ms 7496 KB Output is correct
35 Correct 533 ms 8008 KB Output is correct
36 Correct 210 ms 6480 KB Output is correct
37 Correct 360 ms 8636 KB Output is correct
38 Correct 376 ms 6472 KB Output is correct
39 Correct 479 ms 7612 KB Output is correct
40 Correct 508 ms 7596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Correct 1 ms 4432 KB Output is correct
3 Correct 1 ms 4432 KB Output is correct
4 Correct 1 ms 4568 KB Output is correct
5 Correct 1 ms 4432 KB Output is correct
6 Correct 2 ms 4432 KB Output is correct
7 Correct 2 ms 4432 KB Output is correct
8 Correct 1 ms 4432 KB Output is correct
9 Correct 1 ms 4432 KB Output is correct
10 Correct 2 ms 4432 KB Output is correct
11 Correct 62 ms 25324 KB Output is correct
12 Correct 57 ms 25320 KB Output is correct
13 Correct 70 ms 25536 KB Output is correct
14 Correct 92 ms 25180 KB Output is correct
15 Correct 63 ms 25320 KB Output is correct
16 Correct 83 ms 25324 KB Output is correct
17 Correct 74 ms 25216 KB Output is correct
18 Correct 42 ms 25432 KB Output is correct
19 Correct 57 ms 25068 KB Output is correct
20 Correct 56 ms 25436 KB Output is correct
21 Correct 69 ms 25320 KB Output is correct
22 Correct 85 ms 25184 KB Output is correct
23 Correct 60 ms 25116 KB Output is correct
24 Correct 91 ms 25184 KB Output is correct
25 Correct 85 ms 25340 KB Output is correct
26 Correct 43 ms 25132 KB Output is correct
27 Correct 57 ms 25320 KB Output is correct
28 Correct 62 ms 25060 KB Output is correct
29 Correct 76 ms 25184 KB Output is correct
30 Correct 87 ms 25576 KB Output is correct
31 Correct 60 ms 25320 KB Output is correct
32 Correct 104 ms 25332 KB Output is correct
33 Correct 81 ms 25320 KB Output is correct
34 Correct 41 ms 25076 KB Output is correct
35 Correct 74 ms 25320 KB Output is correct
36 Correct 69 ms 25332 KB Output is correct
37 Correct 71 ms 25324 KB Output is correct
38 Correct 75 ms 25320 KB Output is correct
39 Correct 92 ms 25192 KB Output is correct
40 Correct 73 ms 25320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Correct 1 ms 4432 KB Output is correct
3 Correct 1 ms 4432 KB Output is correct
4 Correct 1 ms 4568 KB Output is correct
5 Correct 1 ms 4432 KB Output is correct
6 Correct 2 ms 4432 KB Output is correct
7 Correct 2 ms 4432 KB Output is correct
8 Correct 1 ms 4432 KB Output is correct
9 Correct 1 ms 4432 KB Output is correct
10 Correct 2 ms 4432 KB Output is correct
11 Correct 320 ms 8548 KB Output is correct
12 Correct 323 ms 8008 KB Output is correct
13 Correct 389 ms 7496 KB Output is correct
14 Correct 377 ms 7412 KB Output is correct
15 Correct 354 ms 8520 KB Output is correct
16 Correct 405 ms 7752 KB Output is correct
17 Correct 413 ms 7496 KB Output is correct
18 Correct 203 ms 9360 KB Output is correct
19 Correct 290 ms 6472 KB Output is correct
20 Correct 304 ms 8036 KB Output is correct
21 Correct 375 ms 8520 KB Output is correct
22 Correct 401 ms 8776 KB Output is correct
23 Correct 506 ms 7768 KB Output is correct
24 Correct 508 ms 7496 KB Output is correct
25 Correct 421 ms 9552 KB Output is correct
26 Correct 555 ms 7920 KB Output is correct
27 Correct 529 ms 7840 KB Output is correct
28 Correct 222 ms 10568 KB Output is correct
29 Correct 358 ms 6472 KB Output is correct
30 Correct 412 ms 8776 KB Output is correct
31 Correct 503 ms 8520 KB Output is correct
32 Correct 544 ms 8520 KB Output is correct
33 Correct 402 ms 7496 KB Output is correct
34 Correct 515 ms 7496 KB Output is correct
35 Correct 533 ms 8008 KB Output is correct
36 Correct 210 ms 6480 KB Output is correct
37 Correct 360 ms 8636 KB Output is correct
38 Correct 376 ms 6472 KB Output is correct
39 Correct 479 ms 7612 KB Output is correct
40 Correct 508 ms 7596 KB Output is correct
41 Correct 62 ms 25324 KB Output is correct
42 Correct 57 ms 25320 KB Output is correct
43 Correct 70 ms 25536 KB Output is correct
44 Correct 92 ms 25180 KB Output is correct
45 Correct 63 ms 25320 KB Output is correct
46 Correct 83 ms 25324 KB Output is correct
47 Correct 74 ms 25216 KB Output is correct
48 Correct 42 ms 25432 KB Output is correct
49 Correct 57 ms 25068 KB Output is correct
50 Correct 56 ms 25436 KB Output is correct
51 Correct 69 ms 25320 KB Output is correct
52 Correct 85 ms 25184 KB Output is correct
53 Correct 60 ms 25116 KB Output is correct
54 Correct 91 ms 25184 KB Output is correct
55 Correct 85 ms 25340 KB Output is correct
56 Correct 43 ms 25132 KB Output is correct
57 Correct 57 ms 25320 KB Output is correct
58 Correct 62 ms 25060 KB Output is correct
59 Correct 76 ms 25184 KB Output is correct
60 Correct 87 ms 25576 KB Output is correct
61 Correct 60 ms 25320 KB Output is correct
62 Correct 104 ms 25332 KB Output is correct
63 Correct 81 ms 25320 KB Output is correct
64 Correct 41 ms 25076 KB Output is correct
65 Correct 74 ms 25320 KB Output is correct
66 Correct 69 ms 25332 KB Output is correct
67 Correct 71 ms 25324 KB Output is correct
68 Correct 75 ms 25320 KB Output is correct
69 Correct 92 ms 25192 KB Output is correct
70 Correct 73 ms 25320 KB Output is correct
71 Correct 597 ms 49588 KB Output is correct
72 Correct 695 ms 49824 KB Output is correct
73 Correct 811 ms 48104 KB Output is correct
74 Correct 1282 ms 48616 KB Output is correct
75 Correct 649 ms 50604 KB Output is correct
76 Correct 1211 ms 48988 KB Output is correct
77 Correct 1184 ms 48816 KB Output is correct
78 Correct 327 ms 52712 KB Output is correct
79 Correct 599 ms 46564 KB Output is correct
80 Correct 647 ms 49640 KB Output is correct
81 Correct 960 ms 49640 KB Output is correct
82 Correct 1255 ms 48504 KB Output is correct
83 Correct 680 ms 47848 KB Output is correct
84 Correct 1186 ms 48616 KB Output is correct
85 Correct 1032 ms 48872 KB Output is correct
86 Correct 294 ms 46572 KB Output is correct
87 Correct 650 ms 49524 KB Output is correct
88 Correct 638 ms 46484 KB Output is correct
89 Correct 825 ms 48332 KB Output is correct
90 Correct 1105 ms 48368 KB Output is correct
91 Correct 642 ms 47848 KB Output is correct
92 Correct 1174 ms 48872 KB Output is correct
93 Correct 1132 ms 48648 KB Output is correct
94 Correct 342 ms 46568 KB Output is correct
95 Correct 929 ms 48584 KB Output is correct
96 Correct 861 ms 48576 KB Output is correct
97 Correct 1016 ms 48588 KB Output is correct
98 Correct 911 ms 48572 KB Output is correct
99 Correct 996 ms 48540 KB Output is correct
100 Correct 1088 ms 48648 KB Output is correct