Submission #949619

# Submission time Handle Problem Language Result Execution time Memory
949619 2024-03-19T12:38:35 Z ace5 Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1060 ms 57052 KB
#include <bits/stdc++.h>

using namespace std;

const int maxn = 21;
const int maxsz = 1<<21;

int x[maxsz];
int sos[maxsz];
int soo[maxsz];
int dp[maxsz];
int dp2[maxsz];

int recq(int sv,int tpos,vector<int>& posq)
{
    if(tpos == posq.size())
    {
        return x[sv];
    }
    else
    {
        return recq(sv,tpos+1,posq) + recq(sv+(1<<posq[tpos]),tpos+1,posq);
    }
}
int rec0(int sv,int c0,int tpos,vector<int>& pos1)
{
    if(tpos == pos1.size())
    {
        return (c0%2 == 0 ? 1 : -1)*sos[sv];
    }
    else
    {
        return rec0(sv,c0+1,tpos+1,pos1) + rec0(sv+(1<<pos1[tpos]),c0,tpos+1,pos1);
    }
}
int rec1(int sv,int c1,int tpos,vector<int>& pos0)
{
    if(tpos == pos0.size())
    {
        return (c1%2 == 0 ? 1 : -1)*soo[sv];
    }
    else
    {
        return rec1(sv,c1,tpos+1,pos0) + rec1(sv+(1<<pos0[tpos]),c1+1,tpos+1,pos0);
    }
}


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n,q;
    cin >> n >> q;
    int sz = 1<<n;
    for(int j = 0;j < sz;++j)
    {
        char c;
        cin >> c;
        x[j] = c-'0';
    }
    for(int i = 0;i <= n;++i)
    {
        for(int j = 0;j < sz;++j)
        {
            if(i == 0)
            {
                dp[j] = x[j];
            }
            else
            {
                dp[j] = ((j&(1<<(i-1))) == 0 ? dp2[j] : dp2[j] + dp2[j^(1<<(i-1))]);
            }
        }
        for(int j = 0;j < sz;++j)
            dp2[j] = dp[j];
    }
    for(int i = 0;i < sz;++i)
    {
        sos[i] = dp[i];
    }
    for(int i = 0;i <= n;++i)
    {
        for(int j = 0;j < sz;++j)
        {
            if(i == 0)
            {
                dp[j] = x[j];
            }
            else
            {
                dp[j] = ((j&(1<<(i-1))) != 0 ? dp2[j] : dp2[j] + dp2[j^(1<<(i-1))]);
            }
        }
        for(int j = 0;j < sz;++j)
            dp2[j] = dp[j];
    }
    for(int i = 0;i < sz;++i)
    {
        soo[i] = dp[i];
    }
    while(q--)
    {
        vector<char> c(n);
        int c0 = 0,c1 = 0,cq = 0;
        for(int j = 0;j < n;++j)
        {
            cin >> c[j];
            if(c[j] == '?')
                cq++;
            else if(c[j] == '0')
                c0++;
            else
                c1++;
        }
        if(cq <= 6)
        {
            int sv = 0;
            vector<int> posq;
            for(int j = 0;j < n;++j)
            {
                if(c[j] == '?')
                    posq.push_back(n-j-1);
                sv*=2;
                sv += (c[j] == '1' ? 1 : 0);
            }
            cout << recq(sv,0,posq) << "\n";
        }
        else if(c0 <= 6)
        {
            int sv = 0;
            vector<int> pos0;
            for(int j = 0;j < n;++j)
            {
                if(c[j] == '0')
                    pos0.push_back(n-j-1);
                sv*=2;
                sv += (c[j] == '1' ? 1 : 0);
            }
            cout << rec1(sv,0,0,pos0) << "\n";
        }
        else
        {
            int sv = 0;
            vector<int> pos1;
            for(int j = 0;j < n;++j)
            {
                if(c[j] == '1')
                    pos1.push_back(n-j-1);
                sv*=2;
                sv += (c[j] == '?' ? 1 : 0);
            }
            cout << rec0(sv,0,0,pos1) << "\n";
        }
    }
}

Compilation message

snake_escaping.cpp: In function 'int recq(int, int, std::vector<int>&)':
snake_escaping.cpp:16:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |     if(tpos == posq.size())
      |        ~~~~~^~~~~~~~~~~~~~
snake_escaping.cpp: In function 'int rec0(int, int, int, std::vector<int>&)':
snake_escaping.cpp:27:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |     if(tpos == pos1.size())
      |        ~~~~~^~~~~~~~~~~~~~
