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 (xi−xi−1, where i≥2) is constant — such difference is called a common difference of the sequence.
That is, an arithmetic progression is a sequence of form xi=x1+(i−1)d, where d is a common difference of the sequence.
There is a secret list of n integers a1,a2,…,an.
It is guaranteed that all elements a1,a2,…,an are between 0 and 109, 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 xn=9+5⋅(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≤i≤n), the device will show the value of the ai.
Given a value x (0≤x≤109), 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 x1 and d in the definition of the arithmetic progression. Note that the array a is not sorted.
Interaction
The interaction starts with a single integer n (2≤n≤106), the size of the list of integers.
Then you can make queries of two types:
“? i” (1≤i≤n) — to get the value of ai.
“> x” (0≤x≤109) — 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≤r≤109.
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 x1 and common difference d, print
“! x1 d”
第二类询问显然是用来二分查找最大值的,剩下只能使用第一类询问了,还剩下三十多次机会,我们用来随机询问序列里面的值,然后把这些数字排序,求出相邻两数之差的最大公约数,就可以作为答案了。
这么做有运气成分在,但是基本上求出答案错误的概率很小。有点像“生日悖论”。不会证明,但瞎想一下,在原来的有序等差数列中取出三十多个数,考虑这三十多个数差值的gcd是公差d的倍数t,可能性最大的就是2|t了,要想做到这一点,等价于从n/2个数中挑这些数,最后方案数的两倍,规模显然远远小从n个数中挑这些数,大概概率也就2的负三十次幂左右,其他情况概率更小了。
注意最好保证挑选的数字不重复。另外不要用rand()了,范围太小(之前一直用rand()*rand()%mo实际上也是中间多,两边少的不均匀方法),C++11有更好的随机数生成器,详见 https://codeforces.com/blog/entry/61587
#include<cstdio>
#include<random>
#include<chrono>
#include<algorithm>
#define M (L+R>>1)
using namespace std;
int L,R,t,a[40],n,an,d;
bool vis[1000005];
int query(char c, int x)
{
int res;
printf("%c %d\n",c,x);
fflush(stdout);
scanf("%d",&res);
return res;
}
int main()
{
scanf("%d",&n);
mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());
uniform_int_distribution<> dis(1,n);
L=0,R=1E9;
while(L<R)
{
if(query('>',M))
L=M+1;
else
R=M;
t++;
}
a[1]=L; //L是最大的那个数的大小
an=1;
for(int i=t+1,tmp;i<=60;i++)
{
tmp=dis(gen);
for(int j=1;j<=50&&vis[tmp];j++) //n太小或者实在运气太差就算了吧,允许一下重复。
tmp=dis(gen);
vis[tmp]=true;
a[++an]=query('?',dis(gen));
}
sort(a+1,a+an+1);
for(int i=2;i<=an;i++)
d=__gcd(d,a[i]-a[i-1]);
printf("! %d %d",L-(n-1)*d,d);
return 0;
}