Matrix Power Series(POJ 3233)
- 原题如下:
Matrix Power Series
Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 28044 Accepted: 11440 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 1Sample Output
1 2
2 3 - 题解:构造矩阵:
此时,令Sk=I+A+…+Ak-1,则有:

通过计算这个矩阵的k次幂,就可求出A的累乘和,时间复杂度为O(n3logk) - 代码:
#include <cstdio>
#include <cctype>
#define number s-'0'
#include <cstring>
#include <vector> using namespace std; typedef vector<int> vec;
typedef vector<vec> mat; int n,k,m;
mat A; void read(int &x)
{
char s;
x=;
bool flag=;
while (!isdigit(s=getchar()))
(s=='-')&&(flag=true);
for (x=number; isdigit(s=getchar());x=x*+number);
(flag)&&(x=-x);
} void write(int x)
{
if (x<)
{
putchar('-');
x=-x;
}
if (x>) write(x/);
putchar(x%+'');
} mat mul(mat &A, mat &B)
{
mat C(A.size(), vec(B[].size()));
for (int i=; i<A.size(); i++)
{
for (int j=; j<B[].size(); j++)
{
for (int k=; k<B.size(); k++)
{
C[i][j]=(C[i][j]+A[i][k]*B[k][j])%m;
}
}
}
return C;
} mat pow(mat A, int n)
{
mat B(A.size(), vec(A.size()));
for (int i=; i<A.size(); i++) B[i][i]=;
while (n>)
{
if (n&) B=mul(B, A);
A=mul(A, A);
n>>=;
}
return B;
} int main(int argc, char * argv[])
{
read(n);read(k);read(m);
A=mat (n,vec(n));
mat B(n*, vec(n*));
for (int i=; i<n; i++)
{
for (int j=; j<n; j++)
{
read(A[i][j]);
B[i][j]=A[i][j];
}
B[n+i][i]=B[n+i][n+i]=;
}
B=pow(B,k+);
for (int i=; i<n; i++)
{
for (int j=; j<n; j++)
{
int a=B[n+i][j]%m;
if (i==j) a=(a+m-)%m;
printf("%d%c", a, j+==n?'\n':' ');
}
}
}
Matrix Power Series(POJ 3233)的更多相关文章
- Matrix Power Series POJ - 3233 矩阵幂次之和。
矩阵幂次之和. 自己想着想着就想到了一个解法,但是还没提交,因为POJ崩了,做了一个FIB的前n项和,也是用了这个方法,AC了,相信是可以得. 提交了,是AC的 http://poj.org/prob ...
- POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】
任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K To ...
- 矩阵十点【两】 poj 1575 Tr A poj 3233 Matrix Power Series
poj 1575 Tr A 主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575 题目大意:A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的 ...
- POJ 3233 Matrix Power Series (矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 11954 Accepted: ...
- [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: ...
- 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 Description Given a n × n matrix A and a positive integer k, find the sum S = ...
- POJ 3233 Matrix Power Series(二分等比求和)
Matrix Power Series [题目链接]Matrix Power Series [题目类型]二分等比求和 &题解: 这题我原来用vector写的,总是超时,不知道为什么,之后就改用 ...
- POJ 3233 Matrix Power Series(矩阵快速幂)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 19338 Accepted: 8161 ...
随机推荐
- C#LeetCode刷题之#633-平方数之和( Sum of Square Numbers)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3885 访问. 给定一个非负整数 c ,你要判断是否存在两个整数 ...
- NeuralCoref: python的共指消解工具教程
转载地址 https://blog.csdn.net/blmoistawinde/article/details/81782971 共指消解 首先简要地说说共指消解是什么,有什么用处.假设机器正在阅读 ...
- myBatis源码解析-类型转换篇(5)
前言 开始分析Type包前,说明下使用场景.数据构建语句使用PreparedStatement,需要输入的是jdbc类型,但我们一般写的是java类型.同理,数据库结果集返回的是jdbc类型,而我们需 ...
- Vue源码解析,keep-alive是如何实现缓存的?
前言 在性能优化上,最常见的手段就是缓存.对需要经常访问的资源进行缓存,减少请求或者是初始化的过程,从而降低时间或内存的消耗.Vue 为我们提供了缓存组件 keep-alive,它可用于路由级别或组件 ...
- rabbitmq python demo 参考链接地址
链接地址: https://docs.openstack.org/oslo.messaging/latest/reference/server.html https://www.cnblogs.com ...
- neutron plugin 与 extension 编写流程
原文链接:neutron plugin 与 extension 编写流程 参考: 怎样写 OpenStack Neutron 的 Plugin (一)怎样写 OpenStack Neutron 的 P ...
- MD笔记
1.力场中的例子电荷是有效电荷(clayff),有别于化学式中的电荷. 2.游离状态的阳离子(如层间阳离子)的电荷不能变动:而Al-O八面体.Si-O四面体中的离子(Al.Si等)电荷可以微调. 3. ...
- curl报错60的问题
使用curl发请post请求的时候,会遇到如下错误: curl: (60) SSL certificate problem: self signed certificate More details ...
- jQuery源码分析系列(三)Sizzle选择器引擎-下
选择函数:select() 看到select()函数,if(match.length === 1){}存在的意义是尽量简化执行步骤,避免compile()函数的调用. 简化操作同样根据tokenize ...
- springsession
Spring Session 一. HttpSession 回顾 1 什么是 HttpSession 是 JavaWeb 服务端提供的用来建立与客户端会话状态的对象. 二. Session 共享 1 ...