snake_escaping.cpp: In function 'int rec1(int, int, int, std::vector<int>&)':
snake_escaping.cpp:38:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |     if(tpos == pos0.size())
      |        ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8684 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 1 ms 8536 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8684 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 1 ms 8536 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
11 Correct 491 ms 12644 KB Output is correct
12 Correct 487 ms 12316 KB Output is correct
13 Correct 321 ms 11532 KB Output is correct
14 Correct 342 ms 11500 KB Output is correct
15 Correct 583 ms 12508 KB Output is correct
16 Correct 398 ms 11692 KB Output is correct
17 Correct 383 ms 11604 KB Output is correct
18 Correct 187 ms 13648 KB Output is correct
19 Correct 195 ms 10652 KB Output is correct
20 Correct 451 ms 12456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8684 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 1 ms 8536 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
11 Correct 491 ms 12644 KB Output is correct
12 Correct 487 ms 12316 KB Output is correct
13 Correct 321 ms 11532 KB Output is correct
14 Correct 342 ms 11500 KB Output is correct
15 Correct 583 ms 12508 KB Output is correct
16 Correct 398 ms 11692 KB Output is correct
17 Correct 383 ms 11604 KB Output is correct
18 Correct 187 ms 13648 KB Output is correct
19 Correct 195 ms 10652 KB Output is correct
20 Correct 451 ms 12456 KB Output is correct
21 Correct 298 ms 12880 KB Output is correct
22 Correct 620 ms 12844 KB Output is correct
23 Correct 442 ms 11840 KB Output is correct
24 Correct 456 ms 11816 KB Output is correct
25 Correct 415 ms 13652 KB Output is correct
26 Correct 503 ms 12116 KB Output is correct
27 Correct 501 ms 12116 KB Output is correct
28 Correct 217 ms 14676 KB Output is correct
29 Correct 210 ms 10580 KB Output is correct
30 Correct 456 ms 12624 KB Output is correct
31 Correct 657 ms 12640 KB Output is correct
32 Correct 533 ms 12488 KB Output is correct
33 Correct 371 ms 11580 KB Output is correct
34 Correct 435 ms 11604 KB Output is correct
35 Correct 485 ms 12112 KB Output is correct
36 Correct 211 ms 10716 KB Output is correct
37 Correct 662 ms 12608 KB Output is correct
38 Correct 213 ms 10580 KB Output is correct
39 Correct 427 ms 11860 KB Output is correct
40 Correct 461 ms 11700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8684 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 1 ms 8536 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
11 Correct 79 ms 31568 KB Output is correct
12 Correct 84 ms 31632 KB Output is correct
13 Correct 88 ms 31576 KB Output is correct
14 Correct 104 ms 31572 KB Output is correct
15 Correct 86 ms 31640 KB Output is correct
16 Correct 102 ms 31368 KB Output is correct
17 Correct 98 ms 31568 KB Output is correct
18 Correct 70 ms 31572 KB Output is correct
19 Correct 76 ms 31316 KB Output is correct
20 Correct 79 ms 31572 KB Output is correct
21 Correct 96 ms 31568 KB Output is correct
22 Correct 105 ms 31568 KB Output is correct
23 Correct 92 ms 31520 KB Output is correct
24 Correct 100 ms 31532 KB Output is correct
25 Correct 107 ms 31824 KB Output is correct
26 Correct 79 ms 31304 KB Output is correct
27 Correct 85 ms 31516 KB Output is correct
28 Correct 77 ms 31316 KB Output is correct
29 Correct 91 ms 31532 KB Output is correct
30 Correct 100 ms 31576 KB Output is correct
31 Correct 81 ms 31320 KB Output is correct
32 Correct 105 ms 31568 KB Output is correct
33 Correct 95 ms 31572 KB Output is correct
34 Correct 71 ms 31284 KB Output is correct
35 Correct 91 ms 31572 KB Output is correct
36 Correct 102 ms 31360 KB Output is correct
37 Correct 90 ms 31360 KB Output is correct
38 Correct 93 ms 31568 KB Output is correct
39 Correct 91 ms 31572 KB Output is correct
40 Correct 103 ms 31352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8684 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 1 ms 8536 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
11 Correct 491 ms 12644 KB Output is correct
12 Correct 487 ms 12316 KB Output is correct
13 Correct 321 ms 11532 KB Output is correct
14 Correct 342 ms 11500 KB Output is correct
15 Correct 583 ms 12508 KB Output is correct
16 Correct 398 ms 11692 KB Output is correct
17 Correct 383 ms 11604 KB Output is correct
18 Correct 187 ms 13648 KB Output is correct
19 Correct 195 ms 10652 KB Output is correct
20 Correct 451 ms 12456 KB Output is correct
21 Correct 298 ms 12880 KB Output is correct
22 Correct 620 ms 12844 KB Output is correct
23 Correct 442 ms 11840 KB Output is correct
24 Correct 456 ms 11816 KB Output is correct
25 Correct 415 ms 13652 KB Output is correct
26 Correct 503 ms 12116 KB Output is correct
27 Correct 501 ms 12116 KB Output is correct
28 Correct 217 ms 14676 KB Output is correct
29 Correct 210 ms 10580 KB Output is correct
30 Correct 456 ms 12624 KB Output is correct
31 Correct 657 ms 12640 KB Output is correct
32 Correct 533 ms 12488 KB Output is correct
33 Correct 371 ms 11580 KB Output is correct
34 Correct 435 ms 11604 KB Output is correct
35 Correct 485 ms 12112 KB Output is correct
36 Correct 211 ms 10716 KB Output is correct
37 Correct 662 ms 12608 KB Output is correct
38 Correct 213 ms 10580 KB Output is correct
39 Correct 427 ms 11860 KB Output is correct
40 Correct 461 ms 11700 KB Output is correct
41 Correct 79 ms 31568 KB Output is correct
42 Correct 84 ms 31632 KB Output is correct
43 Correct 88 ms 31576 KB Output is correct
44 Correct 104 ms 31572 KB Output is correct
45 Correct 86 ms 31640 KB Output is correct
46 Correct 102 ms 31368 KB Output is correct
47 Correct 98 ms 31568 KB Output is correct
48 Correct 70 ms 31572 KB Output is correct
49 Correct 76 ms 31316 KB Output is correct
50 Correct 79 ms 31572 KB Output is correct
51 Correct 96 ms 31568 KB Output is correct
52 Correct 105 ms 31568 KB Output is correct
53 Correct 92 ms 31520 KB Output is correct
54 Correct 100 ms 31532 KB Output is correct
55 Correct 107 ms 31824 KB Output is correct
56 Correct 79 ms 31304 KB Output is correct
57 Correct 85 ms 31516 KB Output is correct
58 Correct 77 ms 31316 KB Output is correct
59 Correct 91 ms 31532 KB Output is correct
60 Correct 100 ms 31576 KB Output is correct
61 Correct 81 ms 31320 KB Output is correct
62 Correct 105 ms 31568 KB Output is correct
63 Correct 95 ms 31572 KB Output is correct
64 Correct 71 ms 31284 KB Output is correct
65 Correct 91 ms 31572 KB Output is correct
66 Correct 102 ms 31360 KB Output is correct
67 Correct 90 ms 31360 KB Output is correct
68 Correct 93 ms 31568 KB Output is correct
69 Correct 91 ms 31572 KB Output is correct
70 Correct 103 ms 31352 KB Output is correct
71 Correct 516 ms 54092 KB Output is correct
72 Correct 562 ms 54096 KB Output is correct
73 Correct 677 ms 52736 KB Output is correct
74 Correct 987 ms 52972 KB Output is correct
75 Correct 659 ms 55012 KB Output is correct
76 Correct 925 ms 53588 KB Output is correct
77 Correct 1060 ms 53116 KB Output is correct
78 Correct 354 ms 57052 KB Output is correct
79 Correct 346 ms 50932 KB Output is correct
80 Correct 511 ms 53804 KB Output is correct
81 Correct 826 ms 53736 KB Output is correct
82 Correct 982 ms 52768 KB Output is correct
83 Correct 581 ms 51796 KB Output is correct
84 Correct 905 ms 52760 KB Output is correct
85 Correct 918 ms 53128 KB Output is correct
86 Correct 346 ms 52228 KB Output is correct
87 Correct 535 ms 55044 KB Output is correct
88 Correct 355 ms 52052 KB Output is correct
89 Correct 707 ms 53752 KB Output is correct
90 Correct 877 ms 53928 KB Output is correct
91 Correct 583 ms 53228 KB Output is correct
92 Correct 1044 ms 54424 KB Output is correct
93 Correct 953 ms 54248 KB Output is correct
94 Correct 337 ms 52088 KB Output is correct
95 Correct 831 ms 54200 KB Output is correct
96 Correct 829 ms 54012 KB Output is correct
97 Correct 802 ms 54144 KB Output is correct
98 Correct 835 ms 54144 KB Output is correct
99 Correct 827 ms 54160 KB Output is correct
100 Correct 778 ms 54196 KB Output is correct