zoukankan      html  css  js  c++  java
  • 华东交通大学2017年ACM“双基”程序设计竞赛 1005

    Problem Description

    假设你有一个矩阵,有这样的运算A^(n+1) = A^(n)*A (*代表矩阵乘法)
    现在已知一个n*n矩阵A,S = A+A^2+A^3+...+A^k,输出S,因为每一个元素太大了,输出的每个元素模10

    Input

    先输入一个T(T<=10),每组一个n,k(1<=n<=30, k<=1000000)

    Output

    输出一个矩阵,每个元素模10(行末尾没有多余空格)

    Sample Input

    1
    3 2
    0 2 0
    0 0 2
    0 0 0

    Sample Output

    0 2 4
    0 0 2
    0 0 0

    解法:矩阵的等比求和,知道这个就用模版写
    #include<bits/stdc++.h>
    #define clr(x) memset(x,0,sizeof(x))
    #define LL long long
    using namespace std;
    #define INF 0x3f3f3f3f
    typedef long long ll;
    const int N= 30 +9;
    struct Matrix
    {
        int m[N][N];
    };
    Matrix I;
    int n,k,M;
    
    Matrix add(Matrix a,Matrix b)
    {
        Matrix c;
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                c.m[i][j]=(a.m[i][j]+b.m[i][j])%M;
        return c;
    }
    
    Matrix multi(Matrix a,Matrix b)
    {
        Matrix c;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                c.m[i][j]=0;
                for(int k=0;k<n;k++)
                    c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j])%M;
            }
        }
        return c;
    }
    Matrix power(Matrix A,ll n)
    {
        Matrix ans=I;
        while(n){
            if(n&1)
                ans=multi(ans,A);
            A=multi(A,A);
            n>>=1;
        }
        return ans;
    }
    
    Matrix sum(Matrix A,ll k)
    {
        if(k==1) return A;
        Matrix t=sum(A,k/2);
        Matrix cur=power(A,k/2+(k&1));
        t=add(t,multi(t,cur));
        if(k&1) t=add(t,cur);
        return t;
    }
    
    int main()
    {
        int T;
        ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
        while(cin>>T){
            while(T--){
                cin>>n>>k;
                M=10;
                Matrix A;
                for(int i=0;i<n;i++){
                    for(int j=0;j<n;j++){
                        cin>>A.m[i][j];
                        A.m[i][j]%=M;
                    }
                    I.m[i][i]=1;
                }
                Matrix ans=sum(A,k);
                for(int i=0;i<n;i++){
                    for(int j=0;j<n;j++){
                        if(j+1!=n) cout<<ans.m[i][j]<<" ";
                        else cout<<ans.m[i][j]<<endl;
                    }
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    [转载]游戏外挂原理
    python cookbook
    RF user guide
    测试理论-selenium的工作原理
    测试理论- the conten of test plan
    测试理论--branch testing and boundary testing
    测试理论--向高级出发
    fiddler安装及配置
    python 面试题: 列表表达式
    [redis]如何将redis设置成diango的cache backend
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/7857582.html
Copyright © 2011-2022 走看看