#include <bits/stdc++.h>
using namespace std;
const int maxn = 20;
const int maxsz = 1<<20;
int x[maxsz];
int sos[maxsz];
int soo[maxsz];
int dp[maxn][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>& posq)
{
if(tpos == posq.size())
{
return (c0%2 == 0 ? 1 : -1)*sos[sv];
}
else
{
return rec0(sv,c0+1,tpos+1,posq) + rec0(sv+(1<<posq[tpos]),c0,tpos+1,posq);
}
}
int rec1(int sv,int c1,int tpos,vector<int>& posq)
{
if(tpos == posq.size())
{
return (c1%2 == 0 ? 1 : -1)*soo[sv];
}
else
{
return rec1(sv,c1,tpos+1,posq) + rec1(sv+(1<<posq[tpos]),c1+1,tpos+1,posq);
}
}
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[i][j] = x[j];
}
else
{
dp[i][j] = ((j&(1<<(i-1))) == 0 ? dp[i-1][j] : dp[i-1][j] + dp[i-1][j^(1<<(i-1))]);
}
}
}
for(int i = 0;i < sz;++i)
{
sos[i] = dp[n][i];
}
for(int i = 0;i <= n;++i)
{
for(int j = 0;j < sz;++j)
{
if(i == 0)
{
dp[i][j] = x[j];
}
else
{
dp[i][j] = ((j&(1<<(i-1))) != 0 ? dp[i-1][j] : dp[i-1][j] + dp[i-1][j^(1<<(i-1))]);
}
}
}
for(int i = 0;i < sz;++i)
{
soo[i] = dp[n][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:15:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | if(tpos == posq.size())
| ~~~~~^~~~~~~~~~~~~~
snake_escaping.cpp: In function 'int rec0(int, int, int, std::vector<int>&)':
snake_escaping.cpp:26:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | if(tpos == posq.size())
| ~~~~~^~~~~~~~~~~~~~
snake_escaping.cpp: In function 'int rec1(int, int, int, std::vector<int>&)':
snake_escaping.cpp:37:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | if(tpos == posq.size())
| ~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
26972 KB |
Output is correct |
2 |
Correct |
4 ms |
26972 KB |
Output is correct |
3 |
Correct |
5 ms |
26972 KB |
Output is correct |
4 |
Correct |
4 ms |
26972 KB |
Output is correct |
5 |
Correct |
4 ms |
26972 KB |
Output is correct |
6 |
Correct |
4 ms |
27100 KB |
Output is correct |
7 |
Correct |
4 ms |
27040 KB |
Output is correct |
8 |
Correct |
3 ms |
26972 KB |
Output is correct |
9 |
Correct |
4 ms |
26972 KB |
Output is correct |
10 |
Correct |
4 ms |
26972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
26972 KB |
Output is correct |
2 |
Correct |
4 ms |
26972 KB |
Output is correct |
3 |
Correct |
5 ms |
26972 KB |
Output is correct |
4 |
Correct |
4 ms |
26972 KB |
Output is correct |
5 |
Correct |
4 ms |
26972 KB |
Output is correct |
6 |
Correct |
4 ms |
27100 KB |
Output is correct |
7 |
Correct |
4 ms |
27040 KB |
Output is correct |
8 |
Correct |
3 ms |
26972 KB |
Output is correct |
9 |
Correct |
4 ms |
26972 KB |
Output is correct |
10 |
Correct |
4 ms |
26972 KB |
Output is correct |
11 |
Correct |
509 ms |
41316 KB |
Output is correct |
12 |
Correct |
486 ms |
41024 KB |
Output is correct |
13 |
Correct |
337 ms |
40148 KB |
Output is correct |
14 |
Correct |
352 ms |
40260 KB |
Output is correct |
15 |
Correct |
590 ms |
41192 KB |
Output is correct |
16 |
Correct |
414 ms |
40764 KB |
Output is correct |
17 |
Correct |
394 ms |
40360 KB |
Output is correct |
18 |
Correct |
205 ms |
42144 KB |
Output is correct |
19 |
Correct |
202 ms |
39136 KB |
Output is correct |
20 |
Correct |
477 ms |
40884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
26972 KB |
Output is correct |
2 |
Correct |
4 ms |
26972 KB |
Output is correct |
3 |
Correct |
5 ms |
26972 KB |
Output is correct |
4 |
Correct |
4 ms |
26972 KB |
Output is correct |
5 |
Correct |
4 ms |
26972 KB |
Output is correct |
6 |
Correct |
4 ms |
27100 KB |
Output is correct |
7 |
Correct |
4 ms |
27040 KB |
Output is correct |
8 |
Correct |
3 ms |
26972 KB |
Output is correct |
9 |
Correct |
4 ms |
26972 KB |
Output is correct |
10 |
Correct |
4 ms |
26972 KB |
Output is correct |
11 |
Correct |
509 ms |
41316 KB |
Output is correct |
12 |
Correct |
486 ms |
41024 KB |
Output is correct |
13 |
Correct |
337 ms |
40148 KB |
Output is correct |
14 |
Correct |
352 ms |
40260 KB |
Output is correct |
15 |
Correct |
590 ms |
41192 KB |
Output is correct |
16 |
Correct |
414 ms |
40764 KB |
Output is correct |
17 |
Correct |
394 ms |
40360 KB |
Output is correct |
18 |
Correct |
205 ms |
42144 KB |
Output is correct |
19 |
Correct |
202 ms |
39136 KB |
Output is correct |
20 |
Correct |
477 ms |
40884 KB |
Output is correct |
21 |
Correct |
321 ms |
50124 KB |
Output is correct |
22 |
Correct |
645 ms |
50260 KB |
Output is correct |
23 |
Correct |
440 ms |
49308 KB |
Output is correct |
24 |
Correct |
445 ms |
48976 KB |
Output is correct |
25 |
Correct |
464 ms |
50928 KB |
Output is correct |
26 |
Correct |
564 ms |
49664 KB |
Output is correct |
27 |
Correct |
493 ms |
49500 KB |
Output is correct |
28 |
Correct |
255 ms |
51908 KB |
Output is correct |
29 |
Correct |
225 ms |
47956 KB |
Output is correct |
30 |
Correct |
474 ms |
50072 KB |
Output is correct |
31 |
Correct |
664 ms |
50032 KB |
Output is correct |
32 |
Correct |
543 ms |
50000 KB |
Output is correct |
33 |
Correct |
367 ms |
48804 KB |
Output is correct |
34 |
Correct |
476 ms |
49132 KB |
Output is correct |
35 |
Correct |
492 ms |
49748 KB |
Output is correct |
36 |
Correct |
226 ms |
47956 KB |
Output is correct |
37 |
Correct |
664 ms |
50100 KB |
Output is correct |
38 |
Correct |
223 ms |
48076 KB |
Output is correct |
39 |
Correct |
441 ms |
49232 KB |
Output is correct |
40 |
Correct |
440 ms |
49092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
26972 KB |
Output is correct |
2 |
Correct |
4 ms |
26972 KB |
Output is correct |
3 |
Correct |
5 ms |
26972 KB |
Output is correct |
4 |
Correct |
4 ms |
26972 KB |
Output is correct |
5 |
Correct |
4 ms |
26972 KB |
Output is correct |
6 |
Correct |
4 ms |
27100 KB |
Output is correct |
7 |
Correct |
4 ms |
27040 KB |
Output is correct |
8 |
Correct |
3 ms |
26972 KB |
Output is correct |
9 |
Correct |
4 ms |
26972 KB |
Output is correct |
10 |
Correct |
4 ms |
26972 KB |
Output is correct |
11 |
Runtime error |
53 ms |
65536 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
26972 KB |
Output is correct |
2 |
Correct |
4 ms |
26972 KB |
Output is correct |
3 |
Correct |
5 ms |
26972 KB |
Output is correct |
4 |
Correct |
4 ms |
26972 KB |
Output is correct |
5 |
Correct |
4 ms |
26972 KB |
Output is correct |
6 |
Correct |
4 ms |
27100 KB |
Output is correct |
7 |
Correct |
4 ms |
27040 KB |
Output is correct |
8 |
Correct |
3 ms |
26972 KB |
Output is correct |
9 |
Correct |
4 ms |
26972 KB |
Output is correct |
10 |
Correct |
4 ms |
26972 KB |
Output is correct |
11 |
Correct |
509 ms |
41316 KB |
Output is correct |
12 |
Correct |
486 ms |
41024 KB |
Output is correct |
13 |
Correct |
337 ms |
40148 KB |
Output is correct |
14 |
Correct |
352 ms |
40260 KB |
Output is correct |
15 |
Correct |
590 ms |
41192 KB |
Output is correct |
16 |
Correct |
414 ms |
40764 KB |
Output is correct |
17 |
Correct |
394 ms |
40360 KB |
Output is correct |
18 |
Correct |
205 ms |
42144 KB |
Output is correct |
19 |
Correct |
202 ms |
39136 KB |
Output is correct |
20 |
Correct |
477 ms |
40884 KB |
Output is correct |
21 |
Correct |
321 ms |
50124 KB |
Output is correct |
22 |
Correct |
645 ms |
50260 KB |
Output is correct |
23 |
Correct |
440 ms |
49308 KB |
Output is correct |
24 |
Correct |
445 ms |
48976 KB |
Output is correct |
25 |
Correct |
464 ms |
50928 KB |
Output is correct |
26 |
Correct |
564 ms |
49664 KB |
Output is correct |
27 |
Correct |
493 ms |
49500 KB |
Output is correct |
28 |
Correct |
255 ms |
51908 KB |
Output is correct |
29 |
Correct |
225 ms |
47956 KB |
Output is correct |
30 |
Correct |
474 ms |
50072 KB |
Output is correct |
31 |
Correct |
664 ms |
50032 KB |
Output is correct |
32 |
Correct |
543 ms |
50000 KB |
Output is correct |
33 |
Correct |
367 ms |
48804 KB |
Output is correct |
34 |
Correct |
476 ms |
49132 KB |
Output is correct |
35 |
Correct |
492 ms |
49748 KB |
Output is correct |
36 |
Correct |
226 ms |
47956 KB |
Output is correct |
37 |
Correct |
664 ms |
50100 KB |
Output is correct |
38 |
Correct |
223 ms |
48076 KB |
Output is correct |
39 |
Correct |
441 ms |
49232 KB |
Output is correct |
40 |
Correct |
440 ms |
49092 KB |
Output is correct |
41 |
Runtime error |
53 ms |
65536 KB |
Execution killed with signal 9 |
42 |
Halted |
0 ms |
0 KB |
- |