codeforces 1011D. Rocket(交互+二分)

                                                   D. Rocket

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

This is an interactive problem.

Natasha is going to fly to Mars. Finally, Natasha sat in the rocket. She flies, flies... but gets bored. She wishes to arrive to Mars already! So she decides to find something to occupy herself. She couldn't think of anything better to do than to calculate the distance to the red planet.

Let's define xx as the distance to Mars. Unfortunately, Natasha does not know xx. But it is known that 1≤x≤m1≤x≤m, where Natasha knows the number mm. Besides, xx and mm are positive integers.

Natasha can ask the rocket questions. Every question is an integer yy (1≤y≤m1≤y≤m). The correct answer to the question is −1−1, if x<yx<y, 00, if x=yx=y, and 11, if x>yx>y. But the rocket is broken — it does not always answer correctly. Precisely: let the correct answer to the current question be equal to tt, then, if the rocket answers this question correctly, then it will answer tt, otherwise it will answer −t−t.

In addition, the rocket has a sequence pp of length nn. Each element of the sequence is either 00 or 11. The rocket processes this sequence in the cyclic order, that is 11-st element, 22-nd, 33-rd, ……, (n−1)(n−1)-th, nn-th, 11-st, 22-nd, 33-rd, ……, (n−1)(n−1)-th, nn-th, ……. If the current element is 11, the rocket answers correctly, if 00 — lies. Natasha doesn't know the sequence pp, but she knows its length — nn.

You can ask the rocket no more than 6060 questions.

Help Natasha find the distance to Mars. Assume, that the distance to Mars does not change while Natasha is asking questions.

Your solution will not be accepted, if it does not receive an answer 00 from the rocket (even if the distance to Mars is uniquely determined by the already received rocket's answers).

Input

The first line contains two integers mm and nn (1≤m≤1091≤m≤109, 1≤n≤301≤n≤30) — the maximum distance to Mars and the number of elements in the sequence pp.

Interaction

You can ask the rocket no more than 6060 questions.

To ask a question, print a number yy (1≤y≤m1≤y≤m) and an end-of-line character, then do the operation flush and read the answer to the question.

If the program reads 00, then the distance is correct and you must immediately terminate the program (for example, by calling exit(0)). If you ignore this, you can get any verdict, since your program will continue to read from the closed input stream.

If at some point your program reads −2−2 as an answer, it must immediately end (for example, by calling exit(0)). You will receive the "Wrong answer" verdict, and this will mean that the request is incorrect or the number of requests exceeds 6060. If you ignore this, you can get any verdict, since your program will continue to read from the closed input stream.

If your program's request is not a valid integer between −231−231 and 231−1231−1 (inclusive) without leading zeros, then you can get any verdict.

You can get "Idleness limit exceeded" if you don't print anything or if you forget to flush the output.

To flush the output buffer you can use (after printing a query and end-of-line):

  • fflush(stdout) in C++;
  • System.out.flush() in Java;
  • stdout.flush() in Python;
  • flush(output) in Pascal;
  • See the documentation for other languages.

Hacking

Use the following format for hacking:

In the first line, print 33 integers m,n,xm,n,x (1≤x≤m≤1091≤x≤m≤109, 1≤n≤301≤n≤30) — the maximum distance to Mars, the number of elements in the sequence pp and the current distance to Mars.

In the second line, enter nn numbers, each of which is equal to 00 or 11 — sequence pp.

The hacked solution will not have access to the number xx and sequence pp.

Example

input

Copy

5 2
1
-1
-1
1
0

output

Copy

1
2
4
5
3

Note

In the example, hacking would look like this:

5 2 3

1 0

This means that the current distance to Mars is equal to 33, Natasha knows that it does not exceed 55, and the rocket answers in order: correctly, incorrectly, correctly, incorrectly ...

Really:

on the first query (11) the correct answer is 11, the rocket answered correctly: 11;

on the second query (22) the correct answer is 11, the rocket answered incorrectly: −1−1;

on the third query (44) the correct answer is −1−1, the rocket answered correctly: −1−1;

on the fourth query (55) the correct answer is −1−1, the rocket answered incorrectly: 11;

on the fifth query (33) the correct and incorrect answer is 00.

 

 

一、原题地址

点我传送

 

二、大致题意

现在有一个要猜测的数字。一开始给出m,n。要猜测的数字大小最大可能为m。然后你每次输出一个数字给机器让机器回答你给出的数字是大了还是小了。但是机器会欺骗你,也就是说有时候他会故意反着说。但是机器的欺骗是存在周期性的,也就是说他存在一个 T 来保证第i次回答和第i+T次回答都是正确的或者是骗人的。而这个周期就是n。

当你给出的数过大机器会返回 -1

当你给出的数过小机器会返回 1

当你给出的数就是答案的时候会返回0

 

三、思路

先用一组全是1的询问来得到机器的欺骗序列,这样就得到了周期内机器的正确性。然后就是一般的二分操作。注意题目中给出的提示需要使用 fflush(stdout)来保证不超时。

 

四、代码

#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <cstdio>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <iterator>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;
#define LL long long
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;

int m, n;
int lie_or_not[65];
int main()
{
	scanf("%d %d", &m, &n);
	for (int i = 0; i < n; i++)
	{
		printf("1\n");
		fflush(stdout);
		scanf("%d", &lie_or_not[i]);
		if (lie_or_not[i] != -1 && lie_or_not[i] != 1)
			return 0;
	}
	int l = 1, r = m;
	int mid;
	int cnt = 0, t;
	while (l <= r)
	{
		mid = (l + r) / 2;
		printf("%d\n", mid);
		fflush(stdout);
		scanf("%d", &t);
		if (t == 0)
			return 0;
		if (lie_or_not[cnt%n] == -1)
		{
			if (t == -1)
			{
				l = mid + 1;
			}
			else
			{
				 r = mid - 1;
			}
		}
		else
		{
			if (t == 1)
			{
				l = mid + 1; 
			}
			else
			{
				r = mid - 1;
			}
		}
		cnt++;
	}

	getchar();
	getchar();
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值