Problem Description
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.

Input
The 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.

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

 

输入样例

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

输出样例

45
104

本题只需要计算系数矩阵,最后一行乘积和即可算出f(k)
附ac代码
#include<bits/stdc++.h>
using namespace std;
const int N=10;
struct matrix{
    long long ma[N][N];
};
int k,m;
matrix mutil(matrix a,matrix b)
{
    matrix c;
    for(int i=0;i<10;++i)
      for(int j=0;j<10;++j)
      c.ma[i][j]=0;
    for(int i=0;i<10;++i)
      for(int j=0;j<10;++j)
       for(int z=0;z<10;++z)
       {c.ma[i][j]+=((a.ma[i][z]%m)*(b.ma[z][j]%m))%m;
       c.ma[i][j]%=m;
       }
    return c;
}
matrix pow_ma(matrix a,int n)
{
    if(n==1) return a;
    matrix s;
    s=pow_ma(mutil(a,a),n/2);
    if(n%2) s=mutil(s,a);
    return s;
}
int a[N];
int main()
{
    while(scanf("%d%d",&k,&m)==2)
    {
        for(int i=0;i<=9;++i)
        scanf("%d",&a[i]);
        if(k<10)
        {
         printf("%d\n",k%m);continue;
        }
        matrix ans;
        for(int i=0;i<N;++i)
          for(int j=0;j<N;++j)
          ans.ma[i][j]=0;
        //ans记得需要初始化-----
        for(int i=0;i<=9;++i)
            ans.ma[0][i]=a[i];
        for(int i=1;i<=9;++i)
            ans.ma[i][i-1]=1;
        ans=pow_ma(ans,k-9);
        long long sum=0;
        for(int i=0;i<10;++i)
        sum+=(ans.ma[0][i]%m*((9-i)%m))%m;
        printf("%lld\n",sum%m);
    }
    return 0;
}

 

 posted on 2023-01-14 11:11  ruoye123456  阅读(33)  评论(0)    收藏  举报