Hiring process8 levels
  1. 1 IQ Test
  2. 2 Additional Online Test (If Required)
  3. 3 Office Interview with Hiring Manager
  4. 4 Unpaid Test Task (Motivation Check)
  5. 5 Paid Test Task and Follow-Up Interview
  6. 6 Iterative Paid Test Tasks
  7. 7 Office Verification
  8. 8 Final Interview with the Founder

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 Statistics

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.
Ctrl+Enter submit · Ctrl+Space autocomplete · F11 fullscreen