#C2622. Fibonacci-like Sequence Generator

    ID: 45959 Type: Default 1000ms 256MiB

Fibonacci-like Sequence Generator

Fibonacci-like Sequence Generator

You are given two starting integers \(a\) and \(b\) and an integer \(n\) representing the position in the sequence. You are also given a list of coefficients \(c_0, c_1, \ldots, c_{k-1}\). For \(i \ge 3\), the \(i\)th term of the sequence is defined by the recurrence:

[ a_i = (a_{i-1} + a_{i-2}) \times c_{(i-3) \bmod k} ]

Your task is to compute the \(n\)th term of this Fibonacci-like sequence.

The input consists of multiple datasets. Each dataset is given in two lines:

  • The first line contains three integers: \(a\), \(b\), and \(n\).
  • The second line contains a list of coefficients (at least one integer).

The datasets end with a line where \(a = 0\), \(b = 0\), and \(n = 0\) (this dataset should not be processed).

inputFormat

Input is read from standard input and consists of multiple datasets. Each dataset contains two lines:

  • The first line contains three space-separated integers: \(a\), \(b\), and \(n\) where \(a\) and \(b\) are the first two numbers and \(n\) is the target term position.
  • The second line contains a list of space-separated integers representing the coefficients \(c_0, c_1, \ldots, c_{k-1}\).

The input terminates with a line "0 0 0".

outputFormat

For each dataset, output the \(n\)th term of the sequence on a separate line. The output should be printed to standard output.

## sample
1 1 5
2 3 4
2 3 6
1 2 3 4
0 0 0
76

316

</p>