A DP
Fast power acceleration with matrix
And then this DP state is smart with the edge state.
#include <cstdio> #include <cstdlib> #include <algorithm> #include <cstring> #define CL (x) memset (x,0,sizeof (x)) using namespace Std;inline char NC () {static char buf[100000],*p1=buf,*p2=buf;if (P1==P2) {p2= (p1= BUF) +fread (Buf,1,100000,stdin); if (P1==P2) return EOF; }return *p1++;} inline void read (int &x) {char c=nc (), b=1;for (;! (c>= ' 0 ' && c<= ' 9 '); C=nc ()) if (c== '-') b=-1;for (x=0;c>= ' 0 ' && c<= ' 9 '; x=x*10+c-' 0 ', C=NC ()) ; X*=b;} const int P=45989;const int m=125;struct matrix{int a[m][m];int n; Matrix () {}matrix (int _n,int i=0) {n=_n; cl (a); if (i) for (int j=1;j<=n;j++) a[j][j]=1;} Friend Matrix operator * (const matrix &a,const matrix &b) {int N=A.N; Matrix ret (n); for (int. i=1;i<=n;i++) for (int. j=1;j<=n;j++) for (int k=1;k<=n;k++) (ret.a[i][j]+=a.a[i][k]*b.a[ k][j]%p)%=p;return ret;}} A;struct edge{int u,v;edge (int u=0,int v=0): U (U), V (v) {}}edges[m];int tot=1;int n,m,round,s,t;inline matrix Pow (Matrix A , int b) {Matrix ret (a.n,1); for (; b;b>>=1,a=a*a) if (b&1) ret=ret*a;return ret;} int Ans[m],ans;int Main () {int Iu,iv;freopen ("t.in", "R", stdin), Freopen ("T.out", "w", stdout), read (n), read (M); Read ( Round); Read (S); Read (T); s++; t++;for (int i=1;i<=m;i++) {read (IU); Read (iv); iu++; iv++; Edges[++tot]=edge (IU,IV); Edges[++tot]=edge (Iv,iu);} A=matrix (tot); for (int. i=2;i<=tot;i++) {for (int j=2;j<=tot;j++) {if ((i^1) ==j) continue;if (Edges[i].u==edges[j] . v) a.a[i][j]=1;} if (edges[i].u==s) a.a[i][1]=1;} A=pow (A,round); for (int i=1;i<=tot;i++) {ans[i]=a.a[i][1];if (edges[i].v==t) (ans+=ans[i])%=p;} printf ("%d\n", Ans); return 0;}
[DP Matrix Fast Power] Bzoj 1875 [sdoi2009]hh go for a walk