Belts
InterviewTime limit1sMemory limit256 MB
Given each belt rotation ratio and twist, compute the last wheel speed and direction when the first wheel turns clockwise once per minute.
- Level
Easy2 of 10
- Topics
- Simulation, Math
- Solved
- No attempts yet
Problem
A science kit lets you run experiments with wheels and belts. The kit holds wheels of several sizes, a set of belts, and a board with sockets for the wheels.
The board has one row of axles, and each axle takes a single wheel. Neighboring axles sit far enough apart that no two wheels touch, even when every axle is filled. The wheels are numbered 1, 2, and so on from the left.
Two wheels that sit next to each other are always joined by exactly one belt. You can loop a belt without twisting it, in an O shape, or twist it once, in a figure-eight shape. Turning one wheel turns the wheel joined to it, and the two rotation counts differ when the wheels have different sizes. Wheels joined by an untwisted belt turn in the same direction, and wheels joined by a twisted belt turn in opposite directions.
Consider four wheels and three belts given as follows. The shape of a belt is written as for the untwisted O shape and for the twisted figure-eight shape.

Belt 1 joins wheel 1 and wheel 2 in the O shape, and wheel 2 turns twice while wheel 1 turns once. Turning wheel 1 one full turn clockwise turns wheel 2 two full turns clockwise.
Belt 2 joins wheel 2 and wheel 3 in the figure-eight shape, and wheel 3 turns five times while wheel 2 turns once. Turning wheel 2 one full turn clockwise turns wheel 3 five full turns counterclockwise.
Belt 3 joins wheel 3 and wheel 4 in the O shape, and wheel 4 turns once while wheel 3 turns twice. Turning wheel 3 two full turns clockwise turns wheel 4 one full turn clockwise.
So turning wheel 1 one full turn clockwise makes wheel 4 turn five full turns counterclockwise, through wheel 2 and wheel 3.
The rotation ratio and the shape of every belt are given in order. Write a program that prints the rotation direction and the rotations per minute of the last wheel when the first wheel turns clockwise at one rotation per minute.
Input
The first line contains the number of belts (). Each of the next lines describes one belt, starting from belt 1. The -th of those lines contains three integers , , and separated by spaces. Belt joins wheel and wheel , wheel turns times while wheel turns times, and is the shape of the belt: for the untwisted O shape and for the twisted figure-eight shape.
and are positive integers not greater than , and they are coprime. The input is such that the rotations per minute of every wheel is a positive integer not greater than .
Output
Print the rotation direction and the rotations per minute of wheel on one line, separated by a space. The direction is for clockwise and for counterclockwise.