[

]

M

a

t

r

i

x

P

o

w

e

r

S

e

r

i

e

s

[矩阵乘法]Matrix Power Series

[矩阵乘法]MatrixPowerSeries

Description

Given a

n

×

n

n × n

n×n matrix

A

A

A and a positive integer

k

k

k, find the sum

S

=

A

+

A

2

+

A

3

+

.

.

.

+

A

k

S = A + A^2 + A^3 + ... + A^k

S=A+A2+A3+...+Ak.

Input

The input contains exactly one test case. The first line of input contains three positive integers

n

n

n (

n

n

n ≤

30

30

30),

k

k

k (

k

k

k ≤

1

0

9

10^9

109) and

m

m

m (

m

m

m <

1

0

4

10^4

104). Then follow n lines each containing

n

n

n nonnegative integers below

32

,

768

32,768

32,768, giving

A

A

A’s elements in row-major order.

Output

Output the elements of

S

S

S modulo m in the same way as

A

A

A is given.

Sample Input

2 2 4
0 1
1 1

Sample Output

1 2
2 3


题目解析

为了降低时间复杂度,考虑矩阵乘法

然后可以构造出一个

2

r

2r

2r阶的矩阵

T

T

T

A

E

O

E

\begin{vmatrix} A & E \\ O & E \\ \end{vmatrix}

∣∣∣∣​AO​EE​∣∣∣∣​

其中:

A

A

A为输入的矩阵(

A

A

A是

r

r

r阶的矩阵)

O

O

O为全零矩阵 (

O

O

O是值全为

0

0

0的

r

r

r阶矩阵)

E

E

E为对角线矩阵(

E

E

E是除了对角线为

1

1

1,其他的都为

0

0

0的矩阵)

然后可以得出:

S

[

n

1

]

,

A

n

=

S

[

n

2

]

,

A

n

1

T

|S[n-1],A^n| = |S[n-2],A^{n-1}| * T

∣S[n−1],An∣=∣S[n−2],An−1∣∗T

然后通过将矩阵乘法的结合律通过快速幂来计算出

T

n

T^n

Tn再可

A

T

n

A*T^n

A∗Tn来求得答案


关于

T

T

T矩阵的实现

  1. //全零矩阵的实现
  2. //matrix 是已经定义的结构体,n和m是表示矩阵的长和宽,t是矩阵的值
  3. matrix O (int re)
  4. {
  5. matrix c;
  6. c.n = c.m = re;
  7. for (int i = 1; i <= re; ++ i)
  8. for (int j = 1; j <= re; ++ j)
  9. c.t[i][j] = 0;
  10. return c;
  11. }
  1. //对角线矩阵的实现
  2. //matrix 是已经定义的结构体,n和m是表示矩阵的长和宽,t是矩阵的值,O函数为前文定义的全零矩阵
  3. matrix E (int re)
  4. {
  5. matrix c;
  6. c.n = c.m = re;
  7. c = O (re);
  8. for (int i = 1; i <= re; ++ i)
  9. c.t[i][i] = 1;
  10. return a;
  11. }
  1. //关于矩阵的合并。n,m,t,O(),E()前文已述,T1就是前文提到的T矩阵,re为前文提到的r,a是前文提到的A
  2. matrix hb (int re)
  3. {
  4. t1.n = t1.m = re * 2;
  5. for (int i = 1; i <= re; ++ i)
  6. for (int j = 1; j <= re; ++ j)
  7. t1.t[i][j] = a.t[i][j];
  8. matrix er = E (re);
  9. for (int i = 1; i <= re; ++ i)
  10. for (int j = re + 1; j <= re * 2; ++ j)
  11. t1.t[i][j] = er.t[i][j];
  12. for (int i = re + 1; i <= re * 2; ++ i)
  13. for (int j = re + 1; j <= re * 2; ++ j)
  14. t1.t[i][j] = er.t[i][j];
  15. for (int i = re + 1; i <= re * 2; ++ i)
  16. for (int j = 1; j <= re; ++ j)
  17. t1.t[i][j] = 0;
  18. }

