Matrix Power Series(构造矩阵+矩阵快速幂)

Matrix Power Series

Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 262144/131072K (Java/Other)
Problem Description

Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak.


Input
<span lang="en-us"><p>The input contains exactly one test case. The first line of input contains three positive integers <i>n</i> (<i>n</i> ≤ 30), <i>k</i> (<i>k</i> ≤ 10<sup>9</sup>) and <i>m</i> (<i>m</i> < 10<sup>4</sup>). Then follow <i>n</i> lines each containing <i>n</i> nonnegative integers below 32,768, giving <i>A</i>’s elements in row-major order.</p></span>
 

Output
<p>Output the elements of <i>S</i> modulo <i>m</i> in the same way as <i>A</i> is given.</p>
 

Sample Input
 
 
2 2 4 0 1 1 1
 

Sample Output
 
 
1 2 2 3
 

题意

给定一个矩阵A,求S = A + A2 + A3 + … + Ak.


思路

设S[k]=A + A2 + A3 + … + Ak.

那么S[n]=A*S[n-1]+A;


=>所求 S=(A,1)*(A,0 )^(k-1)

                          A,1


注意:矩阵套矩阵,里面的1代表单位矩阵,即对角线上元素为1,其余为0。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

typedef long long ll;

typedef struct {
    ll m[120][120];
}Matrix;

ll  n,k,mod;

Matrix Mul(Matrix a, Matrix b)
{
    Matrix c;
    memset(c.m, 0, sizeof(c.m));
    for (int i = 0; i < 2*n; i++)
    {
        for (int j = 0; j < 2*n; j++)
        {
            for (int k = 0; k < 2*n; k++)
            {
                c.m[i][j] = (c.m[i][j] + (a.m[i][k] * b.m[k][j]) % mod ) % mod;
            }
        }
    }
    return c;
}
//矩阵乘法
Matrix solve(Matrix a, Matrix b)
{
    Matrix c;
    memset(c.m, 0, sizeof(c.m));
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < 2*n; j++)
        {
            for (int k = 0; k < 2*n; k++)
            {
                c.m[i][j] = (c.m[i][j] + (a.m[i][k] * b.m[k][j]) % mod ) % mod;
            }
        }
    }
    return c;
}

Matrix fastm(Matrix a, ll num)
{
    Matrix res;
    memset(res.m, 0, sizeof(res.m));
    //初始化为单位矩阵
    for(int i=0;i<2*n;i++)
        res.m[i][i]=1;
    while (num)
    {
        if (num & 1)
            res = Mul(res, a);
        num >>= 1;
        a = Mul(a, a);
    }
    return res;
}

int main()
{
    Matrix a;
    memset(a.m,0,sizeof(a.m));
    scanf("%lld%lld%lld",&n,&k,&mod);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            scanf("%lld",&a.m[i][j]);
            a.m[i][j]=a.m[i][j]%mod;
        }
    }
    Matrix b;
    memset(b.m,0,sizeof(b.m));
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            b.m[i][j]=a.m[i][j];
    for(int i=0;i<n;i++)
        for(int j=n;j<2*n;j++)
            b.m[i][j]=0;
    for(int i=n;i<2*n;i++)
        for(int j=0;j<n;j++)
            b.m[i][j]=a.m[i-n][j];
    //注意:矩阵等于1表示该矩阵是单位矩阵,即主对角线为1,其余为0
    for(int i=n;i<2*n;i++)
            b.m[i][i]=1;
    for(int i=0;i<n;i++)
            a.m[i][i+n]=1;
    b=fastm(b,k-1);
    Matrix ans=solve(a,b);
    for(int i=0;i<n;i++)
    {
        printf("%lld",ans.m[i][0]);
        for(int j=1;j<n;j++)
        {
            printf(" %lld",ans.m[i][j]);
        }
        printf("\n");
    }
    return 0;
}


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PowerBI系列课程之内置视觉对象和常用自定义视觉对象专题 内置视觉对象Stacked bar chart  堆积条形图Stacked column chart 堆积柱形图 - X轴按类别显示Clustered bar chart  簇状条形图Clustered column chart  簇状柱形图100% Stacked bar chart 百分比堆积条形图100% Stacked column chart 百分比堆积柱形图Line Chart  折线图-预测功能Area Chart  面积图Stacked Area Chart  堆积面积图Line and stacked column chart 折线和堆积柱形图-双Y轴Line and clustered column chart 折线和簇状柱形图Ribbon Chart  丝带图Waterfall Chart  瀑布图Funnel  漏斗图Scatter chart  散点图Pie Chart  饼图 Donut Chart 环形图TreeMap 树状图Map  地图 Filled Map 着色地图Shape Map 形状地图Gauge 仪表Card 卡片图Multi-row card 多行卡片KPI  Table 表格-条件样式Matrix 矩阵详解  Key influencers  关键影响者Decomposition tree 分解树Q&A 问答2021.6月增加分页表格视觉对象自定义视觉对象视觉对象使用建议下载排名前20,免费实用的视觉对象 AllDemo pbix介绍 Pareto 帕累托图DrillDown Donut  可钻取饼图Word Cloud   文字云Gantt  甘特图Infographic Designer  信息柱状图Timeline Slicer  时间轴切片器Chiclet Slicer 图片切片器Text Filter 文本筛选器HierarchySlicer  层级切片器Pulse Chart脉动图Power KPI Matrix  KPI矩阵Animated Bar Chart Race 动态条形图Advance Card  高级卡片Sankey Chart  桑基图Radar Chart   雷达图Dial Gauge  码表Waffle 华夫占比图Quadrant 象限图 Tornado Chart  龙卷风图Histogram Chart  直方图 Box and Whisker chart 盒线图Sunburst  阳光图Chord Chart 和弦图Bullet Chart  子弹图HTML Content  html解析视觉对象  

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值