This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define in ({int x=0;int c=getchar(),n=0;for(;!isdigit(c);c=getchar()) n=(c=='-');for(;isdigit(c);c=getchar()) x=x*10+c-'0';n?-x:x;})
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l,int r){return l+rng()%(r-l+1);}
#define fasty ios_base::sync_with_stdio(0),cin.tie(0)
#define fori(x,a,b) for (int x=a;x<=b;++x)
#define ford(x,a,b) for (int x=a;x>=b;x--)
#define forv(a,b) for(auto&a:b)
#define fi first
#define se second
#define pb push_back
#define ii pair<int,int>
#define mt make_tuple
#define mp make_pair
#define all(a) a.begin(),a.end()
#define reset(f, x) memset(f, x, sizeof(f))
#define getbit(x,i) ((x>>i)&1)
#define batbit(x,i) (x|(1ll<<i))
#define tatbit(x,i) (x&~(1<<i))
#define gg exit(0);
const int N = (1<<20)+3;
int f1[N],f0[N],cnt[N],val[N],n,t;
main()
{
//freopen("task.inp","r",stdin);
fasty;
cin>>n>>t;
int ma = (1<<n)-1;
fori(i,0,ma)
{
char c;cin>>c;
f0[i] = f1[i] = val[i] = c-'0';
cnt[i] = __builtin_popcount(i);
}
fori(i,0,n-1)
{
fori(j,0,ma) if(getbit(j,i))
{
f0[j]+=f0[(j^(1<<i))];
}
ford(j,ma,0) if(getbit(j,i)) f1[(j^(1<<i))]+=f1[j];
}
while(t--)
{
string s;cin>>s;
int a=0,b=0,c=0;
fori(i,0,s.size()-1)
{
if(s[i]=='0') a = batbit(a,s.size()-i-1);
if(s[i]=='1') b = batbit(b,s.size()-i-1);
if(s[i]=='?') c = batbit(c,s.size()-i-1);
}
int kq = 0;
if(cnt[a]<=n/3)
{
for(int sub=a;;sub=(sub-1)&a)
{
if(cnt[sub]%2) kq-=f1[(sub|b)];//tru di cac th nhieu hon 1 bit la 0
else kq+=f1[(sub|b)];//cong lai vi tru nhieu qua
if(!sub) break;
}
cout<<kq<<"\n";
}
else if(cnt[b]<=n/3)
{
for(int sub=b;;sub=(sub-1)&b)
{
if(cnt[(sub^b)]%2) kq-=f0[(sub|c)];//tuong tu bai vim war
else kq+=f0[(sub|c)];
if(!sub) break;
}
cout<<kq<<"\n";
}
else
{
for(int sub=c;;sub=(sub-1)&c)
{
kq+=val[(sub|b)];
if(!sub) break;
}
cout<<kq<<"\n";
}
}
}
//https://oj.uz/problem/view/JOI18_snake_escaping
//https://github.com/hharryyf/codeforcepractice
//https://codeforces.com/topic/91973/en12
//https://codeforces.com/blog/entry/20284
/* Weak:
- DP (SOS,Broken profiles,Connected component) (usaco guide)
- DP + Probabilities
- Matrix Exponentiation(usaco guide)
- Bao ham loai tru(hackerearth,http://algoogle.hadrori.jp/aoj/1254/)
- Lien Thong Manh(https://codeforces.com/blog/entry/68138)
- Dijkstra
- Geometry(MPOLY,CVXPOLY,MTRIAREA)
- DP Thu tu tu dien(google)
*/
Compilation message (stderr)
snake_escaping.cpp:25:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
25 | main()
| ^~~~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:7:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
7 | #define fori(x,a,b) for (int x=a;x<=b;++x)
......
50 | fori(i,0,s.size()-1)
| ~~~~~~~~~~~~~~
snake_escaping.cpp:50:9: note: in expansion of macro 'fori'
50 | fori(i,0,s.size()-1)
| ^~~~
snake_escaping.cpp:52:49: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
52 | if(s[i]=='0') a = batbit(a,s.size()-i-1);
| ~~~~~~~~~~^~
snake_escaping.cpp:19:30: note: in definition of macro 'batbit'
19 | #define batbit(x,i) (x|(1ll<<i))
| ^
snake_escaping.cpp:53:49: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
53 | if(s[i]=='1') b = batbit(b,s.size()-i-1);
| ~~~~~~~~~~^~
snake_escaping.cpp:19:30: note: in definition of macro 'batbit'
19 | #define batbit(x,i) (x|(1ll<<i))
| ^
snake_escaping.cpp:54:49: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
54 | if(s[i]=='?') c = batbit(c,s.size()-i-1);
| ~~~~~~~~~~^~
snake_escaping.cpp:19:30: note: in definition of macro 'batbit'
19 | #define batbit(x,i) (x|(1ll<<i))
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |