vijosP1603迷宫

vijosP1603迷宫

链接:https://vijos.org/p/1603

【思路】

  参考Matrix67的文章:

 

【代码】

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #define FOR(a,b,c) for(int a=(b);a<(c);a++)
 5 using namespace std;
 6 
 7 const int maxn = 100+10;
 8 typedef long long LL;
 9 
10 int n,m,MOD;
11 
12 struct Matrix{
13     int r,c;
14     LL N[maxn][maxn];
15     void init(int r,int c) {
16         this->r=r, this->c=c;
17         memset(N,0,sizeof(N));
18     }
19     Matrix operator*(Matrix& B)const{
20         Matrix A=*this,C;
21         C.init(A.r,B.c);
22         for(int i=0;i<C.r;i++)
23            for(int j=0;j<C.c;j++)
24               for(int k=0;k<A.c;k++)
25                  C.N[i][j] = (C.N[i][j]+A.N[i][k]*B.N[k][j])%MOD;
26         return C;
27     }
28     Matrix pow(int p) {
29         Matrix tmp=*this;
30         Matrix ans;
31         ans.init(r,r);
32         for(int i=0;i<r;i++) ans.N[i][i]=1;
33         while(p) {
34             if(p&1) ans=ans*tmp;
35             tmp=tmp*tmp;
36             p>>=1;
37         }
38         return ans;
39     }
40 };
41 int main() {
42     int s,t;
43     scanf("%d",&n);
44     Matrix ans;
45     ans.init(n,n);
46     FOR(i,0,n)FOR(j,0,n) scanf("%d",&ans.N[i][j]);
47     scanf("%d%d%d%d",&m,&s,&t,&MOD);
48     ans=ans.pow(m);
49     printf("%d
",ans.N[s-1][t-1]);
50     return 0;
51 }
原文地址:https://www.cnblogs.com/lidaxin/p/4922818.html