矩阵封装模板+快读

· · 算法·理论

#include<bits/stdc++.h>
using namespace std;

namespace fastio{
    #ifndef ONLINE_JUDGE
      #define gc() getchar()
    #else
      char buf[1<<20],*p1=buf,*p2=buf;
      #define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin)),p1==p2?EOF:*p1++)
    #endif
    template <typename T> 
    inline void read(T& x){
        x=0;
        int c=gc();
        bool f=0;
        while(c!='-'&&(c<'0'||c>'9')) c=gc();
        if(c=='-') c=gc(),f=1; 
        while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^48),c=gc();
        if(f) x=-x;
    }

    template <typename T>
    inline void write(T x){
        if(x<0) putchar('-'),x=-x;
        if(x>9) write(x/10);
        putchar(x%10+'0');
    }
}
using fastio::read;
using fastio::write;
//快读快写 

template <typename T,int n,int m>//数据类型,矩阵行,列 
struct Mat{
    int len1=n,len2=m;
    T a[n+2][m+2];

    Mat(){
       for(int i=1;i<=n;i++)
          for(int j=1;j<=m;j++)
             a[i][j]=0;
    }//c初始化 

    void build(){
        if(n!=m) return ;
        for(int i=1;i<=n;i++) a[i][i]=1;
    }//构建单位矩阵 

    void rd(){
        for(int i=1;i<=n;i++)
           for(int j=1;j<=m;j++)
              read(a[i][j]);
    }//读入矩阵 

    void wr(){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++) write(a[i][j]),putchar(' ');
            putchar('\n');
        }
    }//输出矩阵 

    Mat operator +(const Mat& x)const{
        Mat res;
        if(len1!=x.len1||len2!=x.len2) return res;
        for(int i=1;i<=n;i++)
           for(int j=1;j<=m;j++)
              res.a[i][j]=a[i][j]+x.a[i][j];
        return res; 
    }//加法 

    Mat operator -(const Mat& x)const{
        Mat res;
        if(len1!=x.len1||len2!=x.len2) return res;
        for(int i=1;i<=n;i++)
           for(int j=1;j<=m;j++)
              res.a[i][j]=a[i][j]-x.a[i][j];
        return res; 
    }//减法

    template <typename T1>
    Mat operator *(T1 x)const{
        Mat res;
        for(int i=1;i<=n;i++)
           for(int j=1;j<=m;j++)
              res.a[i][j]=a[i][j]*x;
        return res; 
    }//乘以整数 

    template <typename T1>
    Mat operator /(T1 x)const{
        Mat res;
        for(int i=1;i<=n;i++)
           for(int j=1;j<=m;j++)
              res.a[i][j]=a[i][j]/x;
        return res; 
    }//除以整数 

    template <typename T1>
    Mat operator %(T1 x)const{
        Mat res;
        for(int i=1;i<=n;i++)
           for(int j=1;j<=m;j++)
              res.a[i][j]=a[i][j]%x;
        return res; 
    }//取模

    template <int k>
    Mat<T,n,k> operator*(const Mat<T,m,k>& x)const{
        Mat<T,n,k> res; 
        for(int i=1;i<=n;i++)
            for(int j=1;j<=k;j++)
                for(int p=1;p<=m;p++)
                    res.a[i][j]+=a[i][p]*x.a[p][j];
        return res;
    }//矩阵乘法
}; 

template <typename T,typename T1,int n>
Mat<T,n,n> mat_pow(Mat<T,n,n> a,T1 b){
    Mat<T,n,n> res;
    res.build();
    while(b){
        if(b&1) res=res*a;
        a=a*a,b>>=1;
    }
    return res;
}//矩阵快速幂

template <typename T,typename T1,typename T2,int n>
Mat<T,n,n> mat_pow(Mat<T,n,n> a,T1 b,T2 mod){
    Mat<T,n,n> res;
    res.build();
    while(b){
        if(b&1) res=res*a%mod;
        a=a*a%mod,b>>=1;
    }
    return res;
}//矩阵快速幂+取模 

int main(){
    Mat<long long,2,2> a,b;
    a.rd(),b.rd();
    (a+b).wr();
    return 0;
}