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
| #include<bits/stdc++.h> using namespace std; #define Mod1(x) ((x>=P)&&(x-=P)) #define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;++i) #define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;--i)
const int N=62,P=1e9+7,INF=1e9+10;
int n,m,q; struct Mat{ int a[N][N]; void clear(){ memset(a,0,sizeof a); } Mat operator * (const Mat &x) const { Mat res; res.clear(); rep(i,1,n) rep(j,1,n) if(a[i][j]) rep(k,1,n) res.a[i][k]=(res.a[i][k]+1ll*a[i][j]*x.a[j][k])%P; return res; } Mat operator + (const Mat &x) const { Mat res; rep(i,1,n) rep(j,1,n) res.a[i][j]=a[i][j]+x.a[i][j],Mod1(res.a[i][j]); return res; } } I,E,dp[N]; int F[N][N],G[N][N]; int S[N][N];
int main(){ freopen("spaceship.in","r",stdin),freopen("spaceship.out","w",stdout); scanf("%d%d%d",&n,&m,&q); rep(i,1,n) I.a[i][i]=1; rep(i,1,n) rep(j,1,n) scanf("%1d",&E.a[i][j]); dp[0]=I; rep(i,1,m) dp[i]=dp[i-1]+dp[i-1]*E*dp[i-1]; while(q--) { int bs,s,bt,t; scanf("%d%d%d%d",&bs,&s,&bt,&t); memset(F,0,sizeof F),memset(G,0,sizeof G); memset(S,0,sizeof S); F[bs][s]=1; rep(i,1,n) S[bs][i]=dp[bs-1].a[s][i]; rep(i,bs+1,m) { rep(a,1,n) if(S[i-1][a]) rep(b,1,n) F[i][b]=(F[i][b]+1ll*S[i-1][a]*E.a[a][b])%P; rep(a,1,n) S[i][a]=S[i-1][a]; rep(a,1,n) if(F[i][a]) rep(b,1,n) S[i][b]=(S[i][b]+1ll*F[i][a]*dp[i-1].a[a][b])%P; }
memset(S,0,sizeof S); G[bt][t]=1; rep(i,1,n) S[bt][i]=dp[bt-1].a[i][t]; rep(i,bt+1,m) { rep(a,1,n) rep(b,1,n) G[i][a]=(G[i][a]+1ll*S[i-1][b]*E.a[a][b])%P; rep(a,1,n) S[i][a]=S[i-1][a]; rep(a,1,n) rep(b,1,n) S[i][a]=(S[i][a]+1ll*G[i][b]*dp[i-1].a[a][b])%P; }
int ans=0; rep(i,1,m) rep(j,1,n) ans=(ans+1ll*F[i][j]*G[i][j])%P; printf("%d\n",ans); } }
|