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 ...
随机推荐
- Flutter 容器 (2) - Padding
Padding: 内边距Widget,与CSS中的padding相似. import 'package:flutter/material.dart'; class AuthList extends S ...
- python 文件读写with open模式r,r+,w,w+,a,a+的区别
模式 可做操作 若文件不存在 是否覆盖 r 只能读 报错 - r+ 可读可写 报错 是 w 只能写 创建 是 w+ 可读可写 创建 是 a 只能写 创建 否,追加写 a+ 可读可写 创建 否,追加写
- JS实例—DOM的增删改
<!DOCTYPE html><html lang="zh"><head> <meta charset="UTF-8" ...
- Spring Boot 2.x基础教程:使用集中式缓存Redis
之前我们介绍了两种进程内缓存的用法,包括Spring Boot默认使用的ConcurrentMap缓存以及缓存框架EhCache.虽然EhCache已经能够适用很多应用场景,但是由于EhCache是进 ...
- vue-cli 安装教程(转)
vue-cli这个构建工具大大降低了webpack的使用难度,支持热更新,有webpack-dev-server的支持,相当于启动了一个请求服务器,给你搭建了一个测试环境,只关注开发就OK. 1.安装 ...
- linux修改最大的文件描述符(max file descriptors)
用xshell登录linux系统之后,用命令>ulimit -a 注意到系统模式是1024个 使用>ulimit -n 数量,可临时更改,生效范围为当前会话 永久修改的方法: > v ...
- SpringBoot集成Junit
1.在pom.xml下添加Junit依赖: <!--添加junit环境的jar包--> <dependency> <groupId>org.springframew ...
- JavaScript学习系列博客_11_JavaScript中的for语句
for循环 - 语法: for(①初始化表达式 ; ②条件表达式 ; ④更新表达式){ ③语句... } - 执行流程: 首先执行①初始化表达式,初始化一个变量,(这里只会执行一次) 然后对②条件表达 ...
- 简单实现C++Stack模板
栈的特点是先进后出以及只能在栈顶进行插入和删除操作 本次实现的栈的基本操作: 1)弹栈 2)压栈 3)求栈大小 4)求栈容量 5)判断栈空 6)获取栈顶元素 1.用数组的方式实现栈基本操作 /** * ...
- 我是怎样刚拿饿了么P7 offer,然后途虎一轮游的
今年初拿了个饿了么P7的offer,于此同时大家顺便看看我怎么途虎一轮游的.废话不多说,直接上题吧. 一面 首先上来就是自我介绍,简单的说下自己的项目经验,涉及的技术栈之类的. 然后每一轮必问的问题来 ...