This page is still under construction.

Parts of this page are still being built. What you see may change.

Sequence Prediction

Time limit1sMemory limit128 MB

Summary
Given observed terms and a modulus, predict the next term under the lowest-degree eventually constant, periodic, or polynomial law.
Level

Medium6 of 10

Topics
Simulation, Math, Brute force
Solved
No attempts yet

Problem

Predict the next term of a sequence by finding the simplest law that fits it. Three laws are considered here. Every element of a sequence is an integer between 00 and m−1m-1, where mm is given in the input and never exceeds 1000010000.

Eventually constant sequences. A sequence a0 a1 … an …a_0\ a_1\ \dots\ a_n\ \dots is eventually constant if there is a number dd such that an=ada_n = a_d for every n≥dn \ge d. The least such dd is the degree of the sequence. For example, 0 8 6 6 6 …0\ 8\ 6\ 6\ 6\ \dots is an eventually constant sequence of degree 22.

Periodic sequences. A periodic sequence is given by a seed a0 a1 … ada_0\ a_1\ \dots\ a_d such that an=an−d−1a_n = a_{n-d-1} for every n>dn > d. The degree dd of a periodic sequence is the least dd for which a0 a1 … ada_0\ a_1\ \dots\ a_d is a seed of the sequence. For example, 1 2 3 4 1 2 3 4 1 2 …1\ 2\ 3\ 4\ 1\ 2\ 3\ 4\ 1\ 2\ \dots is a periodic sequence of degree 33 with seed 1 2 3 41\ 2\ 3\ 4.

Polynomial sequences. A polynomial sequence modulo mm of degree dd is a sequence a0 a1 … an …a_0\ a_1\ \dots\ a_n\ \dots generated by an integer-valued polynomial of degree dd. For example, 0 0 1 3 6 10 15 1 …0\ 0\ 1\ 3\ 6\ 10\ 15\ 1\ \dots is a polynomial sequence modulo 2020 of degree 22, generated by an=(n2/2−n/2) mod 20a_n = (n^2/2 - n/2) \bmod 20. A second definition works too. A sequence a0 a1 …a_0\ a_1\ \dots is a polynomial sequence modulo mm of degree dd if it is a constant sequence (an=a0a_n = a_0 for every nn) with d=0d = 0, or if it comes from another sequence b0 b1 …b_0\ b_1\ \dots of degree d−1d-1 whose terms are not all 00, with an+1=(an+bn) mod ma_{n+1} = (a_n + b_n) \bmod m for every nn. The second definition gives a prediction method that uses neither multiplication nor division. (Here x mod yx \bmod y is the remainder rr of xx divided by yy, with 0≤r<y0 \le r < y.)

A constant sequence has degree 00 under all three laws.

The task is to pick the law of least degree that matches the data and predict the next term along it. Say you want to predict a3a_3 of a periodic sequence with a0=0a_0 = 0, a1=1a_1 = 1 and a2=0a_2 = 0. The data follows the periodic sequence of degree 11 with seed 0 10\ 1, so the prediction is a3=1a_3 = 1. Predicting a3=0a_3 = 0, which follows the degree 22 periodic sequence with seed 0 1 00\ 1\ 0, is wrong.

Some tasks ask you to choose, among eventually constant sequences, periodic sequences and polynomial sequences, the law that gives the lowest degree, and then to predict the next value along that law. Every prediction task given admits a unique solution.

Input

The input holds several prediction tasks. Each task comes in one of four formats.

  • Ec m a0 a1 … an−1m\ a_0\ a_1\ \dots\ a_{n-1}: eventually constant sequence
  • Pe m a0 a1 … an−1m\ a_0\ a_1\ \dots\ a_{n-1}: periodic sequence
  • Po m a0 a1 … an−1m\ a_0\ a_1\ \dots\ a_{n-1}: polynomial sequence
  • Se m a0 a1 … an−1m\ a_0\ a_1\ \dots\ a_{n-1}: your program picks the law of lowest degree itself

Tasks are separated by a semicolon ;, and a full stop . follows the last one. The tokens of a single task may be spread over several lines.

nn is at most 9090, mm is at most 1000010000, and 0≤ai≤m−10 \le a_i \le m-1 for every ii.

Output

For each prediction task print one integer on its own line: the next element of the sequence that matches the description.

Examples1

  1. Example 1

    Input
    Pe 5 0 1 2 0 1;
    Ec 2 0 1 1 1;
    Po 100 0 1 4 9 16;
    Se 50 1 1 0 1.
    
    Expected output
    2
    1
    25
    1