矩阵快速幂。
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<iostream> using namespace std; typedef long long LL; const double pi=acos(-1.0); void File() { freopen("D:\\in.txt","r",stdin); freopen("D:\\out.txt","w",stdout); } int MOD=10000; int T,n,m,k; struct Matrix { int A[105][105]; int R, C; }; Matrix X, Y, Z; Matrix cheng(Matrix a, Matrix b) { Matrix c; int i, j, k; for (i = 1; i <= a.R; i++) for (j = 1; j <= b.C; j++) { c.A[i][j]=0; for (k = 1; k <= a.C; k++) c.A[i][j] = (c.A[i][j] + (a.A[i][k] * b.A[k][j]) % MOD) % MOD; } c.R = a.R; c.C = b.C; return c; } void init() { memset(X.A, 0, sizeof X.A); memset(Y.A, 0, sizeof Y.A); Y.R = n; Y.C = n; for (int i = 1; i <= n; i++) Y.A[i][i] = 1; for(int i=1;i<=m;i++) { int a,b; scanf("%d%d",&a,&b); X.A[a][b]=1; } X.R = n; X.C = n; } void work() { while (k) { if (k % 2 == 1) Y = cheng(Y,X); k = k >> 1; X = cheng(X,X); } printf("%d\n",Y.A[1][n]); } int main() { while(~scanf("%d%d%d",&n,&m,&k)) { init(); work(); } return 0; }
FZU 1627 Revival's road
时间: 2024-10-29 19:11:08