QOJ.ac

QOJ

実行時間制限: 2 s メモリ制限: 256 MB 満点: 100

#4862. 表达式求值

統計

表达式求值是一个经典问题。在本题中,你只需要对长度小于 $10^5$ 的表达式进行求值。表达式仅包含非负整数(可能带有前导零)以及运算符 ‘+’、‘-’ 和 ‘*’。形式上,它满足以下文法:

:= |+|-

:= |*

:= |

:= 0|1|2|3|4|5|6|7|8|9

例如,013、0213-2132*0213 是合法的,但 -2132 和 32113+-3213 是非法的。

求值结果可能非常大或为负数。为了避免溢出,请输出结果对 $2^{32}$ 取模的值,因为你将使用一台自定义的 32 位机器来对表达式求值。

这台 32 位机器包含 $2^{10}$ 个内存单元,记作 $r[0], r[1], \dots, r[2^{10}-1]$。每个单元都是一个 32 位无符号整数,同时也作为指令使用。机器的输入是上述描述的表达式,机器的输出是该表达式对 $2^{32}$ 取模的值。

在每个周期中,令 $pc = r[0] \pmod{2^{10}}$。机器执行指令 $r[pc]$。考虑周期开始时 $r[pc]$ 的展开式 $r[pc] = a \cdot 2^{30} + b \cdot 2^{20} + c \cdot 2^{10} + d$,其中 $a, b, c, d$ 是小于 $2^{10}$ 的非负整数:

  • 如果 $a = 0$,机器输出 $r[b]$ 的值并停止。
  • 如果 $a = 1$,则:
    • 如果输入中没有剩余字符,将 $r[0]$ 设为 $r[d]$;
    • 否则,将 $r[b]$ 设为输入中下一个字符的 ASCII 码,然后将 $r[0]$ 设为 $r[c]$。
  • 如果 $a = 2$,将 $r[b]$ 设为 $(r[b] + r[c]) \pmod{2^{32}}$,然后将 $r[0]$ 设为 $r[d]$。
  • 如果 $a = 3$,则:
    • 如果 $r[b] = 0$,将 $r[0]$ 设为 $r[c]$ 的值;
    • 否则,将 $r[0]$ 设为 $r[d]$ 的值。

注意,如果某些指令中 $b = 0$,$r[0]$ 可能会被多次赋值。其最终值是周期结束前最后一次被赋的值。

你需要为每个内存单元设置初始值,使得对于约束范围内的任何可能表达式,机器都能在有限个周期后停止,并输出表达式对 $2^{32}$ 取模的结果。

由于测试存在时间限制,在本题中,机器最多可以执行 $10^8$ 个周期。

输入格式

仅一行,包含一个 32 位无符号整数:表达式生成器的种子。 你不需要使用它。它仅供校验器生成表达式使用。

输出格式

在唯一的一行中输出 $2^{10}$ 个 32 位无符号整数:内存单元的初始值。

样例

输入 1

0

输出 1

0 0 ... 0 (1024 zeros)

说明

样例中的输出是错误的。它仅用于解释输出格式。

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.