#include<bits/stdc++.h>
#define debu
using namespace std;
typedef long long LL;
const int maxn=25;
struct Matrix
{
LL mat[maxn][maxn];
};
int d;
LL m;
LL ans,n;
Matrix a,c;
LL f[maxn];
Matrix operator * (Matrix a,Matrix b)
{
Matrix c;
memset(c.mat,0,sizeof(c.mat));
for(int i=1; i<=d; i++)
for(int j=1; j<=d; j++)
{
for(int k=1; k<=d; k++)
{
c.mat[i][j]+=(a.mat[i][k]*b.mat[k][j])%m;
c.mat[i][j]%=m;
}
}
return c;
}
void pow(LL n)
{
while(n)
{
if(n&1) c=c*a;
a=a*a;
n>>=1;
}
}
void prepare()
{
memset(a.mat,0,sizeof(a.mat));
memset(c.mat,0,sizeof(c.mat));
for(int i=d; i>=1; i--)
scanf("%lld",&a.mat[d][i]);
for(int i=1; i<=d; i++)
scanf("%lld",&f[i]);
for(int i=1; i<d; i++)
a.mat[i][i+1]=1;
for(int i=1; i<=d; i++)
c.mat[i][i]=1;
}
void solve()
{
ans=0;
if(d<n)
{
pow(n-d);
for(int i=1; i<=d; i++)
{
ans+=f[i]*c.mat[d][i];
ans%=m;
}
}
else ans=f[n]%m;
}
int main()
{
#ifdef debug
freopen("in.in","r",stdin);
#endif // debgu
while(scanf("%d%lld%d",&d,&n,&m)!=EOF)
{
if(!(d||n||m)) break;
prepare();
solve();
printf("%lld\n",ans);
}
return 0;
}
题目地址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1811
题解:F(n)=A*F(n-1)
| f(n-d) | | 1 |
F(n)=| 。 。 | A=| 1 |
| f(n-1) | | |
| f(n) | | 1 |
| a(d) a(d-1) a(1) |
F(n)=A^(n-d)*F(d)