标签:
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
思路:这是一道典型的矩阵快速幂的问题。用到两次二分,相当经典。矩阵快速幂。首先我们知道 A^x 可以用矩阵快速幂求出来。其次可以对k进行二分,每次将规模减半,分k为奇偶两种情况,如当k = 10和k = 5时有:
#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> #include <set> #include <map> #include <queue> using namespace std; const int inf=0x3f3f3f3f; int n,mod; struct node { int mp[50][50]; }init,res; struct node Mult(struct node x,struct node y) { struct node tmp; int i,j,k; for(i=0;i<n;i++) for(j=0;j <n;j++){ tmp.mp[i][j]=0; for(k=0;k<n;k++){ tmp.mp[i][j]=(tmp.mp[i][j]+x.mp[i][k]*y.mp[k][j])%mod; } } return tmp; }; struct node expo(struct node x,int k) { struct node tmp; int i,j; for(i=0;i<n;i++) for(j=0;j<n;j++){ if(i==j) tmp.mp[i][j]=1; else tmp.mp[i][j]=0; } while(k){ if(k&1) tmp=Mult(tmp,x); x=Mult(x,x); k>>=1; } return tmp; }; struct node add(struct node x,struct node y) { struct node tmp; int i,j; for(i=0;i<n;i++) for(j=0;j<n;j++){ tmp.mp[i][j]=(x.mp[i][j]+y.mp[i][j])%mod; } return tmp; }; struct node sum(struct node x,int k) { struct node tmp,y; if(k==1) return x; tmp=sum(x,k/2); if(k&1){ y=expo(x,k/2+1); tmp=add(Mult(y,tmp),tmp); return add(tmp,y); } else{ y=expo(x,k/2); return add(Mult(y,tmp),tmp); } }; int main() { int m,k,i,j,x; scanf("%d %d %d",&n,&k,&mod); for(i=0;i<n;i++) for(j=0;j<n;j++){ scanf("%d",&x); init.mp[i][j]=x%mod; } res=sum(init,k); for(i=0;i<n;i++){ for(j=0;j<n;j++){ if(j==n-1) printf("%d\n",res.mp[i][j]); else printf("%d ",res.mp[i][j]); } } return 0; }
POJ 3233-Matrix Power Series(矩阵快速幂+二分求矩阵和)
标签:
原文地址:http://blog.csdn.net/u013486414/article/details/44082055