[Question link]
Https://www.lydsy.com/JudgeOnline/problem.php? Id = 1875
[Algorithm]
Use F [I] [J] to show the number of solutions on the J edge while taking step I.
Matrix acceleration.
Time Complexity: O (N ^ 3 logn)
[Code]
#include<bits/stdc++.h>using namespace std;#define MAXN 125const int P = 45989;struct edge{ int to , nxt;} e[MAXN << 1];int n , m , t , A , B , tot;int head[MAXN];struct matrix_t{ int mat[MAXN][MAXN]; } a , b;template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }template <typename T> inline void read(T &x){ T f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == ‘-‘) f = -f; for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - ‘0‘; x *= f;}inline void addedge(int u,int v){ tot++; e[tot] = (edge){v , head[u]}; head[u] = tot;}inline void multipy(matrix_t &a , matrix_t b){ static matrix_t ret; for (int i = 1; i <= tot; i++) { for (int j = 1; j <= tot; j++) { ret.mat[i][j] = 0; } } for (int i = 1; i <= tot; i++) { for (int j = 1; j <= tot; j++) { for (int k = 1; k <= tot; k++) { ret.mat[i][j] = (ret.mat[i][j] + 1LL * a.mat[i][k] * b.mat[k][j]) % P; } } } a = ret;}inline void exp_mod(matrix_t &a , int t){ static matrix_t tmp; for (int i = 1; i <= tot; i++) { for (int j = 1; j <= tot; j++) { tmp.mat[i][j] = (i == j); } } while (t > 0) { if (t & 1) multipy(tmp , a); multipy(a , a); t >>= 1; } a = tmp;}int main(){ read(n); read(m); read(t); read(A); read(B); ++A; ++B; tot = 1; for (int i = 1; i <= m; i++) { int u , v; read(u); read(v); ++u; ++v; addedge(u , v); addedge(v , u); } for (int i = 2; i <= tot; i++) { for (int j = 2; j <= tot; j++) { if (i != (j ^ 1) && e[j ^ 1].to == e[i].to) ++b.mat[i][j]; } } for (int i = head[A]; i; i = e[i].nxt) ++a.mat[1][i]; exp_mod(b , t - 1); multipy(a , b); int ans = 0; for (int i = head[B]; i; i = e[i].nxt) ans = (ans + 1LL * a.mat[1][i ^ 1]) % P; printf("%d\n" , ans); return 0; }
[Sdoi 2009] HH for a walk