A-a Simple Math problem

Source: Internet
Author: User

Description

Lele now was thinking about a simple function f (x).

If x < f (x) = x.
If x >= 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'll give A0 ~ A9 and positive integers k and M, and could you help Lele to Caculate f (k)%m.

Input

The problem contains mutiple test cases. Please process to the end of file.
In each case, there'll be is the lines.
In the first line, there is, positive integers k and M. (K<2*10^9, M < 10^5)
In the second line, there is ten integers represent A0 ~ A9.

Output

For each case, output f (k)% m in one line.

Sample Input

10 9999 1 1 1 1 1 1 1 1 1 1 20 500 1 0 1 0 1 0 1 0 1 0

Sample Output

45 104

Describe

Lele is now considering a simple function f (x).

If x < ten f (x) = x.
if x > = ten f (x) = a0 * F (X-1) + A1 * F (2) + A2 * F (x 3) +......+ A9 * F (10 times times);
and artificial Intelligence (0 < = i < = 9) can only be 0 or 1.

Now, I'm going to give A0 ~ A9 and two positive integers k and M, and you can help lele all walks of Life f (k)%.

Input

This problem contains a variety of test cases. Please process the file.
In each case, there will be two lines.
In the first row, there are two positive integers k and M. (K < 2 * Ten ^ 9,m < 10 ^ 5)
On the second line, there are 10 integers representing A0 ~ A9.

Output

In each case, the output f (k)% m is on one line.

Sample input

9999 1 1 1 1 1 1 1 1 1 1 500 1 0 1 0 1 0 1 0 1 0

Sample output

45 104 years The problem is very simple, will not do is the SB;
#include<cstdio>#include<cstring>UsingNamespace Std;LongLong A[20],mNB[20];voidF(){LongLong S1[14][14],s2[14][14],s3[14][14],sum=0;LongLong IJK;For(I=0; I<=9; I++)For(j=0; j<=9; j++){If(j==9){S1[I][9]=a[9-I.]; S2[I][9]=a[9-I.];}ElseIf(I==j+1){S1[I][j]=1; S2[I][j]=1;}Else{S1[I][j]=0; S2[I][j]=0;}} m-=10;While(M){If(M&1){Memset(S3,0,sizeof(S3));For(I=0; I<=9; I++)For(j=0; j<=9; j++)For(k=0; k<=9; k+ +) S3[I][j]+= (S1[I][k]*s2[k][j])%n;For(I=0; I<=9; I++)For(j=0; j<=9; j+ +) s2[I][j]=s3[I][j];}Memset(S3,0,sizeof(S3));For(I=0; I<=9; I++)For(j=0; j<=9; j++)For(k=0; k<=9; k+ +) S3[I][j]+= (S1[I][k]*s1[k][j])%n;For(I=0; I<=9; I++)For(j=0; j<=9; j+ +) S1[I][j]=s3[I][j]; M>>=1;}For(I=0; I<=9; I+ +) sum= ((b[I]*s2[I][9])%n+sum)%n;Printf("%lld\ n", Sum);}IntMain(){LongLong IJ;While(~scanf("%lld%lld", &m, &n)){If(M==0&&n==0)Break;For(I=0; I<10; I++)scanf("%lld", &a[I]);For(I=0; I<10;i++) b< Span class= "Sh-symbol" >[i]=i%n if  (M<10printf (\n ",m%nelse f ();  return 0;< Span class= "Sh-cbracket" >              

A-a Simple Math problem

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.