HDU 5015 233 matrix (matrix fast power)

Source: Internet
Author: User

233 Matrix Time Limit: 10000/5000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 749 accepted submission (s): 453


Problem descriptionin our daily life we often use 233 to express our feelings. actually, we may say 2333,233 33, or 233333... in the same meaning. and here is the question: Suppose we have a matrix called 233 matrix. in the first line, it wocould be 233,233 3, 23333... (It means A0, 1 = 233, A0, 2 = 2333, A0, 3 = 23333 ...) besides, in 233 matrix, we got AI, j = ai-1, J + AI, J-1 (I, j = 0 ). now you have known A1, 0, A2, 0,..., An, 0, cocould you tell me an, m in the 233 matrix?
Inputthere are multiple test cases. Please process till EOF.

For each case, the first line contains two postive integers n, m (n ≤ 10, M ≤109 ). the second line contains N integers, A1, 0, A2, 0 ,..., an, 0 (0 ≤ AI, 0 <231 ).
Outputfor each case, output an, M mod 10000007.
Sample Input
1 112 20 03 723 47 16
 
Sample output
234279972937Hint
 

Ideas:

The first column has the following elements:

0

A1

A2

A3

A4

Convert:

23

A1

A2

A3

A4

3

The second column is:

23*10 + 3

23*10 + 3 + A1

23*10 + 3 + A1 + A2

23*10 + 3 + A1 + A2 + A3

23*10 + 3 + A1 + A2 + A3 + A4

3

Based on the recursive relationship between the first and second columns, the element of matrix A obtained by equality is:

#include"iostream"#include"stdio.h"#include"string.h"#include"algorithm"#include"queue"#include"vector"using namespace std;#define N 15#define LL __int64const int mod=10000007;int n;int b[N];struct Mat{    LL mat[N][N];}a,ans;Mat operator*(Mat a,Mat b){    int i,j,k;    Mat c;    memset(c.mat,0,sizeof(c.mat));    for(i=0; i<=n+1; i++)    {        for(j=0; j<=n+1; j++)        {            c.mat[i][j]=0;            for(k=0; k<=n+1; k++)            {                if(a.mat[i][k]&&b.mat[k][j])                {                    c.mat[i][j]+=a.mat[i][k]*b.mat[k][j];                    c.mat[i][j]%=mod;                }            }        }    }    return c;}void mult(int k){    int i;    memset(ans.mat,0,sizeof(ans.mat));    for(i=0;i<=n+1;i++)        ans.mat[i][i]=1;    while(k)    {        if(k&1)            ans=ans*a;        k>>=1;        a=a*a;    }}void inti(){    int i,j;    b[0]=23;    b[n+1]=3;    for(i=1; i<=n; i++)        scanf("%d",&b[i]);    memset(a.mat,0,sizeof(a.mat));    for(i=0; i<=n; i++)    {        a.mat[i][0]=10;        a.mat[i][n+1]=1;    }    a.mat[n+1][n+1]=1;    for(i=1; i<n+1; i++)    {        for(j=1; j<=i; j++)        {            a.mat[i][j]=1;        }    }}int main(){    int i,m;    while(scanf("%d%d",&n,&m)!=-1)    {        inti();        mult(m);        LL s=0;        for(i=0;i<=n+1;i++)            s=(s+(ans.mat[n][i]*b[i])%mod)%mod;        printf("%I64d\n",s);    }    return 0;}




HDU 5015 233 matrix (matrix fast power)

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.