QOJ.ac

QOJ

実行時間制限: 0.5 s メモリ制限: 1024 MB 満点: 100 コミュニケーション

#16236. 双向通信

統計

这是一个通信问题。

两个程序 Alice 和 Bob 玩以下合作游戏。

给你两个整数 $A$ 和 $B$,满足 $0 \le A, B < 2^{64}$。起初,只有 Alice 知道 $A$,只有 Bob 知道 $B$。设 $C := \min(A, B)$。目标是让 Alice 和 Bob 都能输出 $C$。

为了交换信息,Alice 和 Bob 可以调用以下函数:

  • send(x):向另一个程序发送一个 1 位(1-bit)的值 $x$。该调用在另一个程序调用 receive() 之前不会返回。
  • receive():从另一个程序接收一个 1 位的值并返回它。该调用在另一个程序调用 send() 之前不会返回。

Alice 和 Bob 调用的 send() 总次数之和不能超过 76。对于 receive() 也是如此。

通信结束后,每个程序必须恰好调用一次以下函数:

  • answer(x):声明 $C$ 的值为 $x$。调用此函数后,程序必须立即终止。

如果 Alice 和 Bob 都正确回答了 $C$,则游戏成功。

编写两个程序 Alice 和 Bob,使它们在游戏中总是成功。

交互

这是一个交互式通信问题(你的程序与评测机通过标准输入/输出进行通信)。你扮演 Alice 的程序和扮演 Bob 的程序将通过评测机进行游戏。

程序启动

你的程序首先接收以下格式的输入:

Player
X

这里,Player 是字符串 AliceBob,而 $X$ 是一个满足 $0 \le X < 2^{64}$ 的整数。

  • 如果 PlayerAlice,则 $A = X$。从此时起,你的程序必须表现为 Alice。
  • 如果 PlayerBob,则 $B = X$。从此时起,你的程序必须表现为 Bob。

之后,按照下文所述调用 sendreceive,并最终调用 answer

send

要调用 send(x),输出:

send x

这里,$x$ 必须是 0 或 1。

该调用在另一个程序调用 receive() 之前不会返回。如果发生以下任何一种情况,你将被判定为 Wrong Answer:

  • 另一个程序在你等待时终止,
  • 另一个程序在你等待时也调用了 send()
  • Alice 和 Bob 调用的 send() 总次数超过 76。

receive

要调用 receive(),输出:

receive

然后从标准输入读取接收到的位 $x$:

x

该调用在另一个程序调用 send() 之前不会返回。如果发生以下任何一种情况,你将被判定为 Wrong Answer:

  • 另一个程序在你等待时终止,
  • 另一个程序在你等待时也调用了 receive()
  • Alice 和 Bob 调用的 receive() 总次数超过 76。

如果你在交互过程中被判定为 Wrong Answer,提供的输入将为 -1。如果你读取到 -1,请立即终止程序,否则可能会获得 Time Limit Exceeded。

answer

要调用 answer(x),输出:

answer x

这里,$x$ 必须等于 $\min(A, B)$。调用此函数后,立即终止程序。

说明

  • 每次输出后,你的程序必须刷新标准输出;否则,你可能会获得 Time Limit Exceeded。
  • 评测程序、你扮演 Alice 的程序以及你扮演 Bob 的程序是同时运行的。时间限制和内存限制是这三者之和,因此请在时间和内存使用上留出足够的余量。

    • 评测程序最多消耗约 50 ms 和 5 MiB。
  • 你需要处理 $0$ 到 $2^{64} - 1$ 范围内的整数。请注意溢出。

样例

输入样例 1

Alice
31
1

输出样例 1

send 0
receive
send 1
answer 25

输入样例 2

Bob
25
0
1

输出样例 2

receive
send 1
receive
answer 25

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.