HDU ------ (1757) A simple math problem (simple matrix fast power)

Source: Internet
Author: User
A simple math problem

Time Limit: 3000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2791 accepted submission (s): 1659


Problem descriptionlele now is thinking about a simple function f (x ).

If x <10 f (x) = x.
If X> = 10 f (x) = A0 * F (x-1) + A1 * F (X-2) + A2 * F (X-3) + ...... + A9 * F (X-10 );
And AI (0 <= I <= 9) can only be 0 or 1.

Now, I will give a0 ~ A9 and two positive integers K and M, and cocould you help Lele to caculate F (k) % m.

 

Inputthe problem contains mutiple test cases. Please process to the end of file.
In each case, there will be two lines.
In the first line, there are two positive integers K and M. (K <2*10 ^ 9, m <10 ^ 5)
In the second line, there are ten integers represent a0 ~ A9.

 

Outputfor each case, Output F (k) % m in one line.

 

Sample input10 99991 1 1 1 1 1 1 1 120 5001 0 1 0 1 0 1 0 1 0 0

 

Sample output45104

 

Authorlinle

 

Source 2007 provincial training team exercise session (6) _ linle session code:
 1 //#define LOCAL 2 #include<cstdio> 3 #include<cstring> 4 #define LL __int64 5 using namespace std; 6 const int maxn=10; 7 LL k,m; 8 int aa[maxn],mat[maxn][maxn]; 9 int ans[maxn][maxn];10 11 void init()12 {13     for(int i=0;i<10;i++)14     {15      for(int j=0;j<10;j++)16      {17        if(i==0)18          mat[i][j]=aa[j];19        else20           if(i==j+1)mat[i][j]=1;21        else mat[i][j]=0;22        if(i==j)23            ans[i][j]=1;24        else ans[i][j]=0;25      }26     }27 }28 29 void Matrix(int a[][10],int b[][10])30 {31 32      int c[10][10];33      for(int i=0;i<10;i++)34      {35          for(int j=0;j<10;j++)36         {37             c[i][j]=0;38           for(int k=0;k<10;k++)39            {40             c[i][j]=(c[i][j]+a[i][k]*b[k][j])%m;41           }42         }43      }44     for(int i=0;i<10;i++)45     {46       for(int j=0;j<10;j++)47       {48         a[i][j]=c[i][j];49       }50     }51 }52 53 void pow(LL n)54 {55     while(n>0)56     {57         if(n&1) Matrix(ans,mat);58         n>>=1L;59         if(n==0)break;60         Matrix(mat,mat);61     }62 }63 64 int main()65 {66     #ifdef LOCAL67      freopen("test.in","r",stdin);68     #endif69 70   while(scanf("%I64d%I64d",&k,&m)!=EOF)71   {72        for(int i=0;i<10;i++)73        scanf("%d",aa+i);74      if(k<10) printf("%I64d\n",k%m);75      else76      {77        init();78        pow(k-9);79        int res=0;80        for(int i=0;i<10;i++)81         res=(res+(10-i-1)*ans[0][i])%m;82        printf("%d\n",res);83     }84   }85   return 0;86 }
View code

 

HDU ------ (1757) A simple math problem (simple 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.