#include <bits/stdc++.h>
using namespace std;
//#define FILE_IO
/// Try 2
int N, Q;
int cnt[3];
int val[1100005], dp[1100005], dp2[1100005];
int bts[1100005];
int B, b[25];
char s[1100005];
int ans;
void bck0(int pas, int msk, int smn)
{
if(pas >= B)
{
ans += smn * dp[msk];
return;
}
bck0(pas + 1, msk, smn);
bck0(pas + 1, msk ^ (1 << b[pas]), -smn);
}
void bck1(int pas, int msk, int smn)
{
if(pas >= B)
{
ans += smn * dp2[msk];
return;
}
bck1(pas + 1, msk, smn);
bck1(pas + 1, msk ^ (1 << b[pas]), -smn);
}
void bckq(int pas, int msk)
{
if(pas >= B)
{
ans += val[msk];
return;
}
bckq(pas + 1, msk);
bckq(pas + 1, msk ^ (1 << b[pas]));
}
int main()
{
#ifdef FILE_IO
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
scanf("%d%d\n", &N, &Q);
scanf("%s\n", s);
for(int i = 0; i < (1 << N); i++)
val[i] = dp[i] = dp2[i] = s[i] - '0';
for(int j = 0; j < N; j++)
for(int i = (1 << N) - 1; i >= 0; i--)
if( (i & (1 << j)) == 0 )
dp[i] += dp[i ^ (1 << j)];
for(int j = 0; j < N; j++)
for(int i = 0; i < (1 << N); i++)
if( (i & (1 << j)) != 0 )
dp2[i] += dp2[i ^ (1 << j)];
for(int i = 1; i <= Q; i++)
{
scanf("%s\n", s);
reverse(s, s + N);
cnt[0] = cnt[1] = cnt[2] = 0;
for(int j = 0; j < N; j++)
{
if(s[j] == '0') cnt[0]++;
else if(s[j] == '1') cnt[1]++;
else if(s[j] == '?') cnt[2]++;
}
if(cnt[0] < cnt[1] && cnt[0] < cnt[2])
{
int msk = 0;
B = 0;
for(int i = 0; i < N; i++)
{
if(s[i] == '1') msk |= (1 << i);
if(s[i] == '0') b[B++] = i;
}
ans = 0;
bck0(0, msk, 1);
printf("%d\n", ans);
}
else if(cnt[1] < cnt[2])
{
int msk = 0;
B = 0;
for(int i = 0; i < N; i++)
{
if(s[i] == '1')
{
msk |= (1 << i);
b[B++] = i;
}
if(s[i] == '?')
msk |= (1 << i);
}
ans = 0;
bck1(0, msk, 1);
printf("%d\n", ans);
}
else
{
int msk = 0;
B = 0;
for(int i = 0; i < N; i++)
{
if(s[i] == '?') b[B++] = i;
else msk |= ((s[i] - '0') << i);
}
ans = 0;
bckq(0, msk);
printf("%d\n", ans);
}
}
return 0;
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:59:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d\n", &N, &Q);
~~~~~^~~~~~~~~~~~~~~~~~
snake_escaping.cpp:60:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s\n", s);
~~~~~^~~~~~~~~~~
snake_escaping.cpp:74:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s\n", s);
~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
564 KB |
Output is correct |
3 |
Correct |
2 ms |
564 KB |
Output is correct |
4 |
Correct |
2 ms |
564 KB |
Output is correct |
5 |
Correct |
2 ms |
564 KB |
Output is correct |
6 |
Correct |
3 ms |
652 KB |
Output is correct |
7 |
Correct |
2 ms |
652 KB |
Output is correct |
8 |
Correct |
2 ms |
652 KB |
Output is correct |
9 |
Correct |
2 ms |
716 KB |
Output is correct |
10 |
Correct |
2 ms |
732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
564 KB |
Output is correct |
3 |
Correct |
2 ms |
564 KB |
Output is correct |
4 |
Correct |
2 ms |
564 KB |
Output is correct |
5 |
Correct |
2 ms |
564 KB |
Output is correct |
6 |
Correct |
3 ms |
652 KB |
Output is correct |
7 |
Correct |
2 ms |
652 KB |
Output is correct |
8 |
Correct |
2 ms |
652 KB |
Output is correct |
9 |
Correct |
2 ms |
716 KB |
Output is correct |
10 |
Correct |
2 ms |
732 KB |
Output is correct |
11 |
Correct |
382 ms |
15544 KB |
Output is correct |
12 |
Correct |
359 ms |
18720 KB |
Output is correct |
13 |
Correct |
474 ms |
18720 KB |
Output is correct |
14 |
Correct |
430 ms |
18720 KB |
Output is correct |
15 |
Correct |
423 ms |
19104 KB |
Output is correct |
16 |
Correct |
489 ms |
19104 KB |
Output is correct |
17 |
Correct |
456 ms |
29016 KB |
Output is correct |
18 |
Correct |
293 ms |
41524 KB |
Output is correct |
19 |
Correct |
331 ms |
49468 KB |
Output is correct |
20 |
Correct |
406 ms |
61780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
564 KB |
Output is correct |
3 |
Correct |
2 ms |
564 KB |
Output is correct |
4 |
Correct |
2 ms |
564 KB |
Output is correct |
5 |
Correct |
2 ms |
564 KB |
Output is correct |
6 |
Correct |
3 ms |
652 KB |
Output is correct |
7 |
Correct |
2 ms |
652 KB |
Output is correct |
8 |
Correct |
2 ms |
652 KB |
Output is correct |
9 |
Correct |
2 ms |
716 KB |
Output is correct |
10 |
Correct |
2 ms |
732 KB |
Output is correct |
11 |
Correct |
382 ms |
15544 KB |
Output is correct |
12 |
Correct |
359 ms |
18720 KB |
Output is correct |
13 |
Correct |
474 ms |
18720 KB |
Output is correct |
14 |
Correct |
430 ms |
18720 KB |
Output is correct |
15 |
Correct |
423 ms |
19104 KB |
Output is correct |
16 |
Correct |
489 ms |
19104 KB |
Output is correct |
17 |
Correct |
456 ms |
29016 KB |
Output is correct |
18 |
Correct |
293 ms |
41524 KB |
Output is correct |
19 |
Correct |
331 ms |
49468 KB |
Output is correct |
20 |
Correct |
406 ms |
61780 KB |
Output is correct |
21 |
Runtime error |
468 ms |
66560 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
564 KB |
Output is correct |
3 |
Correct |
2 ms |
564 KB |
Output is correct |
4 |
Correct |
2 ms |
564 KB |
Output is correct |
5 |
Correct |
2 ms |
564 KB |
Output is correct |
6 |
Correct |
3 ms |
652 KB |
Output is correct |
7 |
Correct |
2 ms |
652 KB |
Output is correct |
8 |
Correct |
2 ms |
652 KB |
Output is correct |
9 |
Correct |
2 ms |
716 KB |
Output is correct |
10 |
Correct |
2 ms |
732 KB |
Output is correct |
11 |
Runtime error |
107 ms |
66560 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
564 KB |
Output is correct |
3 |
Correct |
2 ms |
564 KB |
Output is correct |
4 |
Correct |
2 ms |
564 KB |
Output is correct |
5 |
Correct |
2 ms |
564 KB |
Output is correct |
6 |
Correct |
3 ms |
652 KB |
Output is correct |
7 |
Correct |
2 ms |
652 KB |
Output is correct |
8 |
Correct |
2 ms |
652 KB |
Output is correct |
9 |
Correct |
2 ms |
716 KB |
Output is correct |
10 |
Correct |
2 ms |
732 KB |
Output is correct |
11 |
Correct |
382 ms |
15544 KB |
Output is correct |
12 |
Correct |
359 ms |
18720 KB |
Output is correct |
13 |
Correct |
474 ms |
18720 KB |
Output is correct |
14 |
Correct |
430 ms |
18720 KB |
Output is correct |
15 |
Correct |
423 ms |
19104 KB |
Output is correct |
16 |
Correct |
489 ms |
19104 KB |
Output is correct |
17 |
Correct |
456 ms |
29016 KB |
Output is correct |
18 |
Correct |
293 ms |
41524 KB |
Output is correct |
19 |
Correct |
331 ms |
49468 KB |
Output is correct |
20 |
Correct |
406 ms |
61780 KB |
Output is correct |
21 |
Runtime error |
468 ms |
66560 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
22 |
Halted |
0 ms |
0 KB |
- |