要求的东西是 An−k+1h A n − k + 1 h 。
记得以前数学竞赛的同桌大佬告诉我特征方程~
#include
#define show(x) cerr << #x << " = " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> Pairs;
const int N = 4040;
const int MOD = 1000000007;
inline char get(void) {
static char buf[100000], *S = buf, *T = buf;
if (S == T) {
T = (S = buf) + fread(buf, 1, 100000, stdin);
if (S == T) return EOF;
}
return *S++;
}
template<typename T>
inline void read(T &x) {
static char c; x = 0; int sgn = 0;
for (c = get(); c < '0' || c > '9'; c = get()) if (c == '-') sgn = 1;
for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
if (sgn) x = -x;
}
int n, k;
int a[N], h[N], b[N], c[N];
int mod[N];
inline int add(int &x, int a) {
x += a; while (x >= MOD) x -= MOD;
}
inline void mul(int *a, int *b, int *c) {
static int res[N];
for (int i = 0; i <= k * 2; i++) res[i] = 0;
for (int i = 0; i <= k; i++)
for (int j = 0; j <= k; j++)
add(res[i + j], (ll)a[i] * b[j] % MOD);
for (int i = k * 2; i >= k; i--)
for (int j = k - 1; ~j; j--)
add(res[i - k + j], MOD - (ll)res[i] * mod[j] % MOD);
for (int i = 0; i < k; i++) c[i] = res[i];
}
int main(void) {
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
read(n); read(k);
for (int i = 1; i <= k; i++) {
read(a[i]); add(a[i], MOD);
}
for (int i = 1; i <= k; i++) {
read(h[i]); add(h[i], MOD);
}
for (int i = 1; i <= k; i++) mod[k - i] = MOD - a[i];
mod[k] = 1; c[0] = 1; b[1] = 1;
for (int i = n - k + 1; i; i >>= 1) {
if (i & 1) mul(b, c, c);
mul(b, b, b);
}
for (int i = k + 1; i <= k * 2; i++)
for (int j = 1; j <= k; j++)
add(h[i], (ll)h[i - j] * a[j] % MOD);
int ans = 0;
for (int i = 0; i < k; i++)
add(ans, (ll)c[i] * h[i + k] % MOD);
cout << ans << endl;
return 0;
}