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(矩阵乘法)的更多相关文章

  1. C++-POJ3233-Matrix Power Series[矩阵乘法][快速幂]

    构造矩阵 #include <cstdio> ; struct Matrix{int a[MAXN][MAXN];}O,I;int N; ;i<MAXN;i++);j<MAXN ...

  2. Poj 3233 Matrix Power Series(矩阵乘法)

    Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Description Given a n × n matrix A and ...

  3. POJ3233 [C - Matrix Power Series] 矩阵乘法

    解题思路 题目里要求\(\sum_{i=1}^kA^i\),我们不妨再加上一个单位矩阵,求\(\sum_{i=0}^kA^i\).然后我们发现这个式子可以写成这样的形式:\(A(A(A...)+E)+ ...

  4. POJ3233 Matrix Power Series 矩阵乘法

    http://poj.org/problem?id=3233 挺有意思的..学习到结构体作为变量的转移, 题意 : 给定矩阵A,求A + A^2 + A^3 + ... + A^k的结果(两个矩阵相加 ...

  5. POJ3233Matrix Power Series(矩阵快速幂)

    题意 题目链接 给出$n \times n$的矩阵$A$,求$\sum_{i = 1}^k A^i $,每个元素对$m$取模 Sol 考虑直接分治 当$k$为奇数时 $\sum_{i = 1}^k A ...

  6. POJ3233Matrix Power Series(十大矩阵问题之三 + 二分+矩阵快速幂)

    http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total ...

  7. C++题解:Matrix Power Series ——矩阵套矩阵的矩阵加速

    Matrix Power Series r时间限制: 1 Sec 内存限制: 512 MB 题目描述 给定矩阵A,求矩阵S=A^1+A^2+--+A^k,输出矩阵,S矩阵中每个元都要模m. 数据范围: ...

  8. poj 3233 Matrix Power Series(矩阵二分,高速幂)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 15739   Accepted:  ...

  9. POJ3233 Matrix Power Series 矩阵快速幂 矩阵中的矩阵

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 27277   Accepted:  ...

随机推荐

  1. 关于Python中的类普通继承与super函数继承

    关于Python中的类普通继承与super函数继承 1.super只能用于新式类 2.多重继承super可以保公共父类仅被执行一次 一.首先看下普通继承的写法 二.再看看super继承的写法 参考链接 ...

  2. MySQL操作数据库和表的基本语句(DDL

    1.创建数据库: CREATE DATABASE 数据库名; eg.CREATE DATABASE test_ddl;122.创建表 CREATE TABLE 表名(列名 数据类型 约束,...); ...

  3. CentOS 7安装JDK 1.8

    1. 首先查看当前Linux系统是否安装Java ``` rpm -qa | grep java ``` 2. 如果列表显示有,则使用命令将其卸载 rpm -e --nodeps 要卸载的软件名 或 ...

  4. Spring Boot的常见配置项解析

    1.spring-boot-starter-parent:springboot官方推荐的maven管理工具,最简单的做法就是继承它. spring-boot-starter-parent包含了以下信息 ...

  5. 重庆OI2017 老 C 的任务

    老 C 的任务 时间限制: 2 Sec  内存限制: 512 MB 题目描述 老 C 是个程序员. 最近老 C 从老板那里接到了一个任务——给城市中的手机基站写个管理系统.作为经验丰富的程序员,老 C ...

  6. springCloud学习-分布式配置中心(Spring Cloud Config)

    1.简介 Spring Cloud Config :分布式配置中心,方便服务配置文件统一管理,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中.在spring cloud co ...

  7. hdu_1041_Computer Transformation_201311051648

    Computer Transformation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/ ...

  8. 洛谷 P3078 [USACO13MAR]扑克牌型Poker Hands

    P3078 [USACO13MAR]扑克牌型Poker Hands 题目描述 Bessie and her friends are playing a unique version of poker ...

  9. vjudge B - Design T-Shirt

    B - Design T-Shirt 思路:水题,模拟即可. #include<cstdio> #include<cstring> #include<iostream&g ...

  10. memcached集群

    借鉴:http://www.cnblogs.com/happyday56/p/3461113.html 首先说明下memcached存在如下问题 本身没有内置分布式功能,无法实现使用多台Memcach ...