[矩阵乘法] PKU3233 Matrix Power Series
[
矩
阵
乘
法
]
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}
∣∣∣∣AOEE∣∣∣∣
其中:
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矩阵的实现
//全零矩阵的实现
//matrix 是已经定义的结构体,n和m是表示矩阵的长和宽,t是矩阵的值
matrix O (int re)
{
matrix c;
c.n = c.m = re;
for (int i = 1; i <= re; ++ i)
for (int j = 1; j <= re; ++ j)
c.t[i][j] = 0;
return c;
}
//对角线矩阵的实现
//matrix 是已经定义的结构体,n和m是表示矩阵的长和宽,t是矩阵的值,O函数为前文定义的全零矩阵
matrix E (int re)
{
matrix c;
c.n = c.m = re;
c = O (re);
for (int i = 1; i <= re; ++ i)
c.t[i][i] = 1;
return a;
}
//关于矩阵的合并。n,m,t,O(),E()前文已述,T1就是前文提到的T矩阵,re为前文提到的r,a是前文提到的A
matrix hb (int re)
{
t1.n = t1.m = re * 2;
for (int i = 1; i <= re; ++ i)
for (int j = 1; j <= re; ++ j)
t1.t[i][j] = a.t[i][j];
matrix er = E (re);
for (int i = 1; i <= re; ++ i)
for (int j = re + 1; j <= re * 2; ++ j)
t1.t[i][j] = er.t[i][j];
for (int i = re + 1; i <= re * 2; ++ i)
for (int j = re + 1; j <= re * 2; ++ j)
t1.t[i][j] = er.t[i][j];
for (int i = re + 1; i <= re * 2; ++ i)
for (int j = 1; j <= re; ++ j)
t1.t[i][j] = 0;
}
[矩阵乘法] PKU3233 Matrix Power Series的更多相关文章
- Poj 3233 Matrix Power Series(矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Description Given a n × n matrix A and ...
- POJ 3233 Matrix Power Series (矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 11954 Accepted: ...
- 线性代数(矩阵乘法):POJ 3233 Matrix Power Series
Matrix Power Series Description Given a n × n matrix A and a positive integer k, find the sum S = ...
- 构造矩阵解决这个问题 【nyoj299 Matrix Power Series】
矩阵的又一个新使用方法,构造矩阵进行高速幂. 比方拿 nyoj299 Matrix Power Series 来说 给出这样一个递推式: S = A + A2 + A3 + - + Ak. 让你求s. ...
- [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: ...
- [POJ3233]Matrix Power Series 分治+矩阵
本文为博主原创文章,欢迎转载,请注明出处 www.cnblogs.com/yangyaojia [POJ3233]Matrix Power Series 分治+矩阵 题目大意 A为n×n(n<= ...
- C++题解:Matrix Power Series ——矩阵套矩阵的矩阵加速
Matrix Power Series r时间限制: 1 Sec 内存限制: 512 MB 题目描述 给定矩阵A,求矩阵S=A^1+A^2+--+A^k,输出矩阵,S矩阵中每个元都要模m. 数据范围: ...
- POJ 3233 Matrix Power Series(矩阵快速幂)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 19338 Accepted: 8161 ...
- POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】
任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K To ...
随机推荐
- 微前端 & 微前端实践 & 微前端教程
微前端 & 微前端实践 & 微前端教程 微前端 micro frontends https://micro-frontends.org/ https://github.com/neul ...
- Travis CI in Action
Travis CI in Action node.js https://docs.travis-ci.com/user/tutorial/ https://docs.travis-ci.com/use ...
- vue & dynamic components
vue & dynamic components https://vuejs.org/v2/guide/components-dynamic-async.html keep-alive htt ...
- 【C#】反射的用法及效率对比
反射实例化类 public class Person { public string Name { get; set; } public Person(string name) { this.Name ...
- .net使用CSRedis操作Redis缓存的简单笔记(新手教程)
0.介绍 .NET Core or .NET Framework 4.0+ client for Redis and Redis Sentinel (2.8) and Cluster. Include ...
- 使用hive增量更新
目录 1.增量更新 2.对第一种情况 2.1.准备工作 2.2.更新数据 3.对第二种情况 3.1.准备工作 3.2.方法1 3.3.方法2 参考文末文章,加上自己的理解. 1.增量更新 有一个 ba ...
- WPF -- 一种直线识别方案
本文介绍一种直线的识别方案. 步骤 使用最小二乘法回归直线: 得到直线方程y=kx+b后,计算所有点到直线的距离,若在阈值范围内,认为是直线. 实现 /// <summary> /// 最 ...
- Docker搭建Hadoop环境
文章目录 Docker搭建Hadoop环境 Docker的安装与使用 拉取镜像 克隆配置脚本 创建网桥 执行脚本 Docker命令补充 更换镜像源 安装vim 启动Hadoop 测试Word Coun ...
- MySQL:字段约束与索引
字段约束 MySQL的字段约束共四种: 约束名 关键字 描述 主键约束 PRIMARY KEY 字段值唯一,且不能为NULL 非空约束 NOT NULL 字段值不能为NULL 唯一约束 UNIQUE ...
- 图文详解:阿里宠儿【小兔】RabbitMQ的养成攻略