[矩阵乘法] 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 ...
随机推荐
- SVG & Sprite & symbol & use
SVG & Sprite & symbol & use https://www.zhangxinxu.com/sp/svgo/ https://www.zhangxinxu.c ...
- Masterboxan INC金融:在区块链技术基础上推动业务模式的变革创新
10月初,2020年国际区块链技术与应用大会在硅谷开幕,全球内外区块链技术项目团队.行业领导.专家等共聚一堂,围绕区块链技术与应用展开讨论交流.美国Masterboxan INC万事达资产管理有限公司 ...
- 阿里面试这样问:redis 为什么把简单的字符串设计成 SDS?
2021开工第一天,就有小伙伴私信我,还给我分享了一道他面阿里的redis题(这家伙绝比已经拿到年终奖了),我看了以后觉得挺有意思,题目很简单,是那种典型的似懂非懂,常常容易被大家忽略的问题.这里整理 ...
- node_puppeteer无界爬虫
环境:node----v14.5.0 vscode----2019 依赖库 (需要自行设置好目录结构,否则会报目录错误) const puppeteer = require("puppete ...
- SpringCloud Stream
1.介绍 官网:https://www.springcloud.cc/spring-cloud-dalston.html#_spring_cloud_stream 1.1定义 是一个构建消息驱动微服务 ...
- Java后台防止客户端重复请求、提交表单
前言 在Web / App项目中,有一些请求或操作会对数据产生影响(比如新增.删除.修改),针对这类请求一般都需要做一些保护,以防止用户有意或无意的重复发起这样的请求导致的数据错乱. 常见处理方案 1 ...
- Linux 安装python 模块及库
转载于https://blog.csdn.net/csdn_am/article/details/79924744 有时我们使用下载python 自带的pip 安装一些工具包时,会报如下错误 找不到满 ...
- 剑指 Offer 51. 数组中的逆序对 + 归并排序 + 树状数组
剑指 Offer 51. 数组中的逆序对 Offer_51 题目描述 方法一:暴力法(双层循环,超时) package com.walegarrett.offer; /** * @Author Wal ...
- Mongo的相关语法
mongod的条件操作符 $gt -------- greater than > $gte --------- gt equal >= $lt -------- less than < ...
- 再探命令行传参之c与python
继上一次java命令行传参 python sys模块包括了一组非常实用的服务,内含很多函数方法和变量,用来处理Python运行时配置以及资源,从而可以与前当程序之外的系统环境交互,如:python解释 ...