Hyper Dice Rolling
Time limit2.5sMemory limit1024 MB
Track the die center through M rolls on an N-dimensional die and print its squared distance from the origin.
- Level
Medium7 of 10
- Topics
- Simulation, Math, Implementation
- Solved
- No attempts yet
Problem
In -dimensional Euclidean space, an -dimensional Cartesian coordinate system is defined. It has coordinate axes through the origin. In this problem, the -th axis is called the axis. The direction in which increases is the direction, and the direction in which decreases is the direction.
All points with are filled. For convenience, the part with is called the floor, and the hyperplane is called the floor surface. A die of size is centered at the origin. That is, the die occupies . The die has faces, each an -dimensional figure. For each integer with , the face in the direction is labeled , and the face in the direction is labeled .
You are given commands. After performing all commands in order, find the squared Euclidean distance between the origin and the center of the die. Each command is an integer with . It means to roll the die so that the face labeled lies on the floor surface. It is guaranteed that is not labeled on the or face before the roll.
Input
The first line contains two positive integers and . is the number of dimensions of the die, and is the number of rolling commands. (, )
The next line contains positive integers . is the integer for the -th command. ()
Output
On the first line, print the squared Euclidean distance between the origin and the center of the die after performing all commands in order, as an integer.
Hint
The squared Euclidean distance between two points and in -dimensional Euclidean space is
The exact definition of rolling the die is as follows. Let be the face of the die labeled , and let be the face of the die touching the floor. The intersection of and is an -dimensional figure. Rolling the die so that lies on the floor surface means rotating the die 90 degrees clockwise or counterclockwise about . The rotation direction must be one in which the die does not go into the floor.
Rotating the die by degrees about the -dimensional figure is defined as follows. Let be any point inside the die. Let be the space that passes through and is linearly independent of . Since is -dimensional, is always a 2-dimensional infinite plane. Let be the intersection of and . Since and are linearly independent, is a single point. Within the plane , rotate by degrees about . Doing this for every is defined as rotating the die by degrees.