Being Solarly Systematic

Simulate planetoids moving on a wrapping 3D grid and merging on shared cubes until no more collisions occur, then report the final planets.

Medium7SimulationNumber theoryMathNo attempts yetTime limit1sMemory limit256 MB

Problem

Professor Braino Mars studies how solar systems form, and he runs every simulation by hand. He has asked you to automate one of them.

The simulation models how small planetoids collide over time to form larger planets. The space is divided into an nx×ny×nzn_x \times n_y \times n_z grid of cubes, and each cube holds at most one planetoid. Every planetoid has a mass mm, an initial location (x,y,z)(x, y, z), and a velocity (vx,vy,vz)(v_x, v_y, v_z) giving the number of cubes it travels through in each dimension per second. For example, a planetoid that starts at (1,3,2)(1, 3, 2) with velocity (3,1,2)(3, -1, 2) is at (4,2,4)(4, 2, 4) after one second and at (7,1,6)(7, 1, 6) after two seconds.

Paths wrap around in every dimension. If that planetoid lives in an 8×8×88 \times 8 \times 8 space, its next two locations are (2,0,0)(2, 0, 0) and (5,7,2)(5, 7, 2). Cube indices start at 0.

When two or more planetoids share a cube, they form one larger planetoid. Its mass is the sum of the colliding masses. Each component of its velocity is the sum of that component over the colliding planetoids, divided by the number that collided, truncated toward zero. A planetoid of mass 12 with velocity (5,3,2)(5, 3, -2) that collides with one of mass 10 and velocity (8,6,1)(8, -6, 1) gives mass 22 and velocity (6,1,0)(6, -1, 0).

Collisions are considered only at integer time steps. Once no more collisions can happen, the remaining planetoids count as planets.

Input

The first line has four positive integers nn, nxn_x, nyn_y, nzn_z. Here n100n \le 100 is the number of planetoids, and nxn_x, nyn_y, nzn_z are the dimensions of the space, each at most 1000.

Each of the next nn lines has the form m x y z vx vy vzm\ x\ y\ z\ v_x\ v_y\ v_z and gives the mass, location, and velocity of one planetoid at time t=0t = 0, where 1m1001 \le m \le 100, 0x<nx0 \le x < n_x, 0y<ny0 \le y < n_y, 0z<nz0 \le z < n_z, and 1000vx,vy,vz1000-1000 \le v_x, v_y, v_z \le 1000. No two planetoids start in the same location.

Output

Print the number of planets pp that remain once no more collisions can occur. Then print pp lines, one per planet, in the form Pi: m x y z vx vy vz. Here Pi is the identifier with the index ii appended, so the first line starts with P0: and ii runs from 00 to p1p-1. The rest of the line is the mass, location, and velocity of that planet.

Report locations and velocities at the time the last collision occurred. If no collision occurs, report them at time t=0t = 0.

Order the planets from largest mass to smallest. Break ties by the lexicographic order of the location (x,y,z)(x, y, z), smallest xx first.