zoukankan      html  css  js  c++  java
  • 初学矩阵快速幂

    洛谷 P3390
    在这里插入图片描述
    在这里插入图片描述

    #include<bits/stdc++.h>
    using namespace std;
    #define maxn 300005
    #define ll long long
    const int mod = 1e9+7;
    
    int n;
    ll k;
    struct Mat{
        ll m[105][105]; 
        Mat(){
            for(int i=1;i<=n;i++)
                for(int j=1;j<=n;j++)
                    m[j][i]=0;
        }
        inline void build(){
            for(int i=1;i<=n;i++)m[i][i]=1;
        }
    }a;
    
    Mat Mul(Mat x,Mat y)
    {
        Mat c;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                for(int k=1;k<=n;k++)
                    c.m[i][j]=(c.m[i][j]%mod+x.m[i][k]*y.m[k][j]%mod)%mod;
        return c; 
    }
    
    Mat poww(Mat x,ll y){
        Mat ans;ans.build();
        while(y){
            if(y&1)ans=Mul(ans,x);
            x=Mul(x,x);
            y>>=1;
        }
        return ans;
    }
    
    int main()
    {
        scanf("%d %lld",&n,&k);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                scanf("%lld",&a.m[j][i]);
        Mat sum=poww(a,k);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++)
                printf("%lld ",sum.m[j][i]);
            printf("
    ");
        }
        return 0;
    }

    POJ 3070

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    #define maxn 300005
    #define ll long long
    const int mod = 10000;
    
    ll n;
    struct Mat{
        ll m[3][3];
        Mat(){
            memset(m,0,sizeof(m));
        }
        inline void build(){
            for(int i=1;i<=2;i++)m[i][i]=1;
        }
    }a;
    
    Mat Mul(Mat x,Mat y){
        Mat c;
        for(int i=1;i<=2;i++)
            for(int j=1;j<=2;j++)
                for(int k=1;k<=2;k++)
                    c.m[i][j]=(c.m[i][j]+x.m[i][k]*y.m[k][j]%mod)%mod;
        return c;
    }
    
    Mat poww(Mat x,ll y){
        Mat aa;aa.build();
        while(y){
            if(y&1)aa=Mul(aa,x);
            x=Mul(x,x);
            y>>=1;
        }
        return aa;
    }
    
    int main()
    {
        while(~scanf("%lld",&n)){
            if(n==-1)break;
            if(n==0)printf("0
    ");
            else if(n==1||n==2)printf("1
    ");
            else{
                a.m[1][1]=1;       //构建矩阵
                a.m[2][1]=1;
                a.m[1][2]=1;
                a.m[2][2]=0;
                Mat ans=poww(a,n-1);
                printf("%lld
    ",ans.m[1][1]);
            }
        }
        return 0;
    }
    希望用自己的努力为自己赢得荣誉。
  • 相关阅读:
    poj2528 Mayor's posters(线段树区间修改+特殊离散化)
    codeforces 733D Kostya the Sculptor(贪心)
    codeforces 733C Epidemic in Monstropolis
    poj 2828--Buy Tickets(线段树)
    lightoj 1125
    HDU 2795 Billboard (线段树)
    hdu 5945 Fxx and game(dp+单调队列! bc#89)
    poj3666 Making the Grade(基础dp + 离散化)
    codeforces 652D
    lightoj 1140
  • 原文地址:https://www.cnblogs.com/Mmasker/p/11917477.html
Copyright © 2011-2022 走看看