1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| #include<bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;++i)
char IO; int rd(){ int s=0; while(!isdigit(IO=getchar())); do s=(s<<1)+(s<<3)+(IO^'0'); while(isdigit(IO=getchar())); return s; }
const int N=5e4+10,P=1e9+7;
int n,m; int A[N][10]; char s[N]; struct Node{ int a[2]; Node(){ } Node(int x){ a[!x]=0,a[x]=1; } Node operator < (const Node x) const { Node res; res.a[0]=(1ll*a[0]*(x.a[0]+x.a[1])+1ll*a[1]*x.a[0])%P; res.a[1]=1ll*a[1]*x.a[1]%P; return res; } Node operator > (const Node x) const { Node res; res.a[0]=1ll*a[0]*x.a[0]%P; res.a[1]=(1ll*a[1]*(x.a[0]+x.a[1])+1ll*a[0]*x.a[1])%P; return res; } Node operator + (const Node x) const { int l=a[0]+a[1],r=x.a[0]+x.a[1]; Node res; rep(i,0,1) res.a[i]=(1ll*a[i]*r+1ll*l*x.a[i])%P; return res; } } X[N]; int I[10],Y[N],T,val[N];
void Work(int S){ T=0; for(int i=1;s[i];++i) { if(isdigit(s[i])) X[++T]=Node((S>>(s[i]-'0'))&1),Y[T]=0; else if(s[i]=='<') Y[++T]=1; else if(s[i]=='>') Y[++T]=2; else if(s[i]=='?') Y[++T]=3; else if(s[i]=='(') Y[++T]=4; else X[T-1]=X[T],Y[T-1]=Y[T],T--; if(T>2 && !Y[T] && !Y[T-2]){ if(Y[T-1]==1) X[T-2]=X[T-2]<X[T]; if(Y[T-1]==2) X[T-2]=X[T-2]>X[T]; if(Y[T-1]==3) X[T-2]=X[T-2]+X[T]; T-=2; } } val[S]=X[1].a[1]; }
int main(){ n=rd(),m=rd(); rep(i,0,m-1) rep(j,1,n) A[j][i]=rd(); scanf("%s",s+1); rep(S,1,(1<<m)-1) Work(S); int ans=0; rep(i,1,n) { rep(j,0,m-1) I[j]=j; sort(I,I+m,[&](int x,int y){ return A[i][x]<A[i][y]; }); int S=0; for(int j=m-1;~j;--j){ S|=1<<I[j]; ans=(ans+1ll*(A[i][I[j]]-(j?A[i][I[j-1]]:0))*val[S])%P; } } printf("%d\n",ans); }
|