POJ3233Matrix Power Series 矩阵快速幂(分块矩阵构造)

来源:互联网 发布:彩票合买大厅源码 编辑:程序博客网 时间:2024/05/23 02:04

/****************88

这题一看就找到感觉,构造分块矩阵 

  1   A    *   Sk-1     =  Sk                        其中A和Sk都是n*n的矩阵, 1是n*n的单位矩阵,0是n*n的零矩阵

  0   A        Ak-1         Ak

ad****************/

Matrix Power Series
Time Limit: 3000MS Memory Limit: 131072KTotal Submissions: 13592 Accepted: 5875

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 40 11 1

Sample Output

1 22 3
# include<iostream># include<stdio.h># include<string.h>using namespace std;int mod;class Matrix{public:      int row;      int col;      int ** element;      Matrix();      ~Matrix();      Matrix(const int , const int);      Matrix(const Matrix &);      Matrix operator = (const Matrix &);      Matrix operator + (const Matrix &);      Matrix operator * (const Matrix &);      void setMatrix();      void print()      {          for(int i=0; i<row; i++)          {              for(int j=0; j<col; j++)                cout<<element[i][j]<<" ";              cout<<endl;          }      }      Matrix quickpow(int n)      {          Matrix result(row, col);          Matrix a(*this);          for(int i=0; i<row; i++)            result.element[i][i]=1;          while(n)          {              if(n&1)              result = a*result;              n=n>>1;              a=a*a;          }          return result;      }};Matrix::Matrix(){    row=col=0;    element=NULL;}Matrix::~Matrix(){    for(int i=0; i<row; i++)        delete []element[i];      delete []element;}Matrix::Matrix(const int m_row, const int m_col){    row = m_row;    col = m_col;    element = new int *[row];    for(int i=0; i<row; i++)        element[i] = new int[col];    for(int i=0; i<row; i++)        for(int j=0; j<col; j++)        element[i][j]=0;}Matrix::Matrix(const Matrix & temp){    row = temp.row;    col = temp.col;    element = new int *[row];    for(int i=0; i<row; i++)        element[i] = new int[col];    for(int i=0; i<row; i++)        for(int j=0; j<col; j++)        element[i][j]=temp.element[i][j];}Matrix Matrix::operator =(const Matrix & right){    for(int i=0; i<row; i++)      delete []element[i];    delete []element;    row = right.row;    col = right.col;    element = new int *[row];    for(int i=0; i<row; i++)        element[i] = new int[col];    for(int i=0; i<row; i++)        for(int j=0; j<col; j++)        element[i][j]=right.element[i][j];    return *this;}Matrix Matrix::operator + (const Matrix & right){    Matrix result(row, col);    for(int i=0; i<row; i++)        for(int j=0; j<col; j++)        result.element[i][j] = (element[i][j] + right.element[i][j])%mod;    return result;}Matrix Matrix::operator *(const Matrix & right){    Matrix result(row, right.col);    for(int i=0; i<row; i++)        for(int j=0; j<right.col; j++)        {            result.element[i][j]=0;            for(int k=0; k<col; k++)                {                    result.element[i][j]+=(element[i][k]*right.element[k][j])%mod;                    result.element[i][j]%=mod;                }        }    return result;}int main(){    int n, k;    while(~scanf("%d%d%d", &n, &k, &mod))    {        Matrix b(n, n);        Matrix a(2*n, 2*n);        for(int i=0; i<n; i++)            for(int j=0; j<n; j++)             scanf("%d", &b.element[i][j]);             for(int i=0; i<n; i++)                a.element[i][i]=1;        for(int i=0; i<n; i++)            for(int j=n; j<2*n; j++)              a.element[i][j] = b.element[i][j-n];        for(int i=n; i<2*n; i++)            for(int j=n; j<2*n; j++)              a.element[i][j] = b.element[i-n][j-n];        a=a.quickpow(k-1);        Matrix c(n, n);        for(int i=0; i<n; i++)           for(int j=0; j<n; j++)             c.element[i][j] = a.element[i][j];        Matrix d(n, n);        for(int i=0; i<n; i++)            for(int j=0; j<n; j++)               d.element[i][j]=a.element[i][j+n];        Matrix ans(n, n);        ans = c*b+d*b;        ans.print();    }    return 0;}


0 0