#B3734. Unlocking the Tortoise's Chest
Unlocking the Tortoise's Chest
Unlocking the Tortoise's Chest
The tortoise has found a treasure chest with a combination lock. The lock consists of \( n \) dials. Initially, each dial displays an integer between 0 and 99. You can rotate a dial up or down. Rotating a dial upward increases its number by 1 modulo 100 (i.e. \( x \) becomes \( (x+1) \bmod 100 \)), while rotating it downward decreases its number by 1 modulo 100 (i.e. \( x \) becomes \( (x+99) \bmod 100 \)).
However, the lock is old and the more you rotate a dial, the more time it takes. In fact, if a dial is rotated \( k \) times then it costs \( k^2 \) units of time.
The lock will open only when the numbers on all dials form a strictly increasing sequence from left to right. The initial numbers on the dials are generated by a pseudorandom process. You are given an integer \( R_1 \) satisfying \( 0\le R_1 1 \) the sequence \( R \) is defined by
[ R_i = (R_{i-1}\times 6807+2831) \bmod 201701. ]
The number displayed on dial \( i \) is \( R_i \bmod 100 \>.
Your task is to determine the minimum total time required to adjust the dials (by rotating them either upward or downward) so that the final configuration forms a strictly increasing sequence from left to right. If \( n > 100 \) it is impossible to obtain a strictly increasing sequence because the values are between 0 and 99; in that case output -1.
inputFormat
The input consists of two integers:
- \( n \): the number of dials, and
- \( R_1 \): the seed for generating the initial numbers.
It is guaranteed that \( 0 \le R_1 < 201701 \).
outputFormat
Output a single integer representing the minimum total time needed to adjust the dials so that they form a strictly increasing sequence. If it is impossible, output -1.
sample
3 1
0