Problem 560
Linear Recurrence by Matrix Power
Hard · 30 pts · Math, Matrix Exponentiation
Problem
A sequence follows the linear recurrence f(n) = c1 × f(n-1) + c2 × f(n-2) + ... + cK × f(n-K) for n >= K, and its first K values f(0), ..., f(K-1) are given. Compute f(N) modulo 1000000007 for N as large as 10^18. Put the last K values in a vector; one step of the recurrence is multiplication by a K × K companion matrix, so f(N) comes from raising that matrix to a large power with repeated squaring in O(K³ log N). The numbers in this problem can be larger than a 32-bit int can hold (about 2.1 × 10^9), so use a 64-bit integer type: long long in C++, long in Java (Python needs nothing special).
Input
The first line contains K and N. The second line contains c1 ... cK. The third line contains f(0) ... f(K-1).
Output
Print f(N) modulo 1000000007.
Constraints
1 <= K <= 10; 0 <= N <= 10^18; 0 <= ci, f(i) < 1000000007
Example input
2 10 1 1 0 1
Example output
55
Explanation
With K = 2, coefficients 1 1 and starting values 0 1, the recurrence is the Fibonacci sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55. So f(10) = 55.