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

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

当k为奇数时

Sk = A + A2 + A3 + … + Ak  

    =(1+Ak/2)*(A + A2 + A3 + … + Ak/2  )+{Ak}

    =(1+Ak/2)*(Sk/2 )+{Ak}

当k为偶数时

Sk = A + A2 + A3 + … + Ak  

    =(1+Ak/2)*(A + A2 + A3 + … + Ak/2  )+{Ak}

    =(1+Ak/2)*(Sk/2 )

就能够二分递归求Sk

代码:

//829ms
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int n,m;
struct matrix
{
int ma[50][50];
} a;
matrix multi(matrix x,matrix y)//矩阵相乘
{
matrix ans;
memset(ans.ma,0,sizeof(ans.ma));
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
if(x.ma[i][j])//稀疏矩阵优化
for(int k=1; k<=n; k++)
{
ans.ma[i][k]=(ans.ma[i][k]+x.ma[i][j]*y.ma[j][k])%m;
}
}
}
return ans;
}
matrix add(matrix x,matrix y)//矩阵相加
{
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
x.ma[i][j]=(x.ma[i][j]+y.ma[i][j])%m;
return x;
}
matrix pow(matrix a,int m)
{
matrix ans;
for(int i=1; i<=n; i++) //单位矩阵
{
for(int j=1; j<=n; j++)
{
if(i==j)
ans.ma[i][j]=1;
else
ans.ma[i][j]=0;
}
}
while(m)//矩阵高速幂
{
if(m&1)
{
ans=multi(ans,a);
}
a=multi(a,a);
m=(m>>1);
}
return ans;
}
matrix solve(matrix x,int k)//递归求Sk
{
if(k==1)
return x;
matrix ans;
for(int i=1; i<=n; i++) //单位矩阵
{
for(int j=1; j<=n; j++)
{
if(i==j)
ans.ma[i][j]=1;
else
ans.ma[i][j]=0;
}
}
ans=add(ans,pow(x,k/2));
ans=multi(ans,solve(x,k/2));
if(k&1)
ans=add(ans,pow(x,k));
return ans;
}
int main()
{
int k;
while(~scanf("%d%d%d",&n,&k,&m))
{
matrix ans,a;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
scanf("%d",&a.ma[i][j]);
}
ans=solve(a,k);
for(int i=1; i<=n; i++)
{
for(int j=1; j<n; j++)
printf("%d ",ans.ma[i][j]);
printf("%d\n",ans.ma[i][n]);
}
}
return 0;
}

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(矩阵高速功率+二分法)

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

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

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

  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(矩阵等比求和)

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

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

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

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

  9. [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:  ...

随机推荐

  1. hdu5852 Intersection is not allowed! 【矩阵行列式】

    题意 给出\(n*n\)网格\((n<=10^5)\) 顶部有\(K\)个起点,底部有\(K\)个相对应的终点 每次只能向下或向右走 求有多少种从各个起点出发到达对应终点且路径不相交的路径? 对 ...

  2. springboot获取getBean方法以及ApplicationContext空指针问题解决

    创建获取ApplicationContext工具类: package com.performancetest.common.utils; import org.springframework.bean ...

  3. python:print含有中文的list

    Python 的 List 如果有中文的话, 会印出 \xe4\xb8... 等等的编码(如下所示), 要如何印出中文呢? >>> a = ['中文', 'ab']>>& ...

  4. bzoj 1038 瞭望塔 半平面交+分段函数

    题目大意 给你一座山,山的形状在二维平面上为折线 给出\((x_1,y_1),(x_2,y_2)...(x_n,y_n)\)表示山的边界点或转折点 现在要在\([x_1,x_n]\)(闭区间)中选择一 ...

  5. 优化join语句

    Mysql4.1开始支持SQL的子查询.这个技术可以使用SELECT语句来创建一个单列的查询结果,然后把这个结果作为过滤条件用在另一个查询中.使用子查询可以一次性的完成很多逻辑上需要多个步骤才能完成的 ...

  6. *NOI热身赛C. 小x的城池

    码农题gun.

  7. 获取本机外网IP的工具类

    ExternalIpAddressFetcher.java package com.tyust.common; import java.io.IOException; import java.io.I ...

  8. 转 Python爬虫实战一之爬取糗事百科段子

    静觅 » Python爬虫实战一之爬取糗事百科段子 首先,糗事百科大家都听说过吧?糗友们发的搞笑的段子一抓一大把,这次我们尝试一下用爬虫把他们抓取下来. 友情提示 糗事百科在前一段时间进行了改版,导致 ...

  9. CSS-文本(中,英)

    1.缩进文本:text-indent 2.水平对齐:text-align:  left/center/right/justify(实现两端对齐文本效果) 3.字间隔:word-spacing(可以改变 ...

  10. Ext grid单元格编辑时获取获取Ext.grid.column.Column

    item2.width = 80; //item2.flex = 1; item2.align = 'center'; item2.menuDisabled = true; //禁止显示列头部右侧菜单 ...