Sequence Prediction
Time limit1sMemory limit128 MB
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 and , where is given in the input and never exceeds .
Eventually constant sequences. A sequence is eventually constant if there is a number such that for every . The least such is the degree of the sequence. For example, is an eventually constant sequence of degree .
Periodic sequences. A periodic sequence is given by a seed such that for every . The degree of a periodic sequence is the least for which is a seed of the sequence. For example, is a periodic sequence of degree with seed .
Polynomial sequences. A polynomial sequence modulo of degree is a sequence generated by an integer-valued polynomial of degree . For example, is a polynomial sequence modulo of degree , generated by . A second definition works too. A sequence is a polynomial sequence modulo of degree if it is a constant sequence ( for every ) with , or if it comes from another sequence of degree whose terms are not all , with for every . The second definition gives a prediction method that uses neither multiplication nor division. (Here is the remainder of divided by , with .)
A constant sequence has degree 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 of a periodic sequence with , and . The data follows the periodic sequence of degree with seed , so the prediction is . Predicting , which follows the degree periodic sequence with seed , 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: eventually constant sequencePe: periodic sequencePo: polynomial sequenceSe: 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.
is at most , is at most , and for every .
Output
For each prediction task print one integer on its own line: the next element of the sequence that matches the description.