#P5207. Ancient Computer Communication Simulation
Ancient Computer Communication Simulation
Ancient Computer Communication Simulation
In an ancient computer network, each door of a treasury is controlled by a cluster of computers. The computers are interlinked by data lines for transmitting integer data, although many data lines have been damaged so that only some connections remain. Initially, no data is present on any line. When a computer writes an integer to a data line, that integer appears on the line. Each data line can hold at most one integer at a time; once the integer is read, it disappears and the line becomes empty again.
Every ancient computer is equipped with two storage registers named \(a\) and \(b\), each capable of storing a 32-bit signed integer (from \(-2147483648\) to \(2147483647\)). In each cycle, a computer can execute one instruction from the following set:
mov reg val
: Set registerreg
to the valueval
.add reg val
: Add the valueval
to registerreg
.dec reg val
: Subtract the valueval
from registerreg
.mul reg val
: Multiply registerreg
by the valueval
.div reg val
: Divide registerreg
byval
using truncation toward zero (e.g. \(\frac{-5}{2} = -2\)).and reg val
: Apply bitwise AND on registerreg
withval
.or reg val
: Apply bitwise OR on registerreg
withval
.xor reg val
: Apply bitwise XOR on registerreg
withval
.jmp val
: Jump to the \(val\)th instruction of the program (instructions are numbered starting from 1).jz reg val
: If registerreg
is 0, jump to line \(val\).jnz reg val
: If registerreg
is nonzero, jump to line \(val\).jgz reg val
: If registerreg
is greater than 0, jump to line \(val\).jsz reg val
: If registerreg
is less than 0, jump to line \(val\).read x reg
: Read an integer from computer \(x\) into registerreg
. If the data line has an integer, it is read and then removed. Otherwise, the computer waits for the next cycle. When \(x = 0\), the input is taken from the standard input.write val x
: Write the integer value ofval
to the data line connected toward computer \(x\), only if the line is empty; otherwise, the computer waits for the next cycle. When \(x = 0\), the output is written to the standard output.
A read or write command is only legal if the computer \(x\) is directly connected by a data line to the executing computer, or if \(x = 0\). Each computer has its own independent standard input and output. Furthermore, during each cycle the write commands are executed first, then the read commands, and finally all other instructions. If a computer reading from standard input finds no more available data, it remains in a waiting state forever. When a computer finishes executing its last instruction, it restarts from the first instruction. A computer with no instructions will wait indefinitely.
This problem is based on simulating simplified behavior inspired by the ancient computer network. You do not need to simulate the entire instruction set. Instead, the problem is divided into 5 subtasks, each representing a communication or computation task:
- Subtask 1: Computer 1 receives at most 100 non-negative integers from its standard input, and must echo these integers (in order) to its standard output.
- Subtask 2: Computer 1 receives a non-negative integer \(k\) and must output the \(k\)th Fibonacci number. The Fibonacci sequence is defined as \(F_0 = 0\), \(F_1 = 1\), and \(F_n = F_{n-1} + F_{n-2}\) for \(n \ge 2\). It is guaranteed that \(F_k \le 10^9\).
- Subtask 3: Computer 1 receives at most 100 non-negative integers and needs to transfer them to the output of computer \(n\) (maintaining the original order).
- Subtask 4: Each of computers 1 through 50 receives one integer. These 50 numbers must be output by computers 51 through 100. The output order is arbitrary.
- Subtask 5: Each of computers 1 through 10 receives one integer, and these numbers must be delivered so that the number read by computer \(i\) is output by computer \(101-i\) (i.e. the order is reversed).
The input begins with an integer indicating the subtask number (from 1 to 5), followed by the data corresponding to that subtask.
inputFormat
The first line of input contains an integer \(T\) (1 ≤ T ≤ 5) specifying the subtask number. The remaining input depends on the subtask:
- Subtask 1 & 3: A sequence of non-negative integers.
- Subtask 2: A single non-negative integer \(k\).
- Subtask 4: 50 non-negative integers.
- Subtask 5: 10 non-negative integers.
outputFormat
Output requirements for each subtask:
- Subtask 1 & 3: Echo the input integers in the same order.
- Subtask 2: Output the \(k\)th Fibonacci number.
- Subtask 4: Output the 50 numbers (order may be arbitrary; here, the input order is acceptable).
- Subtask 5: Output the 10 integers in reverse order.
sample
1
3 5 7
3 5 7