Firepersons
Time limit1sMemory limit128 MB
Given the first k terms and multipliers of a linear recurrence modulo 10000, return the i-th term for i up to 10^9.
- Level
Medium7 of 10
- Topics
- Math, Matrix, Divide and conquer, Implementation
- Solved
- No attempts yet
Problem
Each fireperson is given an integer rank, and these ranks come from an integer sequence defined as follows.
A sequence of order is defined by its first terms and by integer constants . For , the terms follow the recurrence
The -th oldest fireperson receives rank . Given the parameters of the sequence and an integer , compute the rank of the -th fireperson, that is, the -th term of the sequence.
Input
The input consists of several test cases. Each test case is given on a single line containing the following integers separated by single spaces, in order:
- : the order of the sequence
- : the first terms of the sequence
- : the multipliers of the recurrence
- : the index of the requested term
The input ends with a line containing a single .
Output
For each test case, print on its own line the -th term of the corresponding sequence. The output lines must appear in the same order as the test cases in the input.