zoukankan      html  css  js  c++  java
  • FZU 1627 Revival's road

    矩阵快速幂。

    #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
    ",Y.A[1][n]);
    }
    
    int main()
    {
        while(~scanf("%d%d%d",&n,&m,&k))
        {
            init();
            work();
        }
        return 0;
    }
  • 相关阅读:
    反爬的几种手段总结
    算法基础篇一
    python总结九
    python总结八
    python总结七
    python总结六
    初识Machine学习
    python总结五
    python总结四
    python总结三
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5782062.html
Copyright © 2011-2022 走看看