A linear homogeneous recurrence relation of order
with constant coefficients has the seed values
with further terms defined according to
. For example, let
.
This means that the first two terms
and
have specified
values. Let
, then all terms
for
are
defined as
. When
and
,
the sequence begins
: the Fibonacci
sequence.
Given the values of , and
and a non-negative integer
, can you determine the term
in
this sequence, modulo
?
Input Specification
The first line of input contains two integers separated by a space,
and
.
Each of the following lines contains one integer
. They are given in the order
.
Each of the following lines contains one integer
. They are given in the order
.
Output Specification
Output a single line containing .
Note that although the mod operator in your language may give negative
results, the answer should be a non-negative integer.
Sample Input
2 10
2 1
1 1
Sample Output
123
Explanation
This sequence is defined according to , and
where
. This is the sequence of
so-called Lucas numbers, beginning
.
(This question was also used in the PEG short questions homework packages.)
Comments
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.