#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast", "unroll-loops")
using namespace std;
#define ll long long
#define int ll
#define FOR(i,a,b) for (int i = (a); i<(b); i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n+1)
#define RREP(i,n) for (int i=(n)-1; i>=0; i--)
#define RREP1(i,n) for (int i=(n); i>=1; i--)
#define f first
#define s second
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)(x.size())
#define SQ(x) (x)*(x)
#define pii pair<int, int>
#define pdd pair<double ,double>
#define pcc pair<char, char>
#define endl '\n'
//#define TOAD
#ifdef TOAD
#define bug(x) cerr<<__LINE__<<": "<<#x<<" is "<<x<<endl
#define IOS()
#else
#define bug(...)
#define IOS() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#endif
const ll inf = 1ll<<60;
const int iinf=2147483647;
const ll mod = 1e9+7;
const ll maxn=3005;
const double PI=acos(-1);
ll pw(ll x, ll p, ll m=mod){
ll ret=1;
while (p>0){
if (p&1){
ret*=x;
ret%=m;
}
x*=x;
x%=m;
p>>=1;
}
return ret;
}
ll inv(ll a, ll m=mod){
return pw(a,m-2);
}
//=======================================================================================
int n, res=0;
vector<int> a, b, c;
signed main (){
IOS();
int n,q; cin>>n>>q;
int n2=(1<<n);
vector<int> dp(1<<n), rdp(1<<n), val(1<<n);
vector<bool> par(1<<n);
REP(i,(1<<n)){
par[i]=__builtin_popcount(i)%2;
}
string str; cin>>str;
REP(i, (1<<n)){
val[i]=str[i]-'0';
dp[i]=val[i];
rdp[i]=val[i];
}
REP(i, n){
REP(j, (1<<n)){
if (j&(1<<i)){
dp[j]+=dp[j-(1<<i)];
}
}
}
REP(i, n){
RREP(j, (1<<n)){
if ((j&(1<<i))==0){
rdp[j]+=rdp[j+(1<<i)];
}
}
}
REP(i, q){
a.clear(); b.clear(); c.clear();
res=0;
string in; cin>>in;
reverse(ALL(in));
int ta=0, tb=0, tc=0;
REP(j, SZ(in)){
if (in[j]=='?') {
a.pb(j);
ta+=(1<<j);
}
else if (in[j]=='0') {
b.pb(j);
tb+=(1<<j);
}
else {
c.pb(j);
tc+=(1<<j);
}
}
if (SZ(a)<=n/3){
for (int j=ta; j>0; j=(j-1)&ta){
res+=val[j+tc];
}
res+=val[tc];
cout<<res<<endl;
}
else if (SZ(c)<=n/3){
for (int j=tc; j>0; j=(j-1)&tc){
res+=dp[j+ta]*(par[j+ta]*2-1);
}
res+=dp[ta]*(par[ta]*2-1);
res=abs(res);
cout<<res<<endl;
}
else {
bug(rdp[2]);
bug(rdp[6]);
for (int j=tb; j>0; j=(j-1)&tb){
res+=rdp[j+tc]*(par[j+tc]*2-1);
}
res+=rdp[tc]*(par[tc]*2-1);
res=abs(res);
cout<<res<<endl;
}
}
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:60:9: warning: unused variable 'n2' [-Wunused-variable]
60 | int n2=(1<<n);
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
238 ms |
15016 KB |
Output is correct |
12 |
Correct |
242 ms |
14668 KB |
Output is correct |
13 |
Correct |
252 ms |
13936 KB |
Output is correct |
14 |
Correct |
275 ms |
14112 KB |
Output is correct |
15 |
Correct |
243 ms |
14992 KB |
Output is correct |
16 |
Correct |
263 ms |
14176 KB |
Output is correct |
17 |
Correct |
274 ms |
14156 KB |
Output is correct |
18 |
Correct |
189 ms |
16080 KB |
Output is correct |
19 |
Correct |
225 ms |
13004 KB |
Output is correct |
20 |
Correct |
269 ms |
14708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
238 ms |
15016 KB |
Output is correct |
12 |
Correct |
242 ms |
14668 KB |
Output is correct |
13 |
Correct |
252 ms |
13936 KB |
Output is correct |
14 |
Correct |
275 ms |
14112 KB |
Output is correct |
15 |
Correct |
243 ms |
14992 KB |
Output is correct |
16 |
Correct |
263 ms |
14176 KB |
Output is correct |
17 |
Correct |
274 ms |
14156 KB |
Output is correct |
18 |
Correct |
189 ms |
16080 KB |
Output is correct |
19 |
Correct |
225 ms |
13004 KB |
Output is correct |
20 |
Correct |
269 ms |
14708 KB |
Output is correct |
21 |
Correct |
267 ms |
18236 KB |
Output is correct |
22 |
Correct |
300 ms |
18296 KB |
Output is correct |
23 |
Correct |
306 ms |
17392 KB |
Output is correct |
24 |
Correct |
300 ms |
17200 KB |
Output is correct |
25 |
Correct |
289 ms |
19164 KB |
Output is correct |
26 |
Correct |
322 ms |
17652 KB |
Output is correct |
27 |
Correct |
325 ms |
17624 KB |
Output is correct |
28 |
Correct |
209 ms |
20204 KB |
Output is correct |
29 |
Correct |
258 ms |
16180 KB |
Output is correct |
30 |
Correct |
279 ms |
18384 KB |
Output is correct |
31 |
Correct |
317 ms |
18148 KB |
Output is correct |
32 |
Correct |
343 ms |
18320 KB |
Output is correct |
33 |
Correct |
274 ms |
17124 KB |
Output is correct |
34 |
Correct |
304 ms |
17128 KB |
Output is correct |
35 |
Correct |
337 ms |
17632 KB |
Output is correct |
36 |
Correct |
191 ms |
16124 KB |
Output is correct |
37 |
Correct |
256 ms |
18272 KB |
Output is correct |
38 |
Correct |
276 ms |
16148 KB |
Output is correct |
39 |
Correct |
302 ms |
17364 KB |
Output is correct |
40 |
Correct |
299 ms |
17228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
82 ms |
28672 KB |
Output is correct |
12 |
Correct |
77 ms |
28504 KB |
Output is correct |
13 |
Correct |
86 ms |
28508 KB |
Output is correct |
14 |
Correct |
95 ms |
28396 KB |
Output is correct |
15 |
Correct |
78 ms |
28528 KB |
Output is correct |
16 |
Correct |
101 ms |
28500 KB |
Output is correct |
17 |
Correct |
98 ms |
28500 KB |
Output is correct |
18 |
Correct |
68 ms |
28596 KB |
Output is correct |
19 |
Correct |
74 ms |
28488 KB |
Output is correct |
20 |
Correct |
90 ms |
28528 KB |
Output is correct |
21 |
Correct |
94 ms |
28532 KB |
Output is correct |
22 |
Correct |
103 ms |
28404 KB |
Output is correct |
23 |
Correct |
86 ms |
28496 KB |
Output is correct |
24 |
Correct |
91 ms |
28520 KB |
Output is correct |
25 |
Correct |
98 ms |
28516 KB |
Output is correct |
26 |
Correct |
69 ms |
28452 KB |
Output is correct |
27 |
Correct |
77 ms |
28524 KB |
Output is correct |
28 |
Correct |
96 ms |
28520 KB |
Output is correct |
29 |
Correct |
82 ms |
28496 KB |
Output is correct |
30 |
Correct |
93 ms |
28532 KB |
Output is correct |
31 |
Correct |
75 ms |
28412 KB |
Output is correct |
32 |
Correct |
111 ms |
28492 KB |
Output is correct |
33 |
Correct |
102 ms |
28508 KB |
Output is correct |
34 |
Correct |
73 ms |
28508 KB |
Output is correct |
35 |
Correct |
86 ms |
28524 KB |
Output is correct |
36 |
Correct |
87 ms |
28532 KB |
Output is correct |
37 |
Correct |
92 ms |
28516 KB |
Output is correct |
38 |
Correct |
93 ms |
28508 KB |
Output is correct |
39 |
Correct |
89 ms |
28484 KB |
Output is correct |
40 |
Correct |
88 ms |
28524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
238 ms |
15016 KB |
Output is correct |
12 |
Correct |
242 ms |
14668 KB |
Output is correct |
13 |
Correct |
252 ms |
13936 KB |
Output is correct |
14 |
Correct |
275 ms |
14112 KB |
Output is correct |
15 |
Correct |
243 ms |
14992 KB |
Output is correct |
16 |
Correct |
263 ms |
14176 KB |
Output is correct |
17 |
Correct |
274 ms |
14156 KB |
Output is correct |
18 |
Correct |
189 ms |
16080 KB |
Output is correct |
19 |
Correct |
225 ms |
13004 KB |
Output is correct |
20 |
Correct |
269 ms |
14708 KB |
Output is correct |
21 |
Correct |
267 ms |
18236 KB |
Output is correct |
22 |
Correct |
300 ms |
18296 KB |
Output is correct |
23 |
Correct |
306 ms |
17392 KB |
Output is correct |
24 |
Correct |
300 ms |
17200 KB |
Output is correct |
25 |
Correct |
289 ms |
19164 KB |
Output is correct |
26 |
Correct |
322 ms |
17652 KB |
Output is correct |
27 |
Correct |
325 ms |
17624 KB |
Output is correct |
28 |
Correct |
209 ms |
20204 KB |
Output is correct |
29 |
Correct |
258 ms |
16180 KB |
Output is correct |
30 |
Correct |
279 ms |
18384 KB |
Output is correct |
31 |
Correct |
317 ms |
18148 KB |
Output is correct |
32 |
Correct |
343 ms |
18320 KB |
Output is correct |
33 |
Correct |
274 ms |
17124 KB |
Output is correct |
34 |
Correct |
304 ms |
17128 KB |
Output is correct |
35 |
Correct |
337 ms |
17632 KB |
Output is correct |
36 |
Correct |
191 ms |
16124 KB |
Output is correct |
37 |
Correct |
256 ms |
18272 KB |
Output is correct |
38 |
Correct |
276 ms |
16148 KB |
Output is correct |
39 |
Correct |
302 ms |
17364 KB |
Output is correct |
40 |
Correct |
299 ms |
17228 KB |
Output is correct |
41 |
Correct |
82 ms |
28672 KB |
Output is correct |
42 |
Correct |
77 ms |
28504 KB |
Output is correct |
43 |
Correct |
86 ms |
28508 KB |
Output is correct |
44 |
Correct |
95 ms |
28396 KB |
Output is correct |
45 |
Correct |
78 ms |
28528 KB |
Output is correct |
46 |
Correct |
101 ms |
28500 KB |
Output is correct |
47 |
Correct |
98 ms |
28500 KB |
Output is correct |
48 |
Correct |
68 ms |
28596 KB |
Output is correct |
49 |
Correct |
74 ms |
28488 KB |
Output is correct |
50 |
Correct |
90 ms |
28528 KB |
Output is correct |
51 |
Correct |
94 ms |
28532 KB |
Output is correct |
52 |
Correct |
103 ms |
28404 KB |
Output is correct |
53 |
Correct |
86 ms |
28496 KB |
Output is correct |
54 |
Correct |
91 ms |
28520 KB |
Output is correct |
55 |
Correct |
98 ms |
28516 KB |
Output is correct |
56 |
Correct |
69 ms |
28452 KB |
Output is correct |
57 |
Correct |
77 ms |
28524 KB |
Output is correct |
58 |
Correct |
96 ms |
28520 KB |
Output is correct |
59 |
Correct |
82 ms |
28496 KB |
Output is correct |
60 |
Correct |
93 ms |
28532 KB |
Output is correct |
61 |
Correct |
75 ms |
28412 KB |
Output is correct |
62 |
Correct |
111 ms |
28492 KB |
Output is correct |
63 |
Correct |
102 ms |
28508 KB |
Output is correct |
64 |
Correct |
73 ms |
28508 KB |
Output is correct |
65 |
Correct |
86 ms |
28524 KB |
Output is correct |
66 |
Correct |
87 ms |
28532 KB |
Output is correct |
67 |
Correct |
92 ms |
28516 KB |
Output is correct |
68 |
Correct |
93 ms |
28508 KB |
Output is correct |
69 |
Correct |
89 ms |
28484 KB |
Output is correct |
70 |
Correct |
88 ms |
28524 KB |
Output is correct |
71 |
Correct |
455 ms |
52904 KB |
Output is correct |
72 |
Correct |
493 ms |
52860 KB |
Output is correct |
73 |
Correct |
558 ms |
51444 KB |
Output is correct |
74 |
Correct |
981 ms |
51832 KB |
Output is correct |
75 |
Correct |
491 ms |
53880 KB |
Output is correct |
76 |
Correct |
915 ms |
52208 KB |
Output is correct |
77 |
Correct |
773 ms |
52316 KB |
Output is correct |
78 |
Correct |
355 ms |
55792 KB |
Output is correct |
79 |
Correct |
445 ms |
49840 KB |
Output is correct |
80 |
Correct |
506 ms |
52952 KB |
Output is correct |
81 |
Correct |
628 ms |
52840 KB |
Output is correct |
82 |
Correct |
835 ms |
51952 KB |
Output is correct |
83 |
Correct |
447 ms |
50916 KB |
Output is correct |
84 |
Correct |
706 ms |
52168 KB |
Output is correct |
85 |
Correct |
779 ms |
52008 KB |
Output is correct |
86 |
Correct |
311 ms |
49756 KB |
Output is correct |
87 |
Correct |
442 ms |
52848 KB |
Output is correct |
88 |
Correct |
448 ms |
49788 KB |
Output is correct |
89 |
Correct |
571 ms |
51396 KB |
Output is correct |
90 |
Correct |
691 ms |
51800 KB |
Output is correct |
91 |
Correct |
452 ms |
51100 KB |
Output is correct |
92 |
Correct |
874 ms |
52228 KB |
Output is correct |
93 |
Correct |
865 ms |
52068 KB |
Output is correct |
94 |
Correct |
300 ms |
49760 KB |
Output is correct |
95 |
Correct |
700 ms |
51816 KB |
Output is correct |
96 |
Correct |
691 ms |
51948 KB |
Output is correct |
97 |
Correct |
678 ms |
51880 KB |
Output is correct |
98 |
Correct |
700 ms |
51920 KB |
Output is correct |
99 |
Correct |
751 ms |
51912 KB |
Output is correct |
100 |
Correct |
811 ms |
51904 KB |
Output is correct |