Arithmetic Progression
Contest: Codeforces Round 538 (Div. 2)
Difficulty rating: 2200
Tags: binary search, interactive, number theory, probabilities

This is an interactive problem!

An arithmetic progression or arithmetic sequence is a sequence of integers such that the subtraction of element with its previous element ($$$x_i - x_{i - 1}$$$, where $$$i \ge 2$$$) is constant — such difference is called a common difference of the sequence.

That is, an arithmetic progression is a sequence of form $$$x_i = x_1 + (i - 1) d$$$, where $$$d$$$ is a common difference of the sequence.

There is a secret list of $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$.

It is guaranteed that all elements $$$a_1, a_2, \ldots, a_n$$$ are between $$$0$$$ and $$$10^9$$$, inclusive.

This list is special: if sorted in increasing order, it will form an arithmetic progression with positive common difference ($$$d > 0$$$). For example, the list $$$[14, 24, 9, 19]$$$ satisfies this requirement, after sorting it makes a list $$$[9, 14, 19, 24]$$$, which can be produced as $$$x_n = 9 + 5 \cdot (n - 1)$$$.

Also you are also given a device, which has a quite discharged battery, thus you can only use it to perform at most $$$60$$$ queries of following two types:

- Given a value $$$i$$$ ($$$1 \le i \le n$$$), the device will show the value of the $$$a_i$$$.
- Given a value $$$x$$$ ($$$0 \le x \le 10^9$$$), the device will return $$$1$$$ if an element with a value strictly greater than $$$x$$$ exists, and it will return $$$0$$$ otherwise.

Your can use this special device for at most $$$60$$$ queries. Could you please find out the smallest element and the common difference of the sequence? That is, values $$$x_1$$$ and $$$d$$$ in the definition of the arithmetic progression. Note that the array $$$a$$$ is not sorted.

Interaction format:
The interaction starts with a single integer $$$n$$$ ($$$2 \le n \le 10^6$$$), the size of the list of integers.

Then you can make queries of two types:

- "? i" ($$$1 \le i \le n$$$) — to get the value of $$$a_i$$$.
- "> x" ($$$0 \le x \le 10^9$$$) — to check whether there exists an element greater than $$$x$$$

After the query read its result $$$r$$$ as an integer.

- For the first query type, the $$$r$$$ satisfies $$$0 \le r \le 10^9$$$.
- For the second query type, the $$$r$$$ is either $$$0$$$ or $$$1$$$.
- In case you make more than $$$60$$$ queries or violated the number range in the queries, you will get a $$$r = -1$$$.
- If you terminate after receiving the -1, you will get the "Wrong answer" verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.

When you find out what the smallest element $$$x_1$$$ and common difference $$$d$$$, print

- "! $$$x_1$$$ $$$d$$$"

And quit after that. This query is not counted towards the $$$60$$$ queries limit.

After printing any query do not forget to output end of line and flush the output. Otherwise you will get Idleness limit exceeded. To do this, use:

- fflush(stdout) or cout.flush() in C++;
- System.out.flush() in Java;
- flush(output) in Pascal;
- stdout.flush() in Python;
- see documentation for other languages.

Hacks

For hack, use the following format:

The first line should contain an integer $$$n$$$ ($$$2 \le n \le 10^6$$$) — the list's size.

The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the list.

Also, after the sorting the list must form an arithmetic progression with positive common difference.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
Note that the example interaction contains extra empty lines so that it's easier to read. The real interaction doesn't contain any empty lines and you shouldn't print any extra empty lines as well.

The list in the example test is $$$[14, 24, 9, 19]$$$.

Example input:
4

0

1

14

24

9

19

Example output:
> 25

> 15

? 1

? 2

? 3

? 4

! 9 5

