#P3873. Weather Forecast Prediction

    ID: 17121 Type: Default 1000ms 256MiB

Weather Forecast Prediction

Weather Forecast Prediction

A company’s weather forecasting system encodes the weather of a day as an integer between 0 and 4146 (inclusive). The forecast for a future day is computed using the weather observations from the previous n days. Specifically, if wi denotes the weather condition on day i (with i > n), then the following recurrence is used:

\(w_i = (a_1 \times w_{i-1} + a_2 \times w_{i-2} + \cdots + a_n \times w_{i-n}) \mod 4147\)

Here, a1, a2, \dots, an are known constant coefficients. Given the weather conditions for the first n days, your task is to predict the weather condition on the mth day.

inputFormat

The input consists of three lines:

  • The first line contains two integers n and m (1 ≤ n ≤ m). Here, n is the number of initially given days and m is the day for which the weather prediction is required.
  • The second line contains n integers: w1, w2, ..., wn, each representing the weather condition of that day (0 ≤ wi ≤ 4146).
  • The third line contains n integers: a1, a2, ..., an, representing the coefficients used in the recurrence.

Note: For mn, simply output the mth weather condition.

The recurrence is defined as follows for i > n:

\(w_i = (a_1 \times w_{i-1} + a_2 \times w_{i-2} + \cdots + a_n \times w_{i-n}) \mod 4147\)

outputFormat

Output a single integer, which is the predicted weather condition on the mth day.

sample

3 5
1 2 3
1 1 1
11