Period of a Slot Machine
Time limit2sMemory limit512 MB
Given a sequence of n outcomes, find k and p minimizing k+p (ties by smaller p) such that T[i+p]=T[i] for all i>k with i+p<=n.
- Level
Hard8 of 10
- Topics
- String matching, Implementation, String
- Solved
- No attempts yet
Problem
Slot machines are popular game machines in casinos. The machine considered here has six places where a figure appears, and the combination of figures decides whether you win or lose money. There are ten kinds of figures, so each figure is written as a single digit from 0 to 9. One outcome of the machine is then a six-digit number with .

Figure 1. The layout of a slot machine.
Old slot machines were built from mechanical parts, but PC based devices have taken their place. That change left one fatal weakness: the outcomes come from a pseudo-random number generator, so the sequence of outcomes is periodic. Let be the -th outcome of a machine. The sequence starts with a truly random block of length , and after it there is a positive integer such that for every . Anyone who learns the exact values of and knows in advance when a good combination is due and beats the casino by betting a lot of money on that round.
Only outcomes have been observed, so a pair satisfies the condition when holds for every with and . Here is an integer with and is an integer with .
For example, suppose the first six outcomes are 612534, 3157, 423, 3157, 423, 3157. Leading zeros are dropped, so 3157 stands for 003157 and 423 stands for 000423. To predict the tenth outcome you need the exact values of and , and there are many candidates. One extreme is and , another is and . A candidate is more plausible when both and are small, so take the pair with the smallest . If two or more pairs share that value, take the one with the smallest among them. In this example the answer is and .
You are given consecutive outcomes of a slot machine. Write a program that computes and under the rule above.
Input
The first line contains the length () of the outcome sequence observed so far.
The second line contains numbers separated by spaces. Each number is an integer between 0 and 999999.
Output
Print and on the first line, separated by a space.