Recurrence Solver

View as PDF

Submit solution

Points: 15 (partial)
Time limit: 1.0s
Memory limit: 16M

Problem type

A linear homogeneous recurrence relation of order d with constant coefficients has the seed values t0,t1,,td1 with further terms defined according to tn=c1tn1+c2tn2++cdtnd. For example, let d=2. This means that the first two terms t0 and t1 have specified values. Let c1=c2=1, then all terms tn for n>1 are defined as tn=tn1+tn2. When t0=0 and t1=1, the sequence begins 0,1,1,2,3,5,8, : the Fibonacci sequence.

Given the values of d,t0,,td1, and c1,,cd and a non-negative integer n, can you determine the term tn in this sequence, modulo 10007?

Input Specification

The first line of input contains two integers separated by a space, d (0<d50) and n (0n<109).
Each of the following d lines contains one integer ti (10000<ti<10000). They are given in the order t0,t1,,td1.
Each of the following d lines contains one integer ci (10000<ci<10000). They are given in the order c1,c2,,cd.

Output Specification

Output a single line containing tnmod10007.
Note that although the mod operator in your language may give negative results, the answer should be a non-negative integer.

Sample Input

Copy
2 10
2 1
1 1

Sample Output

Copy
123

Explanation

This sequence is defined according to t0=2,t1=1, and tn=tn1+tn2 where n>1. This is the sequence of so-called Lucas numbers, beginning 2,1,3,4,7,11,18,29,47,76,123. (This question was also used in the PEG short questions homework packages.)


Comments


  • 1
    cabbagecabbagecabbage  commented on Dec. 29, 2020, 8:17 p.m.

    the integers after the first line appear as 1 on each line for 2*d lines as the input specification states, not space separated as the sample input shows. irrelevant to c++ input but slightly misleading for me when I tried to do this with python.