zoukankan      html  css  js  c++  java
  • luogu_3390 【模板】矩阵快速幂

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    typedef long long LL;
    const LL Mod=1e9+7;
    int n;
    LL k;
    struct Matrix{
        static const int N=101;
        int n;
        LL h[N][N];
        void clear(int n){
            this->n=n;
            memset(h,0,sizeof(h));
        }
        friend ostream& operator << (ostream&,Matrix&);
        friend istream& operator >> (istream&,Matrix&);
        Matrix operator * (const Matrix &t) const{
            Matrix b;b.clear(n);
            for(int i=1;i<=n;i++)
                for(int j=1;j<=n;j++){
                    LL sum=0;
                    for(int k=1;k<=n;k++)sum+=h[i][k]*t.h[k][j],sum%=Mod;
                    b.h[i][j]=sum;
                }
            return b;
        }
        Matrix operator ^ (LL k)const {
            Matrix t=(*this),ans=(*this);
            for(k--;k;k>>=1,t=t*t)
                if(k&1)ans=ans*t;
            return ans;
        }
    }a;
    istream& operator >> (istream& input,Matrix &x){
        for(int i=1;i<=x.n;i++)
            for(int j=1;j<=x.n;j++)input>>x.h[i][j];
        return input;
    }
    ostream& operator << (ostream& output,Matrix &x){
        for(int i=1;i<=x.n;i++){
            for(int j=1;j<=x.n;j++)output<<x.h[i][j]<<" ";
            output<<endl;
        }
        return output;
    }
    int main(){
        ios::sync_with_stdio(false);
        cin>>n>>k;
        a.clear(n);
        cin>>a;
        Matrix ans=a^k;
        cout<<ans;
        return 0;
    }
    

      

  • 相关阅读:
    shell脚本sed的基本用法
    shell grep的基本用法
    禁止表单提示输入--autocomplete属性
    Cookie操作介绍
    JSP中的两种重定向
    SSM
    题解 P4994 【终于结束的起点】
    题解 P1286 【两数之和】
    题解 P2340 【奶牛会展】
    题解 CF450B 【Jzzhu and Sequences】
  • 原文地址:https://www.cnblogs.com/codetogether/p/7575825.html
Copyright © 2011-2022 走看看