Matrix multiplication.
The last scheme number can be obtained by matrix multiplication in adjacency matrix
Piranhas are periodic movements that can be represented by 12 matrices. 12 matrices are multiplied together to get a 13th matrix.
Then the K/12 part is transferred with the 13th matrix, and then the remaining matrix is multiplied.
#include <cstdio>#include<algorithm>#include<cstring>#defineLL Long Longusing namespacestd;Const intMAXN = -+Ten;Const intMoD =10000;intn,m,p1,p2,k,nf;intT[MAXN],Q[MAXN][MAXN];structMatrix {intA[MAXN][MAXN]; int*operator[] (intx) {returnA[x]; } Matrixoperator*(Matrix B) {matrix res; for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) for(intk=1; k<=n;k++) Res[i][k]= (Res[i][k]+a[i][j]*b[j][k])%MoD; returnRes; } Matrixoperator^ (inte) {Matrix res,tmp=* This; Res.init (); while(e) {if(e&1) res=res*tmp; TMP=tmp*tmp; E>>=1; } returnRes; } voidinit () {memset (A,0,sizeof(a)); for(intI=1; i<=n;i++) A[i][i]=1; } Matrix () {memset (A,0,sizeof(a)); }}a[ the],b,f;voidbuild () {scanf ("%d%d%d%d%d",&n,&m,&p1,&p2,&k); P1++; p2++; for(intI=1, u,v;i<=m;i++) {scanf ("%d%d",&u,&v); U++; v++; B[U][V]=b[v][u]=1; } scanf ("%d",&NF); for(intI=1; i<=nf;i++) {scanf ("%d",&T[i]); for(intj=0; j<t[i];j++) {scanf ("%d",&Q[i][j]); Q[I][J]++; } } for(intI=1; i<= A; i++) {A[i]=b; for(intj=1, v;j<=nf;j++) {v=q[j][i%T[j]]; for(intk=1; k<=n;k++) a[i][k][v]=0; }} b.init (); for(intI=1; i<= A; i++) b=b*A[i]; F.init (); F=f* (b^ (k/ A)); for(intI=1; I<= (k% A); i++) f=f*A[i]; printf ("%d\n", F[P1][P2]);}intMain () {build (); return 0; }
1898: [Zjoi2005]swamp Swamp crocodile