This page is still under construction.

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

Jumping Robot

Time limit1sMemory limit512 MB

Summary
Find the minimum starting agility for a circular route where agility rises by 1 per jump, and name a platform that succeeds with it.
Level

Medium6 of 10

Topics
Array, Sliding window
Solved
No attempts yet

Statement

Flatland Dynamics is developing a jumping robot. The robot is tested on a circular route of nn special platforms, numbered from 1 to nn. The distance between platform ii and platform i+1i+1 is did_i, and the distance between platform nn and platform 1 is dnd_n.

The robot has an AI and learns to jump farther during the test. At any moment, the robot has an agility, which is an integer aa. The robot can jump from platform ii to platform i+1i+1 if a≥dia \ge d_i. Likewise, it can jump from platform nn to platform 1 if a≥dna \ge d_n. After each jump, the robot's agility increases by 1.

The developers choose one platform as the starting point. The experiment succeeds if the robot can make nn jumps, going from each platform to the next, complete the full circle, and return to the starting platform.

The developers want to know the minimum initial agility that lets the experiment succeed, and which platform the robot should start from.

Input

The first line contains nn (3≤n≤1073 \le n \le 10^7).

The second line contains an integer ff that describes how the distance array is given.

If f=1f = 1, the third line contains nn integers d1,d2,…,dnd_1, d_2, \ldots, d_n (1≤di≤1091 \le d_i \le 10^9).

If f=2f = 2, the third line contains an integer mm (2≤m≤min⁡(n,105)2 \le m \le \min(n, 10^5)) and three integers xx, yy, and zz (0≤x,y,z≤1090 \le x, y, z \le 10^9). The fourth line contains mm integers c1,c2,…,cmc_1, c_2, \ldots, c_m (1≤ci≤1091 \le c_i \le 10^9). The distances are computed as follows.

If 1≤i≤m1 \le i \le m, then di=cid_i = c_i.

If m+1≤i≤nm + 1 \le i \le n, then di=((x⋅di−2+y⋅di−1+z) mod 109)+1d_i = ((x \cdot d_{i-2} + y \cdot d_{i-1} + z) \bmod 10^9) + 1. Here  mod \bmod is the remainder of integer division, written as % in C++, Java, and Python.

Output

Print two integers. The first is the minimum initial agility aa. The second is the number of a starting platform from which the experiment succeeds with that agility.

If several starting platforms work, print any of them.

Hint

In the second example, the distance array is [1,2,3,4,5,18,45,112,273,662][1, 2, 3, 4, 5, 18, 45, 112, 273, 662]. The values from d6d_6 to d10d_{10} are computed as follows.

d6=((1⋅d4+2⋅d5+3) mod 109)+1=((1⋅4+2⋅5+3) mod 109)+1=18d_6 = ((1 \cdot d_4 + 2 \cdot d_5 + 3) \bmod 10^9) + 1 = ((1 \cdot 4 + 2 \cdot 5 + 3) \bmod 10^9) + 1 = 18

d7=((1⋅d5+2⋅d6+3) mod 109)+1=((1⋅5+2⋅18+3) mod 109)+1=45d_7 = ((1 \cdot d_5 + 2 \cdot d_6 + 3) \bmod 10^9) + 1 = ((1 \cdot 5 + 2 \cdot 18 + 3) \bmod 10^9) + 1 = 45

d8=((1⋅d6+2⋅d7+3) mod 109)+1=((1⋅18+2⋅45+3) mod 109)+1=112d_8 = ((1 \cdot d_6 + 2 \cdot d_7 + 3) \bmod 10^9) + 1 = ((1 \cdot 18 + 2 \cdot 45 + 3) \bmod 10^9) + 1 = 112

d9=((1⋅d7+2⋅d8+3) mod 109)+1=((1⋅45+2⋅112+3) mod 109)+1=273d_9 = ((1 \cdot d_7 + 2 \cdot d_8 + 3) \bmod 10^9) + 1 = ((1 \cdot 45 + 2 \cdot 112 + 3) \bmod 10^9) + 1 = 273

d10=((1⋅d8+2⋅d9+3) mod 109)+1=((1⋅112+2⋅273+3) mod 109)+1=662d_{10} = ((1 \cdot d_8 + 2 \cdot d_9 + 3) \bmod 10^9) + 1 = ((1 \cdot 112 + 2 \cdot 273 + 3) \bmod 10^9) + 1 = 662

Examples2

  1. Example 1

    Input
    5
    1
    3 7 4 2 5
    
    Expected output
    4 3
    
  2. Example 2

    Input
    10
    2
    5 1 2 3
    1 2 3 4 5
    
    Expected output
    653 1