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

    先贴个整数快速幂

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long int ll;
    ll b,p,k,ans=1,res;
    
    int main(){
        scanf("%lld%lld%lld",&b,&p,&k);
        cout<<b<<"^"<<p<<" mod "<<k<<"=";
        while(p){
            if(p&1)ans=ans*b%k;
            b=b*b%k;
            p=p>>1;
        }
        cout<<ans%k<<endl;
    }

    矩阵快速幂https://www.cnblogs.com/cmmdc/p/6936196.html

    代码如下

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long int ll;
    const int maxn=1e2+5;
    const ll mod= 1e9+7;
    struct matrix{
        ll m[maxn][maxn];
    }ans,res;
    ll n,N;
    
    matrix mul(matrix a,matrix b){
        matrix tmp;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                tmp.m[i][j]=0;
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                for(int k=1;k<=n;k++){
                    tmp.m[i][j]=(tmp.m[i][j]+(a.m[i][k]*b.m[k][j])%mod)%mod;
                }
            }
        }
        return tmp;
    }
    void quickpow(ll N,ll n){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(i==j)ans.m[i][j]=1;
                else ans.m[i][j]=0;
            }
        }
        while(N){
            if(N&1)ans=mul(res,ans);
            res=mul(res,res);
            N=N>>1;
        }
    }
    int main(){
        scanf("%lld%lld",&n,&N);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                scanf("%lld",&res.m[i][j]);
            }
        }
        quickpow(N,n);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                cout<<ans.m[i][j]<<" ";
            }
            cout<<endl;
        }
        return 0;
    }

     

  • 相关阅读:
    8.CNN应用于手写字识别
    8.优化器
    6.正则化
    5.Dropout
    4.交叉熵
    3.Minst数据集分类
    2.非线性回归
    1.线性回归
    110. Balanced Binary Tree
    106. Construct Binary Tree from Inorder and Postorder Traversal
  • 原文地址:https://www.cnblogs.com/mohari/p/12945187.html
Copyright © 2011-2022 走看看