B. Rocket

Codeforces
IDCF1010B
Time1000ms
Memory256MB
Difficulty
binary searchinteractive
English · Original
Chinese · Translation
Formal · Original
**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 $x$ as the distance to Mars. Unfortunately, Natasha does not know $x$. But it is known that $1 \le x \le m$, where Natasha knows the number $m$. Besides, $x$ and $m$ are positive integers. Natasha can ask the rocket questions. Every question is an integer $y$ ($1 \le y \le m$). The correct answer to the question is $-1$, if $x<y$, $0$, if $x=y$, and $1$, if $x>y$. But the rocket is broken — it does not always answer correctly. Precisely: let the correct answer to the current question be equal to $t$, then, if the rocket answers this question correctly, then it will answer $t$, otherwise it will answer $-t$. In addition, the rocket has a sequence $p$ of length $n$. Each element of the sequence is either $0$ or $1$. The rocket processes this sequence in the cyclic order, that is $1$\-st element, $2$\-nd, $3$\-rd, $\ldots$, $(n-1)$\-th, $n$\-th, $1$\-st, $2$\-nd, $3$\-rd, $\ldots$, $(n-1)$\-th, $n$\-th, $\ldots$. If the current element is $1$, the rocket answers correctly, if $0$ — lies. Natasha doesn't know the sequence $p$, but she knows its length — $n$. You can ask the rocket no more than $60$ 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 $0$ 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 $m$ and $n$ ($1 \le m \le 10^9$, $1 \le n \le 30$) — the maximum distance to Mars and the number of elements in the sequence $p$. ## Interaction You can ask the rocket no more than $60$ questions. To ask a question, print a number $y$ ($1\le y\le m$) and an end-of-line character, then do the operation _flush_ and read the answer to the question. If the program reads $0$, 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$ 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 $60$. 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 $-2^{31}$ and $2^{31}-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 $3$ integers $m,n,x$ ($1\le x\le m\le 10^9$, $1\le n\le 30$) — the maximum distance to Mars, the number of elements in the sequence $p$ and the current distance to Mars. In the second line, enter $n$ numbers, each of which is equal to $0$ or $1$ — sequence $p$. The hacked solution will not have access to the number $x$ and sequence $p$. [samples] ## 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 $3$, Natasha knows that it does not exceed $5$, and the rocket answers in order: correctly, incorrectly, correctly, incorrectly ... Really: on the first query ($1$) the correct answer is $1$, the rocket answered correctly: $1$; on the second query ($2$) the correct answer is $1$, the rocket answered incorrectly: $-1$; on the third query ($4$) the correct answer is $-1$, the rocket answered correctly: $-1$; on the fourth query ($5$) the correct answer is $-1$, the rocket answered incorrectly: $1$; on the fifth query ($3$) the correct and incorrect answer is $0$.
*这是一个交互式问题。* 娜塔莎即将飞往火星。终于,娜塔莎坐进了火箭。她飞啊飞……但感到无聊了。她希望立刻到达火星!于是她决定找点事情做。她想不出比计算到火星的距离更好的事情了。 设 $x$ 为到火星的距离。不幸的是,娜塔莎不知道 $x$。但已知 $1 lt.eq x lt.eq m$,其中娜塔莎知道数字 $m$。此外,$x$ 和 $m$ 均为正整数。 娜塔莎可以向火箭提问。每个问题是一个整数 $y$($1 lt.eq y lt.eq m$)。该问题的正确答案是:若 $x < y$ 则为 $-1$,若 $x = y$ 则为 $0$,若 $x > y$ 则为 $1$。但火箭坏了——它并不总是正确回答。确切地说:设当前问题的正确答案为 $t$,若火箭正确回答,则回答 $t$;否则回答 $-t$。 此外,火箭有一个长度为 $n$ 的序列 $p$。序列的每个元素为 $0$ 或 $1$。火箭按循环顺序处理该序列,即第 $1$ 个元素、第 $2$ 个元素、第 $3$ 个元素、$dots.h$、第 $(n -1)$ 个元素、第 $n$ 个元素、第 $1$ 个元素、第 $2$ 个元素、第 $3$ 个元素、$dots.h$、第 $(n -1)$ 个元素、第 $n$ 个元素、$dots.h$。若当前元素为 $1$,火箭正确回答;若为 $0$,则说谎。娜塔莎不知道序列 $p$,但她知道其长度 $n$。 你可以向火箭提问不超过 $60$ 次。 帮助娜塔莎找出到火星的距离。假设在娜塔莎提问过程中,到火星的距离保持不变。 如果你的解决方案没有从火箭收到答案 $0$(即使根据已收到的火箭回答可以唯一确定到火星的距离),你的解决方案将不会被接受。 第一行包含两个整数 $m$ 和 $n$($1 lt.eq m lt.eq 10^9$,$1 lt.eq n lt.eq 30$)——到火星的最大距离和序列 $p$ 的元素个数。 你可以向火箭提问不超过 $60$ 次。 要提问,请打印一个数 $y$($1 lt.eq y lt.eq m$)和一个换行符,然后执行 _flush_ 操作并读取该问题的答案。 如果程序读到 $0$,则距离正确,你必须立即终止程序(例如,调用 _exit(0)_)。如果你忽略这一点,你可能获得任意评测结果,因为你的程序将继续从已关闭的输入流中读取。 如果在某时刻程序读到 $-2$ 作为答案,必须立即结束(例如,调用 _exit(0)_)。你将收到“Wrong answer”评测结果,这意味着请求不合法或提问次数超过 $60$ 次。如果你忽略这一点,你可能获得任意评测结果,因为你的程序将继续从已关闭的输入流中读取。 如果你的程序的请求不是一个在 $-2^(31)$ 到 $2^(31) -1$(含)之间且无前导零的有效整数,则你可能获得任意评测结果。 如果你不输出任何内容或忘记刷新输出,你可能获得“Idleness limit exceeded”。 要刷新输出缓冲区,可以在打印查询和换行符后使用: *C++*: _fflush(stdout)_ *Java*: _System.out.flush()_ *Python*: _stdout.flush()_ *Pascal*: _flush(output)_ 其他语言请参阅文档。 *Hack 格式* 使用以下格式进行 Hack: 第一行打印 $3$ 个整数 $m, n, x$($1 lt.eq x lt.eq m lt.eq 10^9$,$1 lt.eq n lt.eq 30$)——到火星的最大距离、序列 $p$ 的元素个数和当前到火星的距离。 第二行输入 $n$ 个数,每个数为 $0$ 或 $1$——序列 $p$。 被 Hack 的程序无法访问数字 $x$ 和序列 $p$。 在示例中,Hack 数据如下: _5 2 3_ _1 0_ 这意味着当前到火星的距离为 $3$,娜塔莎知道它不超过 $5$,火箭按顺序回答:正确、错误、正确、错误…… 实际上: 第一次提问($1$):正确答案是 $1$,火箭正确回答:$1$; 第二次提问($2$):正确答案是 $1$,火箭错误回答:$-1$; 第三次提问($4$):正确答案是 $-1$,火箭正确回答:$-1$; 第四次提问($5$):正确答案是 $-1$,火箭错误回答:$1$; 第五次提问($3$):正确和错误答案均为 $0$。 ## Input 第一行包含两个整数 $m$ 和 $n$($1 lt.eq m lt.eq 10^9$,$1 lt.eq n lt.eq 30$)——到火星的最大距离和序列 $p$ 的元素个数。 ## Interaction 你可以向火箭提问不超过 $60$ 次。要提问,请打印一个数 $y$($1 lt.eq y lt.eq m$)和一个换行符,然后执行 _flush_ 操作并读取该问题的答案。如果程序读到 $0$,则距离正确,你必须立即终止程序(例如,调用 _exit(0)_)。如果你忽略这一点,你可能获得任意评测结果,因为你的程序将继续从已关闭的输入流中读取。如果在某时刻程序读到 $-2$ 作为答案,必须立即结束(例如,调用 _exit(0)_)。你将收到“Wrong answer”评测结果,这意味着请求不合法或提问次数超过 $60$ 次。如果你忽略这一点,你可能获得任意评测结果,因为你的程序将继续从已关闭的输入流中读取。如果你的程序的请求不是一个在 $-2^(31)$ 到 $2^(31) -1$(含)之间且无前导零的有效整数,则你可能获得任意评测结果。如果你不输出任何内容或忘记刷新输出,你可能获得“Idleness limit exceeded”。要刷新输出缓冲区,可以在打印查询和换行符后使用: *C++*: _fflush(stdout)_ *Java*: _System.out.flush()_ *Python*: _stdout.flush()_ *Pascal*: _flush(output)_ 其他语言请参阅文档。 *Hack 格式* 使用以下格式进行 Hack: 第一行打印 $3$ 个整数 $m, n, x$($1 lt.eq x lt.eq m lt.eq 10^9$,$1 lt.eq n lt.eq 30$)——到火星的最大距离、序列 $p$ 的元素个数和当前到火星的距离。 第二行输入 $n$ 个数,每个数为 $0$ 或 $1$——序列 $p$。 被 Hack 的程序无法访问数字 $x$ 和序列 $p$。 [samples] ## Note 在示例中,Hack 数据如下: _5 2 3_ _1 0_ 这意味着当前到火星的距离为 $3$,娜塔莎知道它不超过 $5$,火箭按顺序回答:正确、错误、正确、错误…… 实际上: 第一次提问($1$):正确答案是 $1$,火箭正确回答:$1$; 第二次提问($2$):正确答案是 $1$,火箭错误回答:$-1$; 第三次提问($4$):正确答案是 $-1$,火箭正确回答:$-1$; 第四次提问($5$):正确答案是 $-1$,火箭错误回答:$1$; 第五次提问($3$):正确和错误答案均为 $0$。
**Definitions** Let $ m \in \mathbb{Z}^+ $ be the upper bound on the distance to Mars, with $ 1 \le x \le m $. Let $ n \in \mathbb{Z}^+ $ be the length of a fixed but unknown binary sequence $ p = (p_1, p_2, \dots, p_n) \in \{0,1\}^n $, where $ p_i = 1 $ indicates correct response, $ p_i = 0 $ indicates flipped response. Let $ x \in \{1, 2, \dots, m\} $ be the unknown target value. The rocket responds to query $ y \in \{1, \dots, m\} $ as follows: Let $ t = \begin{cases} -1 & \text{if } x < y \\ 0 & \text{if } x = y \\ 1 & \text{if } x > y \end{cases} $ Let $ r = (i \mod n) + 1 $ be the index of the current element in $ p $ (cyclic, 1-based). Then the rocket outputs: $$ a = \begin{cases} t & \text{if } p_r = 1 \\ -t & \text{if } p_r = 0 \end{cases} $$ **Constraints** 1. $ 1 \le m \le 10^9 $ 2. $ 1 \le n \le 30 $ 3. $ 1 \le x \le m $ 4. At most 60 queries allowed. 5. The process terminates only upon receiving answer $ 0 $. **Objective** Determine $ x $ using at most 60 adaptive queries $ y $, such that the response to the $ i $-th query is determined by $ p_{(i \mod n) + 1} $, without knowledge of $ p $ or $ x $.
Samples
Input #1
5 2
1
-1
-1
1
0
Output #1
1
2
4
5
3
API Response (JSON)
{
  "problem": {
    "name": "B. Rocket",
    "description": {
      "content": "**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 fi",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF1010B"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "**This is an interactive problem.**\n\nNatasha 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 fi...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "*这是一个交互式问题。*\n\n娜塔莎即将飞往火星。终于,娜塔莎坐进了火箭。她飞啊飞……但感到无聊了。她希望立刻到达火星!于是她决定找点事情做。她想不出比计算到火星的距离更好的事情了。\n\n设 $x$ 为到火星的距离。不幸的是,娜塔莎不知道 $x$。但已知 $1 lt.eq x lt.eq m$,其中娜塔莎知道数字 $m$。此外,$x$ 和 $m$ 均为正整数。\n\n娜塔莎可以向火箭提问。每个问题是一个整...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ m \\in \\mathbb{Z}^+ $ be the upper bound on the distance to Mars, with $ 1 \\le x \\le m $.  \nLet $ n \\in \\mathbb{Z}^+ $ be the length of a fixed but unknown binary sequence $ p =...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments