Problems > The N-th Term of a Recurrence

The N-th Term of a Recurrence

Difficulty: 1900 4000 ms 128 MB

dp matrix-exponentiation

A sequence obeys f(i) = c1f(i−1) + c2f(i−2) + … + cKf(i−K) with given constant coefficients and the first K values. Print f(N) modulo 109+7.

Input: the first line has K and N; the second line has c1..cK; the third line has f(0)..f(K−1).

Output: f(N) mod 109+7.

Constraints:

  • 1 ≤ K ≤ 6
  • 0 ≤ N ≤ 1018
  • 0 ≤ ci, f(i) ≤ 109

Sample Tests

Input 1
2 10
1 1
0 1
Output 1
55
Input 2
1 5
2
3
Output 2
96

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.