poj3233(快速求幂+二分)
2013-08-06 17:27
399 查看
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
Time Limit: 3000MS | Memory Limit: 131072K | |
Total Submissions: 12322 | Accepted: 5255 |
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为奇数时:S[7]=A^1+A^2+A^3……+A^7=A^4+(1+A^4)(A^1+A^2+A^3)=A^4+S[3]+A^4 *S[3]; 当k为偶数时:S[6]=S[3]+S[3] *A^3;#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const int maxn=50; typedef struct { int m[maxn][maxn]; }Matrax; Matrax a,per; int n,m; void inital() { int i,j; for(i=0;i<n;i++) { for(j=0;j<n;j++) { scanf("%d",&a.m[i][j]); a.m[i][j]%=m; per.m[i][j]=(i==j); } } } Matrax add(Matrax a,Matrax b) { Matrax c; int i,j; for(i=0;i<n;i++) { for(j=0;j<n;j++) { c.m[i][j]=(a.m[i][j]+b.m[i][j])%m; } } return c; } Matrax multi(Matrax a,Matrax b) { Matrax c; int k,i,j; for(i=0;i<n;i++) { for(j=0;j<n;j++) { c.m[i][j]=0; for(k=0;k<n;k++) c.m[i][j]+=((a.m[i][k])%m*(b.m[k][j])%m)%m; } c.m[i][j]%=m; } return c; } Matrax power(int k) { Matrax p,ans=per; p=a; while(k) { if(k&1) { ans=multi(ans,p); k--; } else { k/=2; p=multi(p,p); } } return ans; } Matrax MatraxSum(int k) { if(k==1) return a; Matrax temp,b; temp=MatraxSum(k/2); if(k&1) { b=power(k/2+1); temp=add(temp,multi(temp,b)); temp=add(temp,b); } else { b=power(k/2); temp=add(temp,multi(temp,b)); } return temp; } int main() { int k,i,j; Matrax ans; while(scanf("%d%d%d",&n,&k,&m)==3) { inital(); ans=MatraxSum(k); for(i=0;i<n;i++) { for(j=0;j<n-1;j++) printf("%d ",ans.m[i][j]); printf("%d\n",ans.m[i][j]); } } return 0; }
相关文章推荐
- 九度OJ 1085 求root(N, k) -- 二分求幂及快速幂取模
- HDOJ 2035 人见人爱A^B-----二分快速求幂
- poj3233 Matrix Power Series (矩阵快速幂+二分)
- POJ3233---Matrix Power Series(矩阵快速幂+二分)
- 九度OJ 二分求幂法、快速取模之《root(n,k)》
- 算法 二分求幂(快速取幂)
- hdu 1005 Number Sequence(矩阵连乘+二分快速求幂)
- 判断素数+二分快速求幂——pku3641
- HDU 2035 人见人爱A^B(二分求幂,快速求幂)
- poj3233经典二分矩阵快速幂
- HDU 2035 人见人爱A^B(二分求幂,快速求幂)
- 二分求幂,快速求幂
- POJ3233 - Matrix Power Series - 二分矩阵快速幂
- POJ3233:Matrix Power Series(矩阵快速幂+二分)
- 快速二分求幂 模板
- 二分求幂,快速求解a的b次幂
- 九度OJ 1085 求root(N, k) -- 二分求幂及快速幂取模
- CSU1552: Friends(快速判断大数是不是素数+二分匹配)
- 快速求幂算法
- 【二分】求幂的和