#include <bits/stdc++.h>
#pragma optimize("Ofast")
#pragma target("avx2")
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define pii pair<int,int>
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define mem(a,i) memset(a,i,sizeof(a))
#define sz(s) (int)s.size()
#define y1 yy
#define ppb pop_back
#define lb lower_bound
#define ub upper_bound
#define gcd(a,b) __gcd(a,b)
#define in insert
#define int ll
const int MAX=1e5+15;
const int B=2e5;
const int N=104;
const int block=450;
const int maxB=MAX/B+10;
const ll inf=1e18;
const int mod=1e9+7;
const int mod1=1e9+9;
const ld eps=1e-9;
int dx[8]={1,0,-1,0,1,-1,-1,1};
int dy[8]={0,1,0,-1,1,-1,1,-1};
int binpow(int a,int n){
if(!n)return 1;
if(n%2==1)return a*binpow(a,n-1);
int k=binpow(a,n/2);
return k*k;
}
int n,q;
string s;
int dp1[(1<<20)];
int dp0[(1<<20)];
int bit(int i,int j){
return (i>>j)&1;
}
int stupid(string t){
int ans=0;
int c=0;
for(int j=0;j<n;j++)if(t[j]=='?')c++;
for(int i=0;i<(1<<c);i++){
int cur=0;
int f=0;
for(int j=0;j<n;j++){
if(t[j]!='?'){
f=f*2+(t[j]-'0');
}
else{
f=f*2+bit(i,cur++);
}
}
ans+=s[f]-'0';
}
return ans;
}
void solve(){
cin>>n>>q;
cin>>s;
for(int mask=0;mask<(1<<n);mask++)dp1[mask]=s[mask]-'0';
for(int i=0;i<=n;i++){
for(int mask=(1<<n)-1;mask>=0;mask--){
if(!((mask>>i)&1)){
dp1[mask]+=dp1[mask+(1<<i)];
}
}
}
for(int mask=0;mask<(1<<n);mask++)dp0[mask]=s[mask]-'0';
for(int i=0;i<n;i++){
for(int mask=0;mask<(1<<n);mask++){
if((mask>>i)&1){
dp0[mask]+=dp0[mask-(1<<i)];
}
}
}
while(q--){
string t;
cin>>t;
int c0=0,c1=0,c=0;
for(int i=0;i<n;i++){
if(t[i]=='0')c0++;
else if(t[i]=='1')c1++;
else c++;
}
if(c<=min(c1,c0)){
cout<<stupid(t)<<"\n";
}
else if(c0<=c1){
int ans=0;
for(int mask=0;mask<(1<<c0);mask++){
int f1=0;
int cur=0;
int c=0;
for(int j=0;j<n;j++){
if(t[j]=='1'){
f1=f1*2+1;
}
else if(t[j]=='?'){
f1=f1*2;
}
else{
f1=f1*2;
if(bit(mask,cur++)){
f1++;
c++;
}
}
}
int res=dp1[f1];
if(c%2==1)ans-=res;
else ans+=res;
}
cout<<ans<<"\n";
}
else{
int ans=0;
for(int mask=0;mask<(1<<c1);mask++){
int f1=0;
int cur=0;
int c=0;
for(int j=0;j<n;j++){
if(t[j]=='0'){
f1=f1*2;
}
else if(t[j]=='?'){
f1=f1*2+1;
}
else{
f1=f1*2;
if(!bit(mask,cur)){
f1++;
}
if(bit(mask,cur)){
c++;
}
cur++;
}
}
// cout<<f<<" "<<f1<<"\n";
int res=dp0[f1];
if(c%2==1)ans-=res;
else ans+=res;
}
cout<<ans<<"\n";
}
}
}
//1010
//0011
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// prec();
int t=1;
// cin>>t;
while(t--)solve();
}
Compilation message
snake_escaping.cpp:3: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
3 | #pragma optimize("Ofast")
|
snake_escaping.cpp:4: warning: ignoring '#pragma target ' [-Wunknown-pragmas]
4 | #pragma target("avx2")
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
179 ms |
17088 KB |
Output is correct |
12 |
Correct |
198 ms |
17304 KB |
Output is correct |
13 |
Correct |
314 ms |
16088 KB |
Output is correct |
14 |
Correct |
327 ms |
16152 KB |
Output is correct |
15 |
Correct |
303 ms |
17244 KB |
Output is correct |
16 |
Correct |
454 ms |
16212 KB |
Output is correct |
17 |
Correct |
420 ms |
16280 KB |
Output is correct |
18 |
Correct |
134 ms |
18256 KB |
Output is correct |
19 |
Correct |
196 ms |
15060 KB |
Output is correct |
20 |
Correct |
210 ms |
16720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
179 ms |
17088 KB |
Output is correct |
12 |
Correct |
198 ms |
17304 KB |
Output is correct |
13 |
Correct |
314 ms |
16088 KB |
Output is correct |
14 |
Correct |
327 ms |
16152 KB |
Output is correct |
15 |
Correct |
303 ms |
17244 KB |
Output is correct |
16 |
Correct |
454 ms |
16212 KB |
Output is correct |
17 |
Correct |
420 ms |
16280 KB |
Output is correct |
18 |
Correct |
134 ms |
18256 KB |
Output is correct |
19 |
Correct |
196 ms |
15060 KB |
Output is correct |
20 |
Correct |
210 ms |
16720 KB |
Output is correct |
21 |
Correct |
250 ms |
20420 KB |
Output is correct |
22 |
Correct |
288 ms |
20400 KB |
Output is correct |
23 |
Correct |
538 ms |
19344 KB |
Output is correct |
24 |
Correct |
580 ms |
19152 KB |
Output is correct |
25 |
Correct |
401 ms |
21432 KB |
Output is correct |
26 |
Correct |
798 ms |
19820 KB |
Output is correct |
27 |
Correct |
625 ms |
19732 KB |
Output is correct |
28 |
Correct |
165 ms |
22100 KB |
Output is correct |
29 |
Correct |
227 ms |
18248 KB |
Output is correct |
30 |
Correct |
258 ms |
20304 KB |
Output is correct |
31 |
Correct |
656 ms |
20188 KB |
Output is correct |
32 |
Correct |
1442 ms |
20168 KB |
Output is correct |
33 |
Correct |
347 ms |
19084 KB |
Output is correct |
34 |
Correct |
651 ms |
19252 KB |
Output is correct |
35 |
Correct |
624 ms |
19640 KB |
Output is correct |
36 |
Correct |
171 ms |
18220 KB |
Output is correct |
37 |
Correct |
213 ms |
20052 KB |
Output is correct |
38 |
Correct |
232 ms |
18168 KB |
Output is correct |
39 |
Correct |
540 ms |
19280 KB |
Output is correct |
40 |
Correct |
568 ms |
19048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Runtime error |
34 ms |
24480 KB |
Execution killed with signal 11 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
179 ms |
17088 KB |
Output is correct |
12 |
Correct |
198 ms |
17304 KB |
Output is correct |
13 |
Correct |
314 ms |
16088 KB |
Output is correct |
14 |
Correct |
327 ms |
16152 KB |
Output is correct |
15 |
Correct |
303 ms |
17244 KB |
Output is correct |
16 |
Correct |
454 ms |
16212 KB |
Output is correct |
17 |
Correct |
420 ms |
16280 KB |
Output is correct |
18 |
Correct |
134 ms |
18256 KB |
Output is correct |
19 |
Correct |
196 ms |
15060 KB |
Output is correct |
20 |
Correct |
210 ms |
16720 KB |
Output is correct |
21 |
Correct |
250 ms |
20420 KB |
Output is correct |
22 |
Correct |
288 ms |
20400 KB |
Output is correct |
23 |
Correct |
538 ms |
19344 KB |
Output is correct |
24 |
Correct |
580 ms |
19152 KB |
Output is correct |
25 |
Correct |
401 ms |
21432 KB |
Output is correct |
26 |
Correct |
798 ms |
19820 KB |
Output is correct |
27 |
Correct |
625 ms |
19732 KB |
Output is correct |
28 |
Correct |
165 ms |
22100 KB |
Output is correct |
29 |
Correct |
227 ms |
18248 KB |
Output is correct |
30 |
Correct |
258 ms |
20304 KB |
Output is correct |
31 |
Correct |
656 ms |
20188 KB |
Output is correct |
32 |
Correct |
1442 ms |
20168 KB |
Output is correct |
33 |
Correct |
347 ms |
19084 KB |
Output is correct |
34 |
Correct |
651 ms |
19252 KB |
Output is correct |
35 |
Correct |
624 ms |
19640 KB |
Output is correct |
36 |
Correct |
171 ms |
18220 KB |
Output is correct |
37 |
Correct |
213 ms |
20052 KB |
Output is correct |
38 |
Correct |
232 ms |
18168 KB |
Output is correct |
39 |
Correct |
540 ms |
19280 KB |
Output is correct |
40 |
Correct |
568 ms |
19048 KB |
Output is correct |
41 |
Runtime error |
34 ms |
24480 KB |
Execution killed with signal 11 |
42 |
Halted |
0 ms |
0 KB |
- |