Hdu-5015-233 matrix-Matrix

Source: Internet
Author: User
Tags mul

Obviously, the first column of the Matrix is:

0

A [1]

A [2]

A [3]

A [4]

Let's convert it

23

A [1]

A [2]

A [3]

A [4]

3

Then, when the first column is transferred to the second column

23*10 + 3

A [1] + 23*10 + 3

A [2] + A [1] + 23*10 + 3

A [3] + A [2] + A [1] + 23*10 + 3

A [4] + A [3] + A [2] + A [1] + 23*10 + 3

3

Apparently, transfer matrix A is coming out:

10 0 0 0 0 1

10 1 0 0 0 1

10 1 1 0 0 1

10 1 1 1 0 1

10 1 1 1 1 1

0 0 0 0 1

Then the last column is the M Power * first column of.

#include<stdio.h>#include<iostream>#include<stdlib.h>#include<string.h>#include<algorithm>#include<vector>#include<math.h>#include<queue>#include<stack>#include<map>#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;#define maxn 110000#define mod 10000007#define LL __int64struct matrix{    LL mat[15][15];    matrix()    {        memset(mat,0,sizeof(mat));    }};int a[11];int n;matrix mul(matrix A,matrix B){    matrix C;    int i,j,k;    for(i=1; i<=n+2; i++)    {        for(j=1; j<=n+2; j++)        {            for(k=1; k<=n+2; k++)            {                C.mat[i][j]=(C.mat[i][j]+A.mat[i][k]*B.mat[k][j])%mod;            }        }    }    return C;}matrix powmul(matrix A,int k){    matrix B;    for(int i=1;i<=n+2;i++)B.mat[i][i]=1;    while(k>=1)    {        if(k&1)B=mul(B,A);        A=mul(A,A);        k=k/2;    }    return B;}void print(matrix A){    cout<<"matrix A"<<endl;    for(int i=1;i<=n+2;i++)    {        for(int j=1;j<=n+2;j++)        {            cout<<A.mat[i][j]<<" ";        }        cout<<endl;    }}int main(){    int m;    while(~scanf("%d%d",&n,&m))    {        matrix A,B;        A.mat[1][1]=23;        for(int i=1;i<=n;i++)        {            scanf("%d",&A.mat[i+1][1]);        }        A.mat[n+2][1]=3;        for(int i=1;i<=n+1;i++)B.mat[i][1]=10;        for(int i=1;i<=n+2;i++)B.mat[i][n+2]=1;        for(int i=2;i<=n+1;i++)        {            for(int j=2;j<=i;j++)B.mat[i][j]=1;        }       // print(A);       // print(B);        B=powmul(B,m);        A=mul(B,A);      //  print(A);        cout<<A.mat[n+1][1]<<endl;    }    return 0;}



Hdu-5015-233 matrix-Matrix

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.