[SDOI2009]HH去散步 [矩陣乘法+DP] (hard)
阿新 • • 發佈:2018-12-15
此題較難 , 留一個坑
#include<bits/stdc++.h> #define N 55 #define M 205 #define Mod 45989 using namespace std; struct Matrix{ int a[M][M]; Matrix(){memset(a,0,sizeof(a));} }; int n,m,t,a,b,ans; int first[N],next[M],to[M],tot; void add(int x,int y){ next[++tot]=first[x],first[x]=tot,to[tot]=y; } Matrix mul(Matrix A,Matrix B){ Matrix C; for(int i=1;i<=tot;i++) for(int j=1;j<=tot;j++) for(int k=1;k<=tot;k++) C.a[i][j] = (C.a[i][j] + A.a[i][k] * B.a[k][j]) % Mod; return C; } Matrix power(Matrix A,int k){ Matrix ans; for(int i=1;i<=tot;i++) ans.a[i][i]=1; for(;k;k>>=1){ if(k&1) ans = mul(ans,A); A = mul(A,A); }return ans; } int main(){ scanf("%d%d%d%d%d",&n,&m,&t,&a,&b); ++a , ++b; for(int i=1;i<=m;i++){ int x,y; scanf("%d%d",&x,&y); ++x , ++y; add(x,y),add(y,x); } Matrix A,B; for(int i=first[a];i;i=next[i]) A.a[1][i] = 1; for(int i=1;i<=tot;i++){ int x=to[i]; for(int j=first[x];j;j=next[j]){ int flag; if(i&1) flag=1; else flag=-1; if(j!=i+flag) B.a[i][j]=1; } } B = power(B,t-1); A = mul(A,B); for(int i=1;i<=tot;i++) if(to[i]==b) ans = (ans + A.a[1][i]) % Mod; printf("%d",ans); return 0; }