poj 3233 Matrix Power Series 矩阵快速幂
2014-07-29 19:07
429 查看
点击打开链接 题目链接
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
给出n*n矩阵 ,k,m
求A + A2 + A3 +
… + Ak.各项%m的值
若k为偶数 S(K) =(1 + A^k/2) * S(K/2)。
若K为奇数 S(K)
=A + (A + A^(k/2+1)) * S(k/2)。
贴代码
Matrix Power Series
Time Limit: 3000MS | Memory Limit: 131072K | |
Total Submissions: 14708 | Accepted: 6313 |
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
给出n*n矩阵 ,k,m
求A + A2 + A3 +
… + Ak.各项%m的值
若k为偶数 S(K) =(1 + A^k/2) * S(K/2)。
若K为奇数 S(K)
=A + (A + A^(k/2+1)) * S(k/2)。
贴代码
#include<stdio.h> #include<string.h> int n,m,k; struct juzheng { int v[30][30]; }; juzheng a; juzheng danwei; juzheng mot(juzheng a,juzheng b) { juzheng c; int i,j,k; for(i=0;i<n;i++) { for(j=0;j<n;j++) { c.v[i][j]=0; for(k=0;k<n;k++) { c.v[i][j]+=a.v[i][k]*b.v[k][j]%m; } c.v[i][j]%=m; } } return c; } juzheng add(juzheng a,juzheng b) { juzheng c; int i,j; for(i=0;i<n;i++) { for(j=0;j<n;j++) { c.v[i][j]=(a.v[i][j]+b.v[i][j])%m; } } return c; } juzheng pow(juzheng a,int t) { juzheng c; int i,j; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(i==j) c.v[i][j]=1; else c.v[i][j]=0; } } while(t) { if(t%2) c=mot(c,a); a=mot(a,a); t/=2; } return c; } juzheng sum(juzheng a,int t) { juzheng b,c; int i,j; if(t==1) { for(i=0;i<n;i++) { for(j=0;j<n;j++) { b.v[i][j]=a.v[i][j]; } } return b; } else if(t%2) { c=pow(a,t/2+1); c=add(c,a); b=sum(a,t/2); return add(mot(c,b),a); } else { c=pow(a,t/2); c=add(c,danwei); b=sum(a,t/2); return mot(c,b); } } int main() { int i,j; while(scanf("%d%d%d",&n,&k,&m)!=EOF) { juzheng b; for(i=0;i<n;i++) { for(j=0;j<n;j++) { scanf("%d",&a.v[i][j]); } } for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(i==j) danwei.v[i][j]=1; else danwei.v[i][j]=0; } } b=sum(a,k); for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(j<n-1) printf("%d ",b.v[i][j]); else printf("%d",b.v[i][j]); } printf("\n"); } } return 0; }
相关文章推荐
- POJ 3233 Matrix Power Series (矩阵快速幂+二分求解)
- POJ 3233 Matrix Power Series (矩阵&快速等比数列求和取模)
- POJ 3233 Matrix Power Series(矩阵快速幂)
- POJ 3233:Matrix Power Series 矩阵快速幂 乘积
- POJ 3233 Matrix Power Series 矩阵快速幂求A+A2+A3+…+Ak
- (Relax 矩阵快速幂 1.2)POJ 3233 Matrix Power Series(用矩阵加法+矩阵快速幂来求sum= A + A2 + A3 + … + Ak)
- poj 3233 Matrix Power Series 矩阵快速幂
- POJ 3233 Matrix Power Series 矩阵快速幂 + 二分法
- POJ 3233 && NYOJ 298 Matrix Power Series(矩阵快速幂)
- POJ 3233 Matrix Power Series(矩阵快速幂+二分)
- POJ 3233 Matrix Power Series(矩阵快速幂+二分求和)
- POJ 3233 Matrix Power Series 矩阵快速幂+二分
- poj 3233 Matrix Power Series---矩阵快速幂
- POJ 3233 Matrix Power Series(矩阵快速幂)
- POJ 3233 Matrix Power Series 矩阵快速幂+二分
- poj 3233 Matrix Power Series(矩阵快速幂)
- poj 3233 Matrix Power Series---矩阵快速幂
- POJ 3233 Matrix Power Series (矩阵快速幂和)
- POJ 3233 Matrix Power Series (矩阵快速幂)
- poj 3233 Matrix Power Series(矩阵快速幂)