Lele 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 could 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 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

代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>

using namespace std;

int n,m;
int f=10;

struct node
{
    int matrix[11][11];
};

node mul(node a,node b)
{
    node ans;
    memset(ans.matrix,0,sizeof(ans.matrix));
    for(int i=1;i<=f;i++)
    {
        for(int j=1;j<=f;j++)
        {
            for(int k=1;k<=f;k++)
            {
                ans.matrix[i][j]=(ans.matrix[i][j]+a.matrix[i][k]*b.matrix[k][j]%m+m)%m;
            }
        }
    }
    return ans;
}

node ksm(node a,int b)
{
    node res;
    memset(res.matrix,0,sizeof(res.matrix));
    for(int i=1;i<=f;i++)
        res.matrix[i][i]=1;
    while(b)
    {
        if(b&1) res=mul(res,a);
        b>>=1;
        a=mul(a,a);
    }
    return res;
}

int main()
{
    while(cin>>n>>m)
    {
        if(n<10)
        {
            cout<<n%m<<endl;
        }
        else
        {
            node a,b;
            memset(a.matrix,0,sizeof(a.matrix));
            memset(b.matrix,0,sizeof(b.matrix));
            for(int i=1;i<=10;i++)
                cin>>b.matrix[1][i];            
            for(int i=1;i<=10;i++)
                a.matrix[i][1]=10-i;
            for(int i=2;i<=10;i++)
                b.matrix[i][i-1]=1;
            node ans=ksm(b,n-9);
            ans=mul(ans,a);
            cout<<ans.matrix[1][1]%m<<endl;
        }
    }
    return 0;
}