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

    矩阵快速幂模板

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    
    using namespace std;
    typedef long long ll;
    const int lg= 1e9+7;
    const int maxn = 200;
    struct node {
        ll m[maxn][maxn];
    }ans,res;
    
    node Mul(node a,node b,ll n)
    {
        node tmp;
        memset(tmp.m,0,sizeof(tmp));
        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]%lg)%lg;
            }
        }
        return tmp;
    }
    void jzksm(ll n,ll k)
    {
        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(k)
        {
            if(k&1)ans = Mul(ans,res,n);
            res = Mul(res,res,n);
            k>>=1;
        }
    }
    int main(){
        ll n,k;
        scanf("%lld%lld",&n,&k);
        for(int i=1; i<=n; i++)
        {
            for(int j=1;j<=n;j++)
                scanf("%lld",&res.m[i][j]);
        }
        jzksm(n,k);
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
                printf("%lld%c",ans.m[i][j]," 
    "[j==n]);
        }
        return 0;
    }
     
  • 相关阅读:
    搭建wamp环境,数据库基础知识
    练习题:选择器和选择好友
    例子:滑动效果
    TCPDump
    内存
    Cache和Buffer的区别(转载)
    经典问题回忆
    history
    DNS
    bc的用法
  • 原文地址:https://www.cnblogs.com/ckxkexing/p/9050725.html
Copyright © 2011-2022 走看看