• Vijos1603 迷宫


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

    矩阵快速幂,答案是原矩阵的m次幂的第s行第f列

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 #define rep(i,l,r) for(int i=l; i<=r; i++)
     6 #define clr(x,y) memset(x,y,sizeof(x))
     7 using namespace std;
     8 const int maxn = 51;
     9 int n,m,s,f,KPM;
    10 struct matrix{
    11     int m[maxn][maxn];
    12     matrix(){
    13         clr(m,0);
    14     }
    15     void init(){
    16         rep(i,1,n) m[i][i] = 1;
    17     }
    18     matrix operator * (const matrix &y) const {
    19         matrix ret;
    20         rep(i,1,n) rep(j,1,n) rep(k,1,n)
    21         ret.m[i][j] = (ret.m[i][j] + m[i][k] * y.m[k][j]) % KPM;
    22         return ret;
    23     }
    24 }a;
    25 inline int read(){
    26     int ans = 0, f = 1;
    27     char c = getchar();
    28     while (!isdigit(c)){
    29         if (c == '-') f = -1;
    30         c = getchar();
    31     }
    32     while (isdigit(c)){
    33         ans = ans * 10 + c - '0';
    34         c = getchar();
    35     }
    36     return ans * f;
    37 }
    38 matrix qpow(matrix x,int y){
    39     matrix ret, b = x; ret.init();
    40     while (y){
    41         if (y & 1) ret = ret * b;
    42         b = b * b;
    43         y >>= 1;
    44     }
    45     return ret;
    46 }
    47 int main(){
    48     n = read();
    49     rep(i,1,n) rep(j,1,n) a.m[i][j] = read();
    50     m = read(); s = read(); f = read(); KPM = read();
    51     a = qpow(a,m);
    52     printf("%d
    ",a.m[s][f]);
    53     return 0;
    54 }
    View Code
  • 相关阅读:
    js实现IE6下png背景透明,超简单,超牛!
    SQLSERVER 动态执行SQL sp_executesql与EXEC
    存储过程中的 SET NOCOUNT ON
    sql CHARINDEX
    css让页面居中
    (转)GridView合集
    (转)智能客户端(SmartClient)
    将程序加到启动组
    SQL时间函数详细说明
    独立存储
  • 原文地址:https://www.cnblogs.com/jimzeng/p/vijos1603.html
Copyright © 2020-2023  润新知