[ZJOI 2005] 沼泽鳄鱼

题目传送-Luogu2579

题目传送-BZOJ1898

题意:

有一个无向图。
有许多条鱼在若干个点之间循环出现,循环节长度小于等于(4)
要求从(A)花费(K)个单位时间到达(B),中途不能和鱼相碰,问有多少方案。
(nle50,K le 2000000000)

题解:

这个循环节长度太小了,它们的(lcm)只有(12)
意味着只有12张不同的图
我们以12为一单位跑矩阵快速幂就行了
最后几幅图暴力

过程:

乘法的时候一不小心乘反了

代码:

const int N=60,U=12;
const int P=10000;
int n,m,s,S,T,K;
struct MAT {
	int mat[N][N];
	inline void clear() {
		mem(mat,0);
	}
	inline void print() {
		for(int i=1;i<=n;i++) {
			for(int j=1;j<=n;j++)
				printf("%d ",mat[i][j]);
			puts("");
		}puts("");
	}
	inline MAT operator * (const MAT &a)const {
		MAT c; c.clear();
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				for(int k=1;k<=n;k++) {
					c.mat[i][j]+=mat[i][k]*a.mat[k][j]%P;
					if(c.mat[i][j]>=P) c.mat[i][j]-=P;
				}
		return c;
	}
}unit,u,ma[12],st,trans,ans;
inline MAT Pow(MAT x,int y) {
	MAT ret=unit;
	for(;y;y>>=1,x=x*x)
		if(y&1) ret=ret*x;
	return ret;
}
signed main() {
	// freopen("2.in","r",stdin);
	// freopen("my.out","w",stdout);
	read(n); read(m); read(S); read(T); read(K); ++S; ++T;
	for(int i=1;i<=n;i++) unit.mat[i][i]=1;
	for(int i=1;i<=m;i++) {
		int x,y; read(x); read(y); ++x; ++y;
		u.mat[x][y]=u.mat[y][x]=1;
	}
	for(int i=0;i<12;i++) ma[i]=u;
	read(s);
	for(int i=1;i<=s;i++) {
		int loop; read(loop);
		for(int j=0;j<loop;j++) {
			int x; read(x); ++x;
			for(int k=j;k<12;k+=loop) {
				for(int p=1;p<=n;p++)
					ma[k].mat[p][x]=0;
			}
		}
	}
	// printf("%d
",(ma[1]*ma[2]*ma[3]).mat[S][T]);
	// for(int i=0;i<4;i++) ma[i].print();
	for(int i=2;i<12;i++) ma[i]=ma[i-1]*ma[i]; ma[0]=ma[11]*ma[0];
	int t=K/12,mod=K%12;
	if(mod==0) st=unit;
	else st=ma[mod];
	trans=Pow(ma[0],t);
	// ma[1].print();
	// st.print(); trans.print();
	ans=trans*st;
	printf("%d
",ans.mat[S][T]);
	return 0;
}
/*
20 19 1 15 20
3 14
17 16
7 10
0 2
17 19
12 15
5 7
19 8
19 12
8 0
14 10
14 13
17 6
1 3
3 15
11 17
9 15
9 0
16 15
15
3 12 15 18
4 9 10 11 12
3 16 13 6
2 19 16
3 15 10 5
4 2 0 15 11
2 0 8
3 9 5 0
3 0 4 8
2 8 4
3 7 16 6
2 3 7
4 7 19 8 2
4 3 5 12 18
2 9 13
*/

用时:1h

原文地址:https://www.cnblogs.com/functionendless/p/9470306.html