Matrix Power Series
 

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 思路:1.最基本的,需要用到矩阵快速幂 2.快速幂求完之后怎样快速求和?若逐项累加求和必然会超时,这时需要求递推公式:(1)若n为偶数,则:S(n) = A^(n/2)*S(n/2)+s(n/2);(2)若n为奇数 S(n) = A^(n/2+1) + S(n/2)*A^(n/2+1) + S(n/2),公式不难推,写几个就发现规律了。这样就把时间复杂度降下来了。
 #include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int n, m;
typedef struct Matrix{
int m[][];
Matrix(){
memset(m, , sizeof(m));
}
}Matrix;
Matrix mtAdd(Matrix A, Matrix B){
for(int i = ;i < n;i ++)
for(int j = ;j < n;j ++){
A.m[i][j] += B.m[i][j];
A.m[i][j] %= m;
}
return A;
}
Matrix mtMul(Matrix A, Matrix B){
Matrix tmp;
for(int i = ;i < n;i ++)
for(int j = ;j < n;j ++)
for(int k = ;k < n;k ++){
tmp.m[i][j] += A.m[i][k]*B.m[k][j];
tmp.m[i][j] %= m;
}
return tmp;
}
Matrix mtPow(Matrix A, int k){
if(k == ) return A;
Matrix tmp = mtPow(A, k >> );
Matrix res = mtMul(tmp, tmp);
if(k&) res = mtMul(res, A);
return res;
}
Matrix mtSum(Matrix A, int k){
if(k == ) return A;
Matrix tmp = mtSum(A, k/);
if(k&){
Matrix t = mtPow(A, k/+);
Matrix tmp1 = mtMul(tmp, t);
Matrix tmp2 = mtAdd(t, tmp);
return mtAdd(tmp1, tmp2);
}else return mtAdd(tmp, mtMul(mtPow(A, k/), tmp));
}
int main(){
int k, tmp;
/* freopen("in.c", "r", stdin); */
while(~scanf("%d%d%d", &n, &k, &m)){
Matrix M;
for(int i = ;i < n;i ++)
for(int j = ;j < n;j ++){
scanf("%d", &tmp);
M.m[i][j] = tmp;
}
M = mtSum(M, k);
for(int i = ;i < n;i ++){
for(int j = ;j < n;j ++)
printf("%d ", M.m[i][j]);
puts("");
}
}
return ;
}
												

POJ -- 3233 求“等比矩阵”前n(n <=10^9)项和的更多相关文章

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

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

  2. poj 3233 Matrix Power Series 矩阵求和

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

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

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

  4. 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] ...

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

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

  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 3233 矩阵力量系列

    不要管上面的标题的bug 那是幂的意思,不是力量... POJ 3233 Matrix Power Series 描述 Given a n × n matrix A and a positive in ...

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

随机推荐

  1. css写圆角效果

    .introTips i{ position: absolute; display: block; top: 8px; right: 8px; width:; height:; font-size:; ...

  2. j2ee中如何拦截jsp页面?

    加filter: public class RightFilter implements Filter { public void init(FilterConfig filterConfig) th ...

  3. 一些SVN 地址

    lockbox3 地址: https://svn.code.sf.net/p/tplockbox/code/trunc indy10 地址:https://svn.atozed.com:444/svn ...

  4. [python]使用ElementTree解析XML【译】

    19.7 The ElementTree XML API 源码:Lib/xml/etree/ElementTree.py Element类型是一个灵活的容器对象,设计出来是用于存储有层次的数据结构到内 ...

  5. IOS中利用宏将RGB值转换为UIColor(转)

    可以在pch文件中定义宏,这样整个项目就都可以用了! #define UIColorFromRGBValue(rgbValue) [UIColor colorWithRed:((float)((rgb ...

  6. vi/vim正则表达式

    http://www.cnblogs.com/penseur/archive/2011/02/25/1964522.html 毋庸多言,在vim中正则表达式得到了十分广泛的应用. 最常用的 / 和 : ...

  7. awk的使用备忘

    [转]http://www.cnblogs.com/mydomain/archive/2012/09/24/2699467.html awk引用外部变量   一.用awk 有以下几种方法去调用变量: ...

  8. 常用javascript代码片段集锦

    常用方法的封装 根据类名获取DOM元素 var $$ = function (className, element) { if (document.getElementsByClassName) { ...

  9. 设置html滚动条(陶庭飞问题)

    var height = document.body.scrollHeight; parent.document.all("rightFrame").style.height = ...

  10. js二级下拉菜单

    看似简单的一个菜单,确需要不少的知识点 1. getByClass getElementsByClassName 已经有大部分现代浏览器支持了,只有ie6,ie7,ie8是不支持的.所以对ie6,7, ...