[交互式+随机] 询问求等差数列 CF1114E

21 篇文章 0 订阅
1 篇文章 0 订阅
这是一个交互式问题,需要找到一个特殊列表,当按升序排列时,它形成一个正公差的等差数列。通过不超过60次查询,找出列表的最小元素和公差。
摘要由CSDN通过智能技术生成

E. Arithmetic Progression

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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−1xi−xi−1, where i≥2i≥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)dxi=x1+(i−1)d, where dd is a common difference of the sequence.

There is a secret list of nn integers a1,a2,…,ana1,a2,…,an.

It is guaranteed that all elements a1,a2,…,ana1,a2,…,an are between 00 and 109109, inclusive.

This list is special: if sorted in increasing order, it will form an arithmetic progression with positive common difference (d>0d>0). For example, the list [14,24,9,19][14,24,9,19] satisfies this requirement, after sorting it makes a list [9,14,19,24][9,14,19,24], which can be produced as xn=9+5⋅(n−1)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 6060 queries of following two types:

  • Given a value ii (1≤i≤n1≤i≤n), the device will show the value of the aiai.
  • Given a value xx (0≤x≤1090≤x≤109), the device will return 11 if an element with a value strictly greater than xx exists, and it will return 00otherwise.

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

Interaction

The interaction starts with a single integer nn (2≤n≤1062≤n≤106), the size of the list of integers.

Then you can make queries of two types:

  • "? i" (1≤i≤n1≤i≤n) — to get the value of aiai.
  • "> x" (0≤x≤1090≤x≤109) — to check whether there exists an element greater than xx

After the query read its result rr as an integer.

  • For the first query type, the rr satisfies 0≤r≤1090≤r≤109.
  • For the second query type, the rr is either 00 or 11.
  • In case you make more than 6060 queries or violated the number range in the queries, you will get a r=−1r=−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 x1x1 and common difference dd, print

  • "! x1x1 dd"

And quit after that. This query is not counted towards the 6060 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 nn (2≤n≤1062≤n≤106) — the list's size.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1090≤ai≤109) — the elements of the list.

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

Example

input

Copy

4

0

1

14

24

9

19

output

Copy

> 25

> 15

? 1

? 2

? 3

? 4

! 9 5

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][14,24,9,19].

#include <bits/stdc++.h>
#define ll long long
#define db double
using namespace std;

const int mn = 1e6 + 5;
const int inf = 1e9;

int ans[70];
int ask1(int x)
{
	printf("? %d\n", x);
	cout.flush();
	int t;
	cin >> t;
	return t;
}
bool ask2(int x)
{
	printf("> %d\n", x);
	cout.flush();
	bool t;
	cin >> t;
	return t;
}

int rd[110];
vector<int> vec;
int main()
{
	int n;
	cin >> n;

	int vmax, ci = 0;
	int L = 0, R = inf;
	while (L <= R)
	{
		int mid = (L + R) / 2;
		bool t = ask2(mid);
		ci++;
		if (t == 1)
		{
			vmax = mid + 1;
			L = mid + 1;
		}
		else
			R = mid - 1;
	}

	ci = 60 - ci;
	int len = min(ci, n);

	srand(time(NULL) + 20011104 + 20020918);
	for (int i = 1; i <= ci; i++)
		rd[i] = 1ll * rand() * 1ll * rand() * 1ll * rand() % (1ll * n) + 1;

	for (int i = 1; i <= ci; i++)
	{
		ans[i] = ask1(rd[i]);
	}
	sort(ans + 1, ans + ci + 1);

	int d = 0;
	for (int i = 2; i <= ci; i++)   // 差值是d的倍数
		d = gcd(d, ans[i] - ans[i - 1]);

	printf("! %d %d\n", vmax - (n - 1) * d, d);
	cout.flush();

	return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值