SSL2512&BZOJ1898 沼泽鳄鱼

来源:互联网 发布:photoshop绘画软件 编辑:程序博客网 时间:2024/04/27 17:36

想明白了这东西能分成12个时刻,于是就有12个矩阵,然后乘起来当成操作就行了,时间复杂度一个n3logk

Problem Id:2511 User Id:BPM136 

Memory:1756K  Time:75MSLanguage:G++  Result:Accepted#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<algorithm>#define LL long long#define fo(i,a,b) for(int i=a;i<=b;i++)using namespace std;inline LL read(){LL d=0,f=1;char s=getchar();while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}return d*f;}#define N 50#define FISH 25#define inf 10000int n,m,nm;struct matrix{int a[N][N];void clear(){memset(a,0,sizeof(a));}void OUT(){fo(i,0,m-1){fo(j,0,m-1){cout<<a[i][j]<<' ';}cout<<endl;}cout<<endl;}matrix operator*(const matrix b)const{matrix anss;fo(i,0,m-1)fo(j,0,m-1){anss.a[i][j]=0;fo(k,0,m-1)anss.a[i][j]+=a[i][k]*b.a[k][j];anss.a[i][j]%=inf;}return anss;}}I,tot[FISH+5],A;int fish[FISH][5],s,e,nfish;void getI(){fo(i,0,m-1)fo(j,0,m-1)if(i==j)I.a[i][j]=1;else I.a[i][j]=0;}matrix KSM(matrix a,int k){matrix ret=I;while(k){if(k&1)ret=a*ret;a=a*a;k>>=1;}return ret;}int main(){m=read(),n=read(),s=read(),e=read();nm=read();fo(i,1,12)tot[i].clear();fo(i,1,n){int x=read(),y=read();fo(j,1,12)tot[j].a[x][y]=tot[j].a[y][x]=1;}nfish=read();getI();A=I;fo(i,1,nfish){fish[i][0]=read();fo(j,1,fish[i][0])fish[i][j]=read();fo(j,1,12){fo(k,0,m-1)tot[j].a[k][fish[i][j%fish[i][0]+1]]=0;}}//fo(i,1,12)tot[i].OUT();fo(i,1,12)A=A*tot[i];A=KSM(A,nm/12);nm%=12;fo(i,1,nm)A=A*tot[i];cout<<A.a[s][e]<<endl;return 0;}


1 0