poj3233Matrix Power Series(矩阵乘法)
Matrix Power Series
| Time Limit: 3000MS | Memory Limit: 131072K | |
| Total Submissions: 23187 | Accepted: 9662 |
Description
Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak.
Input
The input contains exactly one test case. The first line of input contains three positive integers n (n ≤ 30), k (k ≤ 109) and m (m < 104). Then follow n lines each containing n nonnegative integers below 32,768, giving A’s elements in row-major order.
Output
Output the elements of S modulo m in the same way as A is given.
Sample Input
2 2 4
0 1
1 1
Sample Output
1 2
2 3
/*
矩阵乘法经典+二分
Sk=A+A2+A3+...+Ak
=(1+Ak/2)*(A+A2+A3+...+Ak/2)+{Ak}
=(1+Ak/2)*(Sk/2)+{Ak}
当k为偶数时不要大括号里面的数
*/
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std;
int n,m,k;
struct matrix
{
int a[][];
void init()
{
memset(a,,sizeof a);
for(int i=;i<;i++) a[i][i]=;
}
}; void print(matrix s)
{
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
if (j)
printf(" ");
printf("%d",s.a[i][j]%m);
}
printf("\n");
}
} matrix m_add(matrix a,matrix b)//加法
{
matrix c;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
c.a[i][j]=((a.a[i][j]+b.a[i][j])%m);
return c;
} matrix m_mul(matrix a,matrix b)//乘法
{
matrix c;
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
c.a[i][j]=;
for(int k=;k<n;k++)
c.a[i][j]+=((a.a[i][k]*b.a[k][j])%m);
c.a[i][j]%=m;
}
}
return c;
} matrix mul(matrix s,int k)//矩阵快速幂
{
matrix ans;ans.init();
while(k>=)
{
if(k&) ans=m_mul(ans,s);
k>>=;
s=m_mul(s,s);
}
return ans;
} matrix sum(matrix s,int k)//矩阵前k项求和
{
if(k==) return s;
matrix tmp;tmp.init();
tmp=m_add(tmp,mul(s,k>>));//计算1+A^(k/2)
tmp=m_mul(tmp,sum(s,k>>));//计算(1+A^(k/2))*(A+A^2+A^3+...+A^(k/2))
if(k&) tmp=m_add(tmp,mul(s,k));
return tmp;
} int main()
{
while(cin>>n>>k>>m)
{
matrix s;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
scanf("%d",&s.a[i][j]);
s=sum(s,k);
print(s);
}
}
poj3233Matrix Power Series(矩阵乘法)的更多相关文章
- C++-POJ3233-Matrix Power Series[矩阵乘法][快速幂]
构造矩阵 #include <cstdio> ; struct Matrix{int a[MAXN][MAXN];}O,I;int N; ;i<MAXN;i++);j<MAXN ...
- Poj 3233 Matrix Power Series(矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Description Given a n × n matrix A and ...
- POJ3233 [C - Matrix Power Series] 矩阵乘法
解题思路 题目里要求\(\sum_{i=1}^kA^i\),我们不妨再加上一个单位矩阵,求\(\sum_{i=0}^kA^i\).然后我们发现这个式子可以写成这样的形式:\(A(A(A...)+E)+ ...
- POJ3233 Matrix Power Series 矩阵乘法
http://poj.org/problem?id=3233 挺有意思的..学习到结构体作为变量的转移, 题意 : 给定矩阵A,求A + A^2 + A^3 + ... + A^k的结果(两个矩阵相加 ...
- POJ3233Matrix Power Series(矩阵快速幂)
题意 题目链接 给出$n \times n$的矩阵$A$,求$\sum_{i = 1}^k A^i $,每个元素对$m$取模 Sol 考虑直接分治 当$k$为奇数时 $\sum_{i = 1}^k A ...
- POJ3233Matrix Power Series(十大矩阵问题之三 + 二分+矩阵快速幂)
http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total ...
- 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: 15739 Accepted: ...
- POJ3233 Matrix Power Series 矩阵快速幂 矩阵中的矩阵
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 27277 Accepted: ...
随机推荐
- 2016.01.22 前端学习 HTML/CSS
学习HTML/CSS http://edu.51cto.com/course/course_id-3116.html 明日实践
- session_start(): open(/var/lib/php/session/sess_tlrp3cpro7gun9uhno8n6futr3, O_RDWR)
在安装一个网站,结果连接上数据库了但是一直报错.下面贴报错内容和图 错误提示 系统:PHP (Linux) 错误类型:WARNING 内容:session_start(): open(/) 错误位置: ...
- CAD取Excel表格(com接口)
1 2 3 4 5 6 7 8 9 10 11 12 MxDrawResbuf ret = (MxDrawResbuf)axMxDrawX1.Call("ExApp_GetExcel&quo ...
- swift-延时加载函数
//延时加载函数 func delayLoad(){ let time: NSTimeInterval = 2.0 let delay = dispatch_time(DISPATCH_TIME_NO ...
- 《啊哈算法》中P81解救小哈
题目描述 首先我们用一个二维数组来存储这个迷宫,刚开始的时候,小哼处于迷宫的入口处(1,1),小哈在(p,q).其实这道题的的本质就在于找从(1,1)到(p,q)的最短路径. 此时摆在小哼面前的路有两 ...
- 洛谷——P2342 叠积木
P2342 叠积木 题目大意: 给你一堆积木,排成一行,初始时每对积木都只有一个,支持两种操作 第一种是移动操作,格式为“移动X到Y的上面”.X和Y代表两块积木的编号,意思是将X所的那堆积 ...
- P4047 [JSOI2010]部落划分(最小生成树)
题目描述 聪聪研究发现,荒岛野人总是过着群居的生活,但是,并不是整个荒岛上的所有野人都属于同一个部落,野人们总是拉帮结派形成属于自己的部落,不同的部落之间则经常发生争斗.只是,这一切都成为谜团了——聪 ...
- Linux之iptables(四、网络防火墙及NAT)
网络防火墙 iptables/netfilter网络防火墙: (1) 充当网关 (2) 使用filter表的FORWARD链 注意的问题: (1) 请求-响应报文均会经由FORWARD链,要注意规则的 ...
- (ccf)201709-4 通信网络
#include<iostream> #include<memory.h> #include<stack> #include<string> #incl ...
- UCloud 的安全秘钥
UCloud 的安全秘钥(困难) 1200ms 262144K 每个 UCloud 用户会构造一个由数字序列组成的秘钥,用于对服务器进行各种操作.作为一家安全可信的云计算平台,秘钥的安全性至关重要.因 ...