QOJ.ac

QOJ

Limite de temps : 3.0 s Limite de mémoire : 512 MB Points totaux : 100 Hackable ✓

#8611. (A + B) mod P

Statistiques

AGI(通用人工智能)指日可待,但它仍然无法计算两个整数模素数的和。在这个任务中,你可以通过训练一个具有 $n$ 个神经元的单层感知机模型来加速 AGI 的革命,该模型针对固定的素数 $p$。该模型以两个整数 $a$ 和 $b$ 作为输入,并计算 $(a+b) \pmod p$。该模型的架构为:

$$ReLU(a_{one-hot}W_{input} + b_{one-hot}W_{input})W_{output}^T$$

其中

$$ReLU(x) = max(x, 0)$$

且 $x_{one-hot}$ 是一个大小为 $1 \times p$ 的矩阵,它由全零组成,仅在第 $x$ 列有一个 1。 例如,如果 $p = 4$,$0_{one-hot} = (1 \quad 0 \quad 0 \quad 0)$,$2_{one-hot} = (0 \quad 0 \quad 1 \quad 0)$。 换句话说,你必须选择神经元的数量 $n$,并生成两个大小为 $p \times n$ 的实数矩阵 $W_{input}$ 和 $W_{output}$。

执行加法运算 $a$ 和 $b$ 分为三个步骤: 1. 计算 $W_{input}$ 中索引为 $a$ 和 $b$ 的两行的逐点和。 2. 将该和中所有负数替换为零。称生成的行为 $M$。 3. 整数 $i$ 的得分定义为 $M$ 与 $W_{output}$ 中第 $i$ 行的标量积。得分最高的整数应等于 $(a + b) \pmod p$。

请参阅样例说明以获得更好的理解。 你需要生成两个实数矩阵 $W_{input}$ 和 $W_{output}$,使得对于所有满足 $0 \le a, b < p$ 的数对 $(a, b)$,模型产生的输出都是正确的。

输入格式

输入仅包含一行,为一个整数 $p$ ($3 \le p \le 100$)。保证 $p$ 是素数。

输出格式

第一行应包含一个整数 $n$ ($1 \le n \le 25$),即模型中神经元的数量。 接下来的 $p$ 行,输出矩阵 $W_{input}$。每一行应包含 $n$ 个绝对值不超过 1000 的实数。 之后,以相同的格式输出矩阵 $W_{output}$。

样例

样例输入 1

3

样例输出 1

4
2.5 3.0 -0.5 -3.0
-3.0 0.0 0.5 2.0
-0.5 0.5 -3.0 1.5
1.5 1.0 -2.0 2.5
-3.0 3.0 -1.0 2.0
-0.5 2.5 0.5 2.0

说明

让我们计算 $(1 + 2) \pmod 3$。首先,我们将行 $[-3.0, 0.0, 0.5, 2.0]$ 和 $[-0.5, 0.5, -3.0, 1.5]$ 相加,得到 $[-3.5, 0.5, -2.5, 3.5]$。然后,我们移除所有负数:$[0.0, 0.5, 0.0, 3.5]$。

然后,我们计算每个可能结果的得分:

  • 0: $[0.0, 0.5, 0.0, 3.5] \times [1.5, 1.0, -2.0, 2.5] = 0.0 \cdot 1.5 + 0.5 \cdot 1.0 + 0.0 \cdot -2.0 + 3.5 \cdot 2.5 = 9.25$。
  • 1: $[0.0, 0.5, 0.0, 3.5] \times [-3.0, 3.0, -1.0, 2.0] = 8.5$。
  • 2: $[0.0, 0.5, 0.0, 3.5] \times [-0.5, 2.5, 0.5, 2.0] = 8.25$。

第 0 行得分最高,为 9.25,且 $(1 + 2) \pmod 3$ 的确等于 0。

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.