#P10396. Robot Memory Grid Challenge

    ID: 12404 Type: Default 1000ms 256MiB

Robot Memory Grid Challenge

Robot Memory Grid Challenge

This is a submission answer problem where you control a robot with a 300-cell memory (indexed from 1 to 300) initially filled with zeros. Each cell holds a 32-bit integer. In the robot's grid, certain special nodes contain instructions. These instructions include the following operations:

  • Increment/Decrement: Increase or decrease the value in a specified memory cell by 1, then move one step in a given direction.
  • Comparison: Compare the values in two memory cells and move one step in a direction depending on the result.
  • Input: The starting node. The robot begins here and immediately moves one step in the specified direction. This node will not be visited again.
  • Output: When the robot reaches this node, it terminates and outputs the value from a specified memory cell.

There must be exactly one input node and one output node.

Your task is to design the instructions for the robot such that it always moves from the unique input node to the output node without visiting any non-special node and uses no more than \(5\times10^7\) moves. In addition, the robot will be used to solve one of the six tasks described below depending on how the memory is initially loaded:

  1. Addition: The initial memory cells at positions 1 and 2 contain \(a\) and \(b\) respectively (\(0\le a,b\le 100\)). Output \(a+b\).
  2. Exponentiation: The initial memory cell at position 1 contains \(a\) (\(0\le a\le 20\)). Output \(2^a\).
  3. Squaring: The initial memory cell at position 1 contains \(a\) (\(1\le a\le 1000\)). Output \(a^2\).
  4. Bitwise XOR: The initial memory cells at positions 1 and 2 contain \(a\) and \(b\) respectively (\(0\le a,b<2^{19}\)). Output \(a\oplus b\) (the bitwise XOR of \(a\) and \(b\)).
  5. Sorting: The initial memory cells at positions 1 to 51 contain \(n=50, a_1, a_2, \dots, a_n\) (\(0\le a_i\le 100\)). Output the sorted sequence of \(a_1, a_2, \dots, a_n\) in ascending order.
  6. Tree Diameter: The initial memory cells at positions 1 to 59 contain \(n=30\) followed by \(u_1, v_1, \dots, u_{n-1}, v_{n-1}\) representing a tree with 30 nodes and 29 edges. Output the length of the diameter of the tree (the number of edges in the longest simple path). It is guaranteed that \(1\le u_i,v_i\le n\).

Your submitted solution will be tested on one of the above tasks. You may determine which task to solve by the number of integers present in the input:

  • If there is only 1 integer, assume it is task 2 if \(a\le20\) and task 3 if \(a>20\).
  • If there are 2 integers, assume task 1 if both are \(\le100\), otherwise assume task 4.
  • If there are 51 integers, solve task 5.
  • If there are 59 integers, solve task 6.

Note: In all cases, the robot’s moves and operations are guaranteed to follow the constraints described.

inputFormat

The input consists of a single line of space-separated integers. The number of integers indicates the task:

  • 1 integer: Task 2 or Task 3.
  • 2 integers: Task 1 or Task 4.
  • 51 integers: The first integer is 50, followed by 50 numbers (Task 5).
  • 59 integers: The first integer is 30, followed by 58 integers representing 29 edges (Task 6).

outputFormat

Output a single line with the answer for the corresponding task.

  • For Task 5, output the sorted numbers separated by a space.
  • For Task 6, output the diameter length of the tree.
  • For the other tasks, output a single integer result.

sample

10 20
30