[矩阵乘法] 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 ...
随机推荐
- Flutter CodePen challenges
Flutter CodePen challenges 挑战赛 https://mp.weixin.qq.com/s/qIYokWN9SVgr-F7YxbJuOQ CodePen Flutter 编辑器 ...
- free online code editor
free online code editor online vscode https://stackblitz.com/ https://codesandbox.io/ https://codesh ...
- Micro Frontends & microservices
Micro Frontends & microservices https://micro-frontends.org/ https://github.com/neuland/micro-fr ...
- MacBook Pro 2019 13 inch & screen blink
MacBook Pro 2019 13 inch & screen blink MacBook Pro 闪屏 https://macreports.com/mac-how-to-trouble ...
- Go 去找个对象吧
前言 我的读者中应该大部分都是 Java 从业者,不知道写 Java 这些年是否真的有找到对象? 没找到也没关系,总不能在一棵树上吊死,我们也可以来 Go 这边看看,说不定会有新发现. 开个玩笑,本文 ...
- python基础(2)字符串常用方法
python字符串常用方法 find(sub[, start[, end]]) 在索引start和end之间查找字符串sub 找到,则返回最左端的索引值,未找到,则返回-1 start和end都可 ...
- 微信小程序:如何实现两个按钮在最右侧并排
要实现的效果: wxml端代码: <view class="prepare_param"> <view clas ...
- ctf.show_web13(文件上传之.user.ini)
这是一道文件上传题,先二话不说丢个图片码,显示为 先考虑文件太小,用burp抓包,添加了一堆无用的东西后显示仍然是error file zise,直到上传正常图片依旧如此,考虑文件太大.将一句话木马修 ...
- Java基础语法:类型转换
由于Java是强类型语言,所以有时候在进行运算时,需要用到类型转换. 整型.常量.字符类型数据可以混合运算. 运算中,不同类型的数据先转化为同一类型,然后再进行运算. 类型转换等级有低级到高级的划分, ...
- 后端程序员之路 47、Hadoop hdfs
Hadoop的核心是HDFS和MapReduce,而两者只是理论基础,不是具体可使用的高级应用,Hadoop旗下有很多经典子项目,比如HBase.Hive等,这些都是基于HDFS和MapReduce发 ...