Poj3233 matrix power series matrix multiplication

Source: Internet
Author: User
 

This question is the third application of matrix multiplication. However, pleaseS=A+A2 +A3 +... +
AK. The result matrix M67 Daniel's blog mentioned above is a sub-governance method. Set f [N] = a ^ 1 + a ^ 2 +... a ^ N;
If n is an even number, F [N] = f [n/2] + F [n/2] * a ^ (n/2 );
However, n is an odd number, and f [N] = f [n-1] + A ^ (N). After reading discuss, we found a method with lower programming complexity. Again ..

Let B = [[a I];
[0 I]

B ^ (k + 1) = [[A ^ k I + A +... + A ^ K];
[0 I]

It's amazing to say that the matrix .......

I am relatively lazy. So... The second method is implemented ....

 

Matrix Power Series
Time limit:3000 Ms   Memory limit:131072 K
Total submissions:11648   Accepted:4977

Description

GivenN×NMatrixAAnd a positive integerK, Find the sumS=
A+A2 +A3 +... +
AK.

Input

The input contains exactly one test case. The first line of input contains three positive integersN(N≤ 30 ),
K(K≤ 109) andM(M<104). Then followNLines each containing
NNonnegative integers below 32,768, givingA'S elements in row-Major Order.

Output

Output the elementsSModuloMIn the same wayAIs given.

Sample Input

2 2 40 11 1

Sample output

1 22 3
#include<iostream>#include<cstdio>#define MAXN 100using namespace std;struct Matrix{    int size;    long modulo;    long element[MAXN][MAXN];    void setSize(int);    void setModulo(int);    Matrix operator* (Matrix);    Matrix power(int);};void Matrix::setSize(int a){    for (int i=0; i<a; i++)        for (int j=0; j<a; j++)            element[i][j]=0;    size = a;}void Matrix::setModulo(int a){    modulo = a;}Matrix Matrix::operator* (Matrix param){    Matrix product;    product.setSize(size);    product.setModulo(modulo);    for (int i=0; i<size; i++)        for (int j=0; j<size; j++)            for (int k=0; k<size; k++)            {                product.element[i][j]+=element[i][k]*param.element[k][j];                product.element[i][j]%=modulo;            }    return product;}Matrix Matrix::power(int exp){    Matrix tmp = (*this) * (*this);    if (exp==1) return *this;    else if (exp & 1) return tmp.power(exp/2) * (*this);    else return tmp.power(exp/2);}int n,m,k;Matrix m1,ans;int main(){    while(~scanf("%d%d%d",&n,&k,&m))    {        m1.setModulo(m);        m1.setSize(n*2);        for(int i=0;i<n;i++)        {            for(int j=0;j<n;j++)            {                scanf("%d",&m1.element[i][j]);                m1.element[i][j]%=m;            }            m1.element[i][i+n]=1;            m1.element[n+i][n+i]=1;        }        m1.element[n][n]=1;        ans=m1.power(k+1);        for(int i=0;i<n;i++)            for(int j=n;j<n*2;j++)            {                int out=ans.element[i][j];                if(j==i+n)                    out=out==0?m-1:out-1;                printf("%d%s",out,j!=n*2-1?" ":"\n");            }        return 0;    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.