Editorial:
The > query type allows you to find the max value of the array through binary searching, which will cost $$$\lceil \log_2(10^9) \rceil = 30$$$ queries.
The remaining queries will be spent for the queries of the ? type to get some random elements of the array. $$$d$$$ will be calculated as greatest common divisors (GCD) of all difference between all consecutive elements, provided all elements found (yes, including the "max" one) is kept into a list sorted by increasing order.
Having d and max, we can easily find the min value: $$$min = max - d \cdot (n - 1)$$$.
The number of allowed queries seem small ($$$30$$$ queries to be exact), yet it's enough for us to have reasonable probability of passing the problem.
By some maths, we can find out the probability of our solution to fail being relatively small - approximately $$$1.86185 \cdot 10^{-9}$$$.
Keep in mind to use a good random number generator and a seed which is hard to obtain, thus making your solution truly random and not falling into corner cases.
Calculate failing probability
For simplicity, assumed that the sequence $$$a$$$ is $$$[0, 1, 2, \ldots, n-1]$$$. Suppose that we randomly (and uniformly) select a subsequence $$$S$$$ containing $$$k$$$ elements from sequence $$$a$$$ (when $$$k$$$ is the number of ? query asked). Denote $$$S = [s_1, s_2, \cdots, s_k]$$$.
Let $$$D$$$ the GCD of all difference between consecutive elements in $$$S$$$. In other word, $$$D = \gcd(s_i - s_{i-1} \; | \; 2 \leq i \leq k)$$$. Our solution will success if $$$D = 1$$$ and will fail otherwise. Let $$$P$$$ the probability that our solution will pass ($$$D = 1$$$). Then the failing probability is $$$1 - P$$$.
We will apply Möbius inversion to calculate $$$P$$$: Let $$$f(x)$$$ the probability that $$$D$$$ is divisible by $$$x$$$. Then, $$$P = \displaystyle \sum_{i=1}^{n} f(i) \cdot \mu(i)$$$ (where $$$\mu(x)$$$ is the Möbius function).
Now we need to calculate $$$f(x)$$$. It can be shown that $$$D$$$ is divisible by $$$x$$$ if and only if $$$s_i \equiv s_{i-1} \pmod{x}$$$ for all $$$2 \leq i \leq k$$$. In other word, there is some $$$r$$$ from $$$0$$$ to $$$x-1$$$ such that $$$s_i \equiv r \pmod{x}$$$ for all $$$1 \leq i \leq k$$$.
Let $$$g(r)$$$ the number of ways to select a subsequence $$$S$$$ such that $$$s_i \equiv r \pmod{x}$$$ for all $$$1 \leq i \leq k$$$. Then, according to definition:
$$$f(x) = \dfrac{ \displaystyle \sum_{r=0}^{x-1} g(r) }{\text{number of way to choose subsequence } S}.$$$
The denominator is simply $$$\binom{k}{n}$$$. To calculate $$$g(r)$$$, notice that for each $$$i$$$, the value of $$$s_i$$$ can be $$$x \cdot j + r$$$ for some integer $$$j$$$. In other word, $$$S$$$ must be a subsequence of the sequence $$$a_r = [r, x + r, 2x + r, \cdots]$$$. Let $$$q = \left \lfloor \frac{n}{x} \right \rfloor$$$.
• If $$$r < n \mod x$$$, $$$a_r$$$ has $$$q + 1$$$ elements. Therefore, $$$g(r) = \binom{k}{q+1}$$$
• If $$$n \mod x \leq r < x$$$, $$$a_r$$$ has $$$q$$$ elements. Therefore, $$$g(r) = \binom{k}{q}$$$.
In summary:
$$$f(x) = \dfrac{ \displaystyle \sum_{r=0}^{x-1} g(r)}{\binom{k}{n}} = \dfrac{(n \mod x) \cdot \binom{k}{q+1} + (n - n \mod x) \cdot \binom{k}{q}}{\binom{k}{n}}$$$
The complexity of this calculating method is $$$O(n)$$$. My implementation of the above method can be found here.