This page is still under construction.

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

Period of a Slot Machine

Time limit2sMemory limit512 MB

Summary
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 w=w1w2w3w4w5w6w = w_1 w_2 w_3 w_4 w_5 w_6 with 0≤w1,w2,w3,w4,w5,w6≤90 \le w_1, w_2, w_3, w_4, w_5, w_6 \le 9.

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 T[i]T[i] be the ii-th outcome of a machine. The sequence starts with a truly random block T[1],T[2],…,T[k]T[1], T[2], \dots, T[k] of length kk, and after it there is a positive integer pp such that T[i+p]=T[i]T[i+p] = T[i] for every i>ki > k. Anyone who learns the exact values of kk and pp knows in advance when a good combination is due and beats the casino by betting a lot of money on that round.

Only nn outcomes have been observed, so a pair (k,p)(k, p) satisfies the condition when T[i+p]=T[i]T[i+p] = T[i] holds for every ii with k<ik < i and i+p≤ni + p \le n. Here kk is an integer with k≥0k \ge 0 and pp is an integer with p≥1p \ge 1.

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 kk and pp, and there are many candidates. One extreme is k=5k = 5 and p=1p = 1, another is k=0k = 0 and p=6p = 6. A candidate is more plausible when both kk and pp are small, so take the pair with the smallest k+pk + p. If two or more pairs share that value, take the one with the smallest pp among them. In this example the answer is k=1k = 1 and p=2p = 2.

You are given nn consecutive outcomes T[1],T[2],…,T[n]T[1], T[2], \dots, T[n] of a slot machine. Write a program that computes kk and pp under the rule above.

Input

The first line contains the length nn (1≤n≤1061 \le n \le 10^6) of the outcome sequence observed so far.

The second line contains nn numbers T[1],T[2],…,T[n]T[1], T[2], \dots, T[n] separated by spaces. Each number is an integer between 0 and 999999.

Output

Print kk and pp on the first line, separated by a space.

Examples2

  1. Example 1

    Input
    6
    612534 3157 423 3157 423 3157
    
    Expected output
    1 2
    
  2. Example 2

    Input
    9
    1 2 1 3 1 2 1 3 1
    
    Expected output
    0 4