[矩阵乘法] PKU3233 Matrix Power Series的更多相关文章

  1. Poj 3233 Matrix Power Series(矩阵乘法)

    Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Description Given a n × n matrix A and ...

  2. POJ 3233 Matrix Power Series (矩阵乘法)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 11954   Accepted:  ...

  3. 线性代数(矩阵乘法):POJ 3233 Matrix Power Series

    Matrix Power Series   Description Given a n × n matrix A and a positive integer k, find the sum S = ...

  4. 构造矩阵解决这个问题 【nyoj299 Matrix Power Series】

    矩阵的又一个新使用方法,构造矩阵进行高速幂. 比方拿 nyoj299 Matrix Power Series 来说 给出这样一个递推式: S = A + A2 + A3 + - + Ak. 让你求s. ...

  5. [ACM] POJ 3233 Matrix Power Series (求矩阵A+A^2+A^3...+A^k,二分求和或者矩阵转化)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 15417   Accepted:  ...

  6. [POJ3233]Matrix Power Series 分治+矩阵

    本文为博主原创文章,欢迎转载,请注明出处 www.cnblogs.com/yangyaojia [POJ3233]Matrix Power Series 分治+矩阵 题目大意 A为n×n(n<= ...

  7. C++题解:Matrix Power Series ——矩阵套矩阵的矩阵加速

    Matrix Power Series r时间限制: 1 Sec 内存限制: 512 MB 题目描述 给定矩阵A,求矩阵S=A^1+A^2+--+A^k,输出矩阵,S矩阵中每个元都要模m. 数据范围: ...

  8. POJ 3233 Matrix Power Series(矩阵快速幂)

    Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 19338 Accepted: 8161 ...

  9. POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】

    任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K To ...

随机推荐

  1. 微前端 & 微前端实践 & 微前端教程

    微前端 & 微前端实践 & 微前端教程 微前端 micro frontends https://micro-frontends.org/ https://github.com/neul ...

  2. Travis CI in Action

    Travis CI in Action node.js https://docs.travis-ci.com/user/tutorial/ https://docs.travis-ci.com/use ...

  3. vue & dynamic components

    vue & dynamic components https://vuejs.org/v2/guide/components-dynamic-async.html keep-alive htt ...

  4. 【C#】反射的用法及效率对比

    反射实例化类 public class Person { public string Name { get; set; } public Person(string name) { this.Name ...

  5. .net使用CSRedis操作Redis缓存的简单笔记(新手教程)

    0.介绍 .NET Core or .NET Framework 4.0+ client for Redis and Redis Sentinel (2.8) and Cluster. Include ...

  6. 使用hive增量更新

    目录 1.增量更新 2.对第一种情况 2.1.准备工作 2.2.更新数据 3.对第二种情况 3.1.准备工作 3.2.方法1 3.3.方法2 参考文末文章,加上自己的理解. 1.增量更新 有一个 ba ...

  7. WPF -- 一种直线识别方案

    本文介绍一种直线的识别方案. 步骤 使用最小二乘法回归直线: 得到直线方程y=kx+b后,计算所有点到直线的距离,若在阈值范围内,认为是直线. 实现 /// <summary> /// 最 ...

  8. Docker搭建Hadoop环境

    文章目录 Docker搭建Hadoop环境 Docker的安装与使用 拉取镜像 克隆配置脚本 创建网桥 执行脚本 Docker命令补充 更换镜像源 安装vim 启动Hadoop 测试Word Coun ...

  9. MySQL:字段约束与索引

    字段约束 MySQL的字段约束共四种: 约束名 关键字 描述 主键约束 PRIMARY KEY 字段值唯一,且不能为NULL 非空约束 NOT NULL 字段值不能为NULL 唯一约束 UNIQUE ...

  10. 图文详解:阿里宠儿【小兔】RabbitMQ的养成攻略