[矩阵乘法] 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 ...
随机推荐
- modal over table bug
modal over table bug table can not clickable bad <el-row> <el-col :span="24"> ...
- Bootstrap5 多级dropdown
<div class="dropdown"> <a class="btn dropdown-toggle"> Dropdown link ...
- outlook & email & animation
outlook & email & animation tada position div
- css整理之-----------布局相关
文档流 文档流指的是元素排版布局过程中,元素会默认自动从左往右,从上往下的流式排列方式布局,文档流可以分为定位流.浮动流.普通流三种 普通流(Normal flow) 在常规流中,盒一个接着一个排列, ...
- 高性能环形队列框架 Disruptor 核心概念
高性能环形队列框架 Disruptor Disruptor 是英国外汇交易公司LMAX开发的一款高吞吐低延迟内存队列框架,其充分考虑了底层CPU等运行模式来进行数据结构设计 (mechanical s ...
- Python学习相关链接
感觉挺全的: http://www.cnblogs.com/xinshiye/p/9015187.html 也挺全的:http://www.cnblogs.com/toutou/category/72 ...
- 低功耗蓝牙 ATT/GATT/Service/Characteristic 规格解读
什么是蓝牙service和characteristic?如何理解蓝牙profile? ATT和GATT两者如何区分?什么是attribute? attribute和characteristic的区别是 ...
- ADT基础(一)—— List,Stack,and Queue
ADT基础(一)-- List,Stack,and Queue 1 List 表示 数组:易于search,难于insert和remove 链表:难于search,易于insert和remove // ...
- SpringCloud(四):服务注册中心Eureka Eureka高可用集群搭建 Eureka自我保护机制
第四章:服务注册中心 Eureka 4-1. Eureka 注册中心高可用集群概述在微服务架构的这种分布式系统中,我们要充分考虑各个微服务组件的高可用性 问题,不能有单点故障,由于注册中心 eurek ...
- 后端程序员之路 24、Redis hiredis
Redishttps://redis.io/ Redis快速入门 - Redis教程http://www.yiibai.com/redis/redis_quick_guide.html wget ht ...