D e s c r i p t i o n Description Description
给定数列 {hn}前k项,其后每一项满足
h n = a 1 ∗ h ( n − 1 ) + a 2 ∗ h ( n − 2 ) + . . . + a k ∗ h ( n − k ) hn = a1*h(n-1) + a2*h(n-2) + ... + ak*h(n-k) hn=a1∗h(n−1)+a2∗h(n−2)+...+ak∗h(n−k)
其中 a1,a2…ak 为给定数列。请计算 h(n),并将结果对 1000000007 取模输出。
I n p u t Input Input
第 1 行包含两个整数 n , k n,k n,k
第 2 行包含 k 个整数 a 1 , a 2... a k a1,a2...ak a1,a2...ak
第 3 行包含 k 个整数$h[0],h[1],…,h[k-1] $
n < = 1 0 9 ; k < = 2000 ; a b s ( h i ) < = 1 0 9 ; a b s ( a i ) < = 1 0 9 n <= 10^9;k <= 2000; abs(hi)<=10^9; abs(ai)<=10^9 n<=109;k<=2000;abs(hi)<=109;abs(ai)<=109
O u t p u t Output Output
一行一个整数 hn mod 1000000007
S a m p l e I n p u t Sample Input SampleInput
6 4
3 -1 0 4
-2 3 1 5
S a m p l e O u t p u t Sample Output SampleOutput
73
H I N T HINT HINT
S o u r c e Source Source
By submittersubmitter
常系数线性递推模板
#include
using namespace std;
#define rep(i,j,k) for(int i = j;i <= k;++i)
#define repp(i,j,k) for(int i = j;i >= k;--i)
#define rept(i,x) for(int i = linkk[x];i;i = e[i].n)
#define P pair
#define Pil pair
#define Pli pair
#define Pll pair
#define pb push_back
#define pc putchar
#define mp make_pair
#define file(k) memset(k,0,sizeof(k))
#define ll long long
int rd()
{
int sum = 0;char c = getchar();bool flag = true;
while(c < '0' || c > '9') {if(c == '-') flag = false;c = getchar();}
while(c >= '0' && c <= '9') sum = sum * 10 + c - 48,c = getchar();
if(flag) return sum;
else return -sum;
}
const int p = 1e9+7;
int n,k;
int a[4010],B[4010],g[4010];
int f[4010],c[4010];
inline int calc(int a,int b){return (a+b)%p;}
inline int mul(int a,int b){return 1ll*a*b%p;}
inline void mo(int &x){x = (x%p+p)%p;}
void change(int *a,int *b)
{
rep(i,0,2*k-2) c[i] = 0;
rep(i,0,k-1) rep(j,0,k-1) c[i+j] = calc(c[i+j],mul(a[i],b[j]));
repp(i,2*k-2,k)
if(c[i])
{
rep(j,i-k,i-1) c[j] = calc(c[j],mul(c[i],B[j-i+k+1]));
c[i] = 0;
}
rep(i,0,k-1) a[i] = c[i];
}
void work(int n)
{
for(int i = 1;i <= n;i <<= 1)
{
if(n&i) change(f,g);
change(g,g);
}
}
int main()
{
n = rd();k = rd();
repp(i,k,1) B[i] = rd();rep(i,0,k-1) a[i] = rd();
rep(i,1,k) mo(a[i]),mo(B[i]);
f[0] = 1;g[1] = 1;
work(n);
int ans = 0;
rep(i,0,k-1) ans = calc(ans,mul(f[i],a[i]));
printf("%d\n",ans);
return 0;
}