The Loathesome Hay Baler
InterviewTime limit1sMemory limit128 MB
Rollers touch when center distance equals the sum of radii; find the path from the drive roller to the take-off roller and sum the absolute speeds, truncated.
Problem
A hay baler is powered by rollers (). Instead of a single drive shaft, the rollers press against one another, so a spinning roller turns every roller it touches.
Each roller is centered at () and has radius (). Two rollers touch, and therefore transmit power, exactly when the distance between their centers equals the sum of their radii. All coordinates and radii are integers, so this test is exact.
The drive-roller is the roller centered at . It turns clockwise at 10000 revolutions per hour (rph). The power take-off is the roller centered at .
When a roller of radius spinning at rph drives a roller of radius , the driven roller turns at rph. The sign flips because two meshing rollers spin in opposite directions.
Every roller other than the drive-roller is driven by exactly one other roller, so the contacts between rollers form a tree rooted at the drive-roller, and there is a single path of rollers from the drive-roller to the power take-off. Only the rollers on that path (the drive-roller, the take-off roller, and every roller between them) belong to the power-train; all other rollers are ignored.
Compute the sum of the absolute values of the speeds (in rph) of every roller on the power-train, then output that sum truncated to an integer.
Input
- Line 1: three space-separated integers , , .
- Lines 2 to : line contains three space-separated integers , , describing roller .
Output
- A single line containing one integer: the sum of the absolute values of the speeds of all rollers on the power-train (the drive-roller, every driven roller along the way, and the power take-off roller), truncated to an integer.
Hint
Because each driven roller multiplies the previous speed by , the magnitudes telescope: a roller of radius on the path spins with magnitude rph. For example, a drive-roller of radius 10 driving a radius-20 roller, which in turn drives another radius-20 roller, produces speeds of 10000, -5000, and 5000 rph, whose absolute values sum to 20000.