File size: 3,629 Bytes
1f3a244 | 1 2 3 4 5 | {"type": "session", "harness": "opencode", "id": "cf-1010-B", "name": "Codeforces: Rocket"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Rocket (Rating: 1800)\nTags: binary search, interactive\n\nThis 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 find something to occupy herself. She couldn't think of anything better to do than to calculate the distance to the red planet.\n\nLet'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.\n\nNatasha 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$.\n\nIn 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$.\n\nYou can ask the rocket no more than $60$ questions.\n\nHelp Natasha find the distance to Mars. Assume, that the distance to Mars does not change while Natasha is asking questions.\n\nYour 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).\n\nInput: 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$.\n\nExamples:\nInput: 5 2\n1\n-1\n-1\n1\n0\nOutput: 1\n2\n4\n5\n3\n"}}
{"type": "message", "message": {"role": "assistant", "content": "First we learn the sequence $$$p[]$$$. For this print the query \"1\" $$$n$$$ times. If the answer is \"0\" (that is, the distance to Mars is equal to one), then immediately terminate the program. Otherwise, it is clear that the correct answer is \"1\" (that is, the distance to Mars is greater than one). If $$$i$$$-th answer of rocket is \"1\", then $$$p[i]=1$$$ (that is, the rocket answered the truth), otherwise $$$p[i]=0$$$ (untruth). On this, we will spend no more than $$$n$$$ queries, within the given constraints it is $$$30$$$.\nNow you can find the number $$$x$$$ using binary search. For each answer, you need to check: if the corresponding element of the sequence $$$p$$$ equals to $$$0$$$, then the answer sign must be changed. On this, we will spend no more than $$$\\lceil\\log_2 m\\rceil$$$ queries, within the given constraints it is $$$30$$$.\nThe total number of queries does not exceed $$$n+\\lceil\\log_2 m\\rceil\\le 30+30=60$$$.\nComplexity: $$$O(n+\\log m)$$$\nBonus. Try to solve a similar problem but with a constraint $$$1 \\le m \\le 1073741853$$$."}}
|