E. Guess two numbers

Codeforces
IDCF1008E
Time2000ms
Memory256MB
Difficulty
binary searchinteractive
English · Original
Chinese · Translation
Formal · Original
**This is an interactive problem.** Vasya and Vitya play a game. Vasya thought of two integers $a$ and $b$ from $1$ to $n$ and Vitya tries to guess them. Each round he tells Vasya two numbers $x$ and $y$ from $1$ to $n$. If both $x=a$ and $y=b$ then Vitya wins. Else Vasya must say one of the three phrases: 1. $x$ is less than $a$; 2. $y$ is less than $b$; 3. $x$ is greater than $a$ or $y$ is greater than $b$. Vasya can't lie, but if multiple phrases are true, he may choose any of them. For example, if Vasya thought of numbers $2$ and $4$, then he answers with the phrase $3$ to a query $(3, 4)$, and he can answer with the phrase $1$ or phrase $3$ to a query $(1, 5)$. Help Vitya win in no more than $600$ rounds. ## Input The first line contains a single integer $n$ ($1 \leq n \leq 10^{18}$) — the upper limit of the numbers. ## Interaction First, you need to read the number $n$, after that you can make queries. To make a query, print two integers: $x$ and $y$ ($1 \leq x, y \leq n$), then _flush_ the output. After each query, read a single integer $ans$ ($0 \leq ans \leq 3$). If $ans > 0$, then it is the number of the phrase said by Vasya. If $ans = 0$, it means that you win and your program should terminate. If you make more than $600$ queries or make an incorrect query, you will get _Wrong Answer_. Your solution will get _Idleness Limit Exceeded_, if you don't print anything or forget to _flush_ the output. To _flush_ you need to do the following right after printing a query and a line end: * _fflush(stdout)_ in C++; * _System.out.flush()_ in Java; * _stdout.flush()_ in Python; * _flush(output)_ in Pascal; * For other languages see documentation. **Hacks format** For hacks, use the following format: In the first line, print a single integer $n$ ($1 \leq n \leq 10^{18}$) — the upper limit of the numbers. In the second line, print two integers $a$ and $b$ ($1 \leq a, b \leq n$) — the numbers which Vasya thought of. In the third line, print a single integer $m$ ($1 \leq m \leq 10^5$) — the number of instructions for the interactor. In each of the next $m$ lines, print five integers: $x_i$, $y_i$, $r^{12}_i$, $r^{13}_i$, and $r^{23}_i$ ($1 \leq x_i, y_i \leq n$), where $r^{ST}_i$ equals to either number $S$ or number $T$. While answering the query $x\,\, y$, the interactor finds a number $i$ from $1$ to $n$ with the minimal value $|x-x_i| + |y-y_i|$. If multiple numbers can be chosen, the least $i$ is preferred. Then the interactor answers to the query, but if there are two phrases $S$ and $T$ that can be given, then $r^{ST}_i$ is chosen. For example, the sample test data file contains the following: 5 2 4 2 2 5 1 1 2 4 1 2 3 3 [samples] ## Note Let's analyze the sample test. The chosen numbers are $2$ and $4$. The interactor was given two instructions. For the query $(4, 3)$, it can return $2$ or $3$. Out of the two instructions the second one is chosen, so the interactor returns $a^{23}_2=3$. For the query $(3, 4)$, it can return only $3$. For the query $(3, 3)$, it can return $2$ or $3$. Out of the two instructions the first one is chosen (since in case of equal values, the least number is preferred), so the interactor returns $a^{23}_1=2$. For the query $(1, 5)$, it can return $1$ or $3$. Out of the two instructions the first one is chosen, so the interactor returns $a^{13}_1=1$. In the fifth query $(2, 4)$, the numbers are guessed correctly, the player wins.
*这是一个交互式问题。* Vasya 和 Vitya 玩一个游戏。Vasya 想了两个整数 $a$ 和 $b$,范围在 $1$ 到 $n$ 之间,Vitya 试图猜出它们。每一轮,他告诉 Vasya 两个数 $x$ 和 $y$,范围也在 $1$ 到 $n$ 之间。如果 $x = a$ 且 $y = b$,则 Vitya 获胜。否则,Vasya 必须说出以下三个短语之一: Vasya 不能说谎,但如果多个短语都为真,他可以选择其中任意一个。例如,如果 Vasya 想的数是 $2$ 和 $4$,那么对于查询 $(3, 4)$,他会回答短语 $3$;对于查询 $(1, 5)$,他可以回答短语 $1$ 或短语 $3$。 帮助 Vitya 在不超过 $600$ 轮内获胜。 第一行包含一个整数 $n$ ($1 lt.eq n lt.eq 10^(18)$) —— 数字的上限。 首先,你需要读取数字 $n$,之后你可以进行查询。 要进行一次查询,输出两个整数:$x$ 和 $y$ ($1 lt.eq x, y lt.eq n$),然后 _刷新_ 输出。 每次查询后,读取一个整数 $ans$ ($0 lt.eq ans lt.eq 3$)。 如果 $ans > 0$,则表示 Vasya 说出的短语编号。 如果 $ans = 0$,表示你猜中了,你的程序应终止。 如果你进行了超过 $600$ 次查询或进行了无效查询,你将获得 _错误答案_。 如果你没有输出任何内容或忘记 _刷新_ 输出,你的程序将获得 _空闲超时_。 要在打印查询和换行后 _刷新_ 输出,你需要执行以下操作: *黑客格式* 对于黑客输入,使用以下格式: 第一行,输出一个整数 $n$ ($1 lt.eq n lt.eq 10^(18)$) —— 数字的上限。 第二行,输出两个整数 $a$ 和 $b$ ($1 lt.eq a, b lt.eq n$) —— Vasya 想的两个数。 第三行,输出一个整数 $m$ ($1 lt.eq m lt.eq 10^5$) —— 交互器的指令数量。 接下来的 $m$ 行,每行输出五个整数:$x_i$、$y_i$、$r^(12)_i$、$r^(13)_i$ 和 $r^(23)_i$ ($1 lt.eq x_i, y_i lt.eq n$),其中 $r^(S T)_i$ 等于数字 $S$ 或 $T$。 当回答查询 $x \; y$ 时,交互器寻找满足 $| x -x_i | + | y -y_i |$ 最小的编号 $i$(从 $1$ 到 $n$)。如果有多个这样的 $i$,则选择最小的 $i$。然后交互器根据该指令回答查询;但如果存在两个可选短语 $S$ 和 $T$,则选择 $r^(S T)_i$。 例如,样例测试数据文件包含以下内容: 让我们分析样例测试。所选数字是 $2$ 和 $4$。交互器被给予两条指令。 对于查询 $(4, 3)$,它可以返回 $2$ 或 $3$。在两条指令中选择第二条,因此交互器返回 $a^(23)_2 = 3$。 对于查询 $(3, 4)$,它只能返回 $3$。 对于查询 $(3, 3)$,它可以返回 $2$ 或 $3$。在两条指令中选择第一条(因为当值相等时优先选择编号最小的),因此交互器返回 $a^(23)_1 = 2$。 对于查询 $(1, 5)$,它可以返回 $1$ 或 $3$。在两条指令中选择第一条,因此交互器返回 $a^(13)_1 = 1$。 在第五次查询 $(2, 4)$ 时,数字被正确猜出,玩家获胜。 ## Input 第一行包含一个整数 $n$ ($1 lt.eq n lt.eq 10^(18)$) —— 数字的上限。 ## Interaction 首先,你需要读取数字 $n$,之后你可以进行查询。要进行一次查询,输出两个整数:$x$ 和 $y$ ($1 lt.eq x, y lt.eq n$),然后 _刷新_ 输出。每次查询后,读取一个整数 $ans$ ($0 lt.eq ans lt.eq 3$)。如果 $ans > 0$,则表示 Vasya 说出的短语编号。如果 $ans = 0$,表示你猜中了,你的程序应终止。如果你进行了超过 $600$ 次查询或进行了无效查询,你将获得 _错误答案_。如果你没有输出任何内容或忘记 _刷新_ 输出,你的程序将获得 _空闲超时_。要在打印查询和换行后 _刷新_ 输出,你需要执行以下操作: _fflush(stdout)_ 在 C++ 中; _System.out.flush()_ 在 Java 中; _stdout.flush()_ 在 Python 中; _flush(output)_ 在 Pascal 中; 其他语言请参阅文档。 *黑客格式* 对于黑客输入,使用以下格式: 第一行,输出一个整数 $n$ ($1 lt.eq n lt.eq 10^(18)$) —— 数字的上限。 第二行,输出两个整数 $a$ 和 $b$ ($1 lt.eq a, b lt.eq n$) —— Vasya 想的两个数。 第三行,输出一个整数 $m$ ($1 lt.eq m lt.eq 10^5$) —— 交互器的指令数量。 接下来的 $m$ 行,每行输出五个整数:$x_i$、$y_i$、$r^(12)_i$、$r^(13)_i$ 和 $r^(23)_i$ ($1 lt.eq x_i, y_i lt.eq n$),其中 $r^(S T)_i$ 等于数字 $S$ 或 $T$。 当回答查询 $x \; y$ 时,交互器寻找满足 $| x -x_i | + | y -y_i |$ 最小的编号 $i$(从 $1$ 到 $n$)。如果有多个这样的 $i$,则选择最小的 $i$。然后交互器根据该指令回答查询;但如果存在两个可选短语 $S$ 和 $T$,则选择 $r^(S T)_i$。 例如,样例测试数据文件包含以下内容: 5 2 4 2 2 5 1 1 2 4 1 2 3 3 [samples] ## Note 让我们分析样例测试。所选数字是 $2$ 和 $4$。交互器被给予两条指令。 对于查询 $(4, 3)$,它可以返回 $2$ 或 $3$。在两条指令中选择第二条,因此交互器返回 $a^(23)_2 = 3$。 对于查询 $(3, 4)$,它只能返回 $3$。 对于查询 $(3, 3)$,它可以返回 $2$ 或 $3$。在两条指令中选择第一条(因为当值相等时优先选择编号最小的),因此交互器返回 $a^(23)_1 = 2$。 对于查询 $(1, 5)$,它可以返回 $1$ 或 $3$。在两条指令中选择第一条,因此交互器返回 $a^(13)_1 = 1$。 在第五次查询 $(2, 4)$ 时,数字被正确猜出,玩家获胜。
**Definitions** Let $ n \in \mathbb{Z} $, $ 1 \leq n \leq 10^{18} $, be the upper bound. Let $ (a, b) \in \{1, \dots, n\}^2 $ be the hidden pair, unknown to the solver. **Query Response Rules** For any query $ (x, y) \in \{1, \dots, n\}^2 $: - If $ x = a $ and $ y = b $, respond with $ 0 $ (win). - Otherwise, respond with $ ans \in \{1, 2, 3\} $, where: - $ ans = 1 $ if $ x = a $ and $ y \neq b $, - $ ans = 2 $ if $ x \neq a $ and $ y = b $, - $ ans = 3 $ if $ x \neq a $ and $ y \neq b $. If multiple conditions hold, any valid $ ans $ may be chosen. **Constraints** - At most $ 600 $ queries allowed. - All queries must satisfy $ 1 \leq x, y \leq n $. **Objective** Determine $ (a, b) $ using at most $ 600 $ adaptive queries, each followed by reading the response $ ans \in \{0, 1, 2, 3\} $, and terminate upon receiving $ ans = 0 $.
Samples
Input #1
5
3
3
2
1
0
Output #1
4 3
3 4
3 3
1 5
2 4
API Response (JSON)
{
  "problem": {
    "name": "E. Guess two numbers",
    "description": {
      "content": "**This is an interactive problem.** Vasya and Vitya play a game. Vasya thought of two integers $a$ and $b$ from $1$ to $n$ and Vitya tries to guess them. Each round he tells Vasya two numbers $x$ and",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF1008E"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "**This is an interactive problem.**\n\nVasya and Vitya play a game. Vasya thought of two integers $a$ and $b$ from $1$ to $n$ and Vitya tries to guess them. Each round he tells Vasya two numbers $x$ and...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "*这是一个交互式问题。*\n\nVasya 和 Vitya 玩一个游戏。Vasya 想了两个整数 $a$ 和 $b$,范围在 $1$ 到 $n$ 之间,Vitya 试图猜出它们。每一轮,他告诉 Vasya 两个数 $x$ 和 $y$,范围也在 $1$ 到 $n$ 之间。如果 $x = a$ 且 $y = b$,则 Vitya 获胜。否则,Vasya 必须说出以下三个短语之一:\n\nVasya 不能说谎,...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z} $, $ 1 \\leq n \\leq 10^{18} $, be the upper bound.  \nLet $ (a, b) \\in \\{1, \\dots, n\\}^2 $ be the hidden pair, unknown to the solver.  \n\n**Query Response Rules**...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments