QOJ.ac

QOJ

Time Limit: 0.1 s Memory Limit: 64 MB Total points: 100 Difficulty: [show]

#287. 解密

Statistics

有传言称科学委员会正在使用一种特殊设备来加密他们的通信。如果你能破解这种加密,你就能窃听他们准备的题目并获得高分。昨晚你很幸运:其中一名成员把设备忘在了酒吧里。你打开它并观察了其总体设计:

所有操作均使用 8 位。XOR 是按位异或函数(C 语言中的 ^ 运算符,Pascal 中的 xor 运算符)。

$R$ 是一个伪随机数序列,定义如下:

  • $R[0]$、$R[1]$ 和 $R[2]$ 是只有科学委员会才知道的秘密值。
  • 对于 $n = 3, 4, \dots$,令 $R[n] = R[n-2] \text{ XOR } R[n-3]$。

此外,我们有一个函数 $M: [0..255] \to [0..255]$。实际上,$M$ 是一个双射,即当 $x \neq y$ 时,必有 $M[x] \neq M[y]$。

科学委员会使用的设备每次接收一个数字,并将其加密后输出。在使用该设备 $N$ 次后,下一个数字 INPUT 的加密方式如下:

  • $\text{OUTPUT} = M(\text{INPUT XOR } R[N])$

尽管你了解加密设备的工作原理,但你不知道秘密值 $R[0]$、$R[1]$ 和 $R[2]$。此外,你也不知道 $M$,因此无法解码通信。你能做的是操作你找到的设备。你可以输入数字并观察输出。

题目描述

你的任务是在少于 320 次查询(输入数字)的情况下,找出所有的秘密值:$R[0], R[1], R[2], M[0], M[1], \dots, M[255]$。

数据范围

  • 只有当查询次数少于 320 时,正确的解法才能得分。
  • 在所有测试中,秘密密钥 $R[0], R[1], R[2], M[0], M[1], \dots, M[255]$ 均为随机生成。
  • $0 \le \text{INPUT}, \text{OUTPUT}, R, M \le 255$

交互

这是一个交互式题目。要与加密模块交互,只需向标准输出写入一个 0 到 255 之间的数字。然后,你可以从标准输入读取加密设备的输出,这也是一个 0 到 255 之间的整数。当你确定了所有秘密值后,输出一行包含字符串 SOLUTION 的内容,随后输出 259 行:$R[0], R[1], R[2], M[0], M[1], \dots, M[255]$,每行一个值。

实现细节

在向标准输出写入每一行完整内容后,C 程序员必须使用 fflush(stdout) 函数,而 Pascal 程序员必须使用 flush(output) 过程。

C C++ Pascal
printf("%d\n", q);
fflush(stdout);
cout<<q<< '\n';
cout.flush();
writeln(q);
flush(output);

样例

假设 $R[0] = 0, R[1] = 1, R[2] = 3$,且 $M[i] = (i + 1) \pmod{256}$。 由此我们推导出 $R[3] = 1$。

输入格式 1

11
12
9
14

输出格式 1

10
10
11
12
SOLUTION
0
1
3
1
2
3
4
...
254
255
0

说明

表格中的每一行展示了选手与设备的交互过程。例如,第一行表示选手输入 10,设备输出 11,因为 $M[10 \text{ XOR } 0] = M[10] = 11$。最后输出 SOLUTION 后,按顺序输出所有秘密值。

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.