Matrix Power Series poj3233矩阵快速幂
2016-08-15 12:41
260 查看
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
Sample Output
令S[k]=I+A+……+A^(k-1)(其中I是单位矩阵)
时间复杂度为O(n^3logk)
AC代码:
Time Limit: 3000MS | Memory Limit: 131072K | |
Total Submissions: 20527 | Accepted: 8612 |
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
令S[k]=I+A+……+A^(k-1)(其中I是单位矩阵)
时间复杂度为O(n^3logk)
AC代码:
#include <iostream> #include<cstdio> #include<cstring> using namespace std; #define N 65 typedef long long ll; int Mod,n,k; struct Matrix { int row,col; ll mat ; void init(int row,int col,bool ok=false){ this->row=row; this->col=col; memset(mat,0,sizeof(mat)); if(!ok) return; for(int i=0;i<N;i++){ mat[i][i]=1; } } Matrix operator *(const Matrix& B){ Matrix C; C.init(row,B.col); for(int k=0;k<col;k++){ for(int i=0;i<row;i++){ if(mat[i][k]==0) continue; for(int j=0;j<B.col;j++){ if(B.mat[k][j]==0) continue; C.mat[i][j]+=(mat[i][k]%Mod)*(B.mat[k][j]%Mod); C.mat[i][j]=C.mat[i][j]%Mod; } } } return C; } Matrix pow(ll n){ Matrix B,A=*this; B.init(row,col,1); while(n){ if(n&1) B=B*A; A=A*A; n>>=1; } return B; } }; int main() { scanf("%d%d%d",&n,&k,&Mod); Matrix A,B; A.init(n,n); for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ scanf("%d",&A.mat[i][j]); } } B.init(2*n,2*n); for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ B.mat[i][j]=A.mat[i][j]; } B.mat[n+i][i]=B.mat[n+i][n+i]=1; } B=B.pow(k+1); for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ ll a=B.mat[n+i][j]%Mod; if(i==j) a=(a+Mod-1)%Mod; printf("%lld%c",a,j+1==n?'\n':' '); } } return 0; }
相关文章推荐
- Ubuntu8.04快速设置指南
- 快速对SQL Server锁机制进行掌握的窍门
- Struts快速入门
- 使用ASP.NET 构建 Web 应用程序快速入门-8小时的免费培训视频 - Scott Hanselman的中文博客【转载】
- 小米通过亚马逊AWS快速响应海外市场
- zoj 2974 Just Pour the Water矩阵快速幂
- jxls框架解析(一)报表导出快速入门
- Robot Framework 快速入门
- jquery Easyui快速开发总结
- Eclipse下 快速找到类对应的包
- 利用ASP.NET MVC和Bootstrap快速搭建个人博客之后台dataTable数据列表
- iOS开发--图形化排序算法比较:快速排序、插入排序、选择排序、冒泡排序
- T解 POJ-3233 [矩阵快速幂][矩阵乘法][二分求解]
- HDU 4990 Reading comprehension(找规律+矩阵快速幂)
- Java 9 新特性快速预览
- python(you-get):2分钟快速下载网站视频
- 使用二分法实现对Sbo DBDatasource数据的快速定位查询
- 快速应用开发平台-对象建模
- 自适应全变分图像去噪模型及其快速求解(Matlab CODE)
- Sharepoint快速启动与导航