https://www.lydsy.com/JudgeOnline/problem.php?id=5285
|0 and &1 have no effect
If fill in '|', record as 0, if fill in '&', record as 1
Consider only the last
if request last = 1
Then the last |1 is after the last &0
Take the last digit of n numbers to form a 01 sequence
The operators filled in before the last digit of all numbers are also taken out to form a 01 sequence
Treat the position of the nth number as the highest bit
for the highest
If the sequence of numbers and the sequence of operators are both 0 or both are 1, no effect
If the sequence of numbers is 0, the sequence of operators is 1, that is, the last is &0, which obviously cannot be equal to 1 in the end, so this sequence of operators is illegal
If the number sequence is 1, the operator sequence is 0, and the last is |1, obviously it must be 1, this operator sequence is legal
If the sequence of numbers is always equal to the sequence of operators, since there is no effect, the 0 that ends up starting with the sequence of operators is also not legal
so
If this bit is required to be 1, and considering only this bit, the valid operator sequences are 01 sequences of operators < 01 sequences of numbers
Similarly, it can be derived
If this bit is required to be 0, and considering only this bit, the legal operator sequence is the 01 sequence of operators >= the 01 sequence of numbers
That is, the following conditions can be obtained:
Let the valid operator sequence be S, and the number sequence of the i-th bit be Ai
If the i-th bit of p is 1, then S<Ai ①
If the i-th bit of p is 0, then S>=Ai ②
Note that the smallest Ai in ① is up, and the largest Ai in ② is down
So the number of Ss that meet the requirements of all bits = up-down
The starting point for calculating the number is high reduction, the question requires modulo, and you can directly hash it.
#include<cstdio> #include<algorithm> #define N 5001 using namespace std; const int mod = 1e9 + 7 ; int bit[N]; char s[N]; int has[N]; int sa[N],now[N]; intmain () { int n,m,q; scanf("%d%d%d",&n,&m,&q); bit[0]=1; for(int i=1;i<=n;++i) { bit[i]=bit[i-1]<<1; bit[i]-=bit[i]>=mod ? mod : 0; } for(int i=1;i<=m;++i) sa[i]=i; int c[2]; for(int i=1;i<=n;++i) { c[1]=c[0]=0; scanf("%s",s+1); for(int j=1;j<=m;++j) { has[j]=has[j]+(s[j]-'0')*bit[i-1]; has[j]-=has[j]>=mod ? mod : 0; c[s[j]-'0']++; } c[1]+=c[0]; for(int j=m;j;--j) now[c[s[sa[j]]-'0']--]=sa[j]; swap(sa,now); } int up,down; for(int t=1;t<=q;++t) { up=m+1; down=0; scanf("%s",s+1); for(int i=1;i<=m && up==m+1;++i) if(s[sa[i]]-'0') up=i; for(int i=m;i && !down;--i) if(!(s[sa[i]]-'0')) down=i; if(up<down) { puts("0"); continue; } up= up==m+1 ? bit[n] : has[sa[up]]; down= !down ? 0 : has[sa[down]]; printf("%d\n",(up-down+mod)%mod); } }