Catapult-Carousel
Time limit1sMemory limit1024 MB
Given N seats and S affine jumps, find where seat U ends up after K rides, where K can be negative.
- Level
Medium6 of 10
- Topics
- Math, Number theory
- Solved
- No attempts yet
Problem
Catapult-Carousel is a new experimental fairground attraction that John invented. It is a rotating wheel with seats spaced at equal distances. The seats are numbered in clockwise order. Each seat has a mechanism that launches the rider into the air in a controlled manner, so that the rider lands on a predetermined seat.
An -jump is an operating mode of the carousel. In this mode, all riders are launched simultaneously. The rider launched from seat lands on seat , and each subsequent rider (from seats ) lands positions clockwise from the previous landing position. John designed the carousel so that no two riders land on the same seat in any allowed jump mode.
One ride consists of several jumps. The number and parameters of the jumps are the same in every ride. Riders do not change seats between jumps, even when they take several rides in a row. John sells multiple-ride tickets that specify the initial seat and the number of rides. He wants to check that a rider is in the expected seat after completing all the rides.
Write a program that takes the seat number of a rider and computes their seat after rides. If is negative, the program computes the seat they were on rides ago.
Input
The first line contains , the number of seats (), and , the number of jumps in a ride ().
Each of the following lines contains the parameters and of one jump (, , ).
The last line contains , the seat number of the rider (), and , the number of rides ().
Output
The only line contains one integer: the seat the rider is on after rides. If is negative, print the seat the rider was on rides ago.