Matrix Power Series
Time Limit: 3000MS   Memory Limit: 131072K
Total Submissions: 11954   Accepted: 5105

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

Source

POJ Monthly--2007.06.03, Huang, Jinsong
 
 
题意:已知一个n*n的矩阵A,和一个正整数k,求S = A + A2 + A3 + … + Ak

 
思路:矩阵快速幂。首先我们知道 A^x 可以用矩阵快速幂求出来(具体可见poj 3070)。其次可以对k进行二分,每次将规模减半,分k为奇偶两种情况,如当k = 6和k = 7时有:
      k = 10 有: S(9) = ( A^1+A^2+A^3+A^4+ A^5 ) + A^5 * ( A^1+A^2+A^3+A^4+A^5 ) = S(5) + A^5 * S(5) 
      k = 5 有: S(5) = ( A^1+A^2 ) + A^3 + A^3 * ( A^1+A^2 ) = S(2) + A^3 + A^3 * S(2)
    k = 2 有 :  S(2) = A^1 + A^2 = S(1) + A^1 * S(1)
 从上面几个式子可以发现,当k为奇数或者偶数的区别,具体见代码中的solve函数。(solve函数的功能:递推到底层,也就是到 k = 1时回退,最后一步一步求出,弄懂递推的思想,这题也就明白了),当然定义成数组,然后再进行一些预处理,效率会更高些。
 
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; int n,k,mod; struct Matrix{
int arr[][];
}; Matrix unit,init; Matrix Mul(Matrix a,Matrix b){
Matrix c;
for(int i=;i<n;i++)
for(int j=;j<n;j++){
c.arr[i][j]=;
for(int k=;k<n;k++)
c.arr[i][j]=(c.arr[i][j]+a.arr[i][k]*b.arr[k][j]%mod)%mod;
c.arr[i][j]%=mod;
}
return c;
} Matrix Pow(Matrix a,Matrix b,int x){
while(x){
if(x&){
b=Mul(b,a);
}
x>>=;
a=Mul(a,a);
}
return b;
} Matrix Add(Matrix a,Matrix b){
Matrix c;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
c.arr[i][j]=(a.arr[i][j]+b.arr[i][j])%mod;
return c;
} Matrix solve(int x){
if(x==)
return init;
Matrix res=solve(x/),cur;
if(x&){
cur=Pow(init,unit,x/+);
res=Add(res,Mul(cur,res));
res=Add(res,cur);
}else{
cur=Pow(init,unit,x/);
res=Add(res,Mul(cur,res));
}
return res;
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d%d",&n,&k,&mod)){
for(int i=;i<n;i++)
for(int j=;j<n;j++){
scanf("%d",&init.arr[i][j]);
unit.arr[i][j]=(i==j?:);
}
Matrix res=solve(k);
for(int i=;i<n;i++){
for(int j=;j<n-;j++)
printf("%d ",res.arr[i][j]);
printf("%d\n",res.arr[i][n-]);
}
}
return ;
}

POJ 3233 Matrix Power Series (矩阵乘法)的更多相关文章

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

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

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

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

  3. POJ 3233 Matrix Power Series(矩阵高速功率+二分法)

    职务地址:POJ 3233 题目大意:给定矩阵A,求A + A^2 + A^3 + - + A^k的结果(两个矩阵相加就是相应位置分别相加).输出的数据mod m. k<=10^9.     这 ...

  4. poj 3233 Matrix Power Series 矩阵求和

    http://poj.org/problem?id=3233 题解 矩阵快速幂+二分等比数列求和 AC代码 #include <stdio.h> #include <math.h&g ...

  5. POJ 3233 Matrix Power Series 矩阵快速幂

    设S[k] = A + A^2 +````+A^k. 设矩阵T = A[1] 0 E E 这里的E为n*n单位方阵,0为n*n方阵 令A[k] = A ^ k 矩阵B[k] = A[k+1] S[k] ...

  6. POJ 3233 Matrix Power Series 矩阵快速幂+二分求和

    矩阵快速幂,请参照模板 http://www.cnblogs.com/pach/p/5978475.html 直接sum=A+A2+A3...+Ak这样累加肯定会超时,但是 sum=A+A2+...+ ...

  7. POJ 3233 Matrix Power Series(矩阵等比求和)

    题目链接 模板题. #include <cstdio> #include <cstring> #include <iostream> #include <ma ...

  8. 矩阵十点【两】 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的迹(就是主对角线上各项的 ...

  9. POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】

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

随机推荐

  1. 第一章 HttpClient的使用

    1.http协议(这一块儿有时间的话会做记录) 2.常用的两种RPC方式 基于http协议:HttpClient和JDK自己的Http操作类 基于TCP或UDP协议:mina2和netty(这一部分以 ...

  2. go语言基础之map介绍和使用

    1.map介绍 Go语言中的map(映射.字典)是一种内置的数据结构,它是一个无序的key—value对的集合,比如以身份证号作为唯一键来标识一个人的信息. 2.map示例 map格式为: map[k ...

  3. go的基结构体如何使用派生结构体的方法

    将派生类的方法声明为接口嵌入到基结构体中,派生结构体声明该接口为自身.

  4. 初识EntityFramework6

    初识EntityFramework6 什么是EF? EF是一种ORM(Object-relational mapping)框架,它能把我们在编程时使用对象映射到底层的数据库结构.比如,你可以在数据库中 ...

  5. C#: 实现支持断点续传多线程下载

    /* .Net/C#: 实现支持断点续传多线程下载的 Http Web 客户端工具类 (C# DIY HttpWebClient)* Reflector 了一下 System.Net.WebClien ...

  6. sass与less

    刚刚发现sass这个东西,前端真热闹,下面比较一下这两者的共同点与区别. 开头总结一下,方便记忆:sass依赖后端计算能力,less依赖客户端的计算能力. 很多开发者不选择LESS是因为LESS输出修 ...

  7. VS2008:Failed to return new Code Element

    VS2008添加自动化类,报错:   [解决方法1] This can be fixed by installing SP1. Please see  https://connect.microsof ...

  8. Web.config中加了system.diagnostics节点后就不能访问了

    Web.config中加了system.diagnostics节点后就不能访问了,怎么回事? [解决方法] 不要把system.diagnostics节点作为web.config的第一个节点.

  9. elasticSearch nested exist与missing查询

    elasticSearch nested查询,简单意义上,你可以理解为,它不会被索引,只是被暂时隐藏起来,而查询的时候,开关就是使用nested query/filter去查询 下面我有一个例子,是查 ...

  10. div css水平垂直居中

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...