Time to Rest
InterviewTime limit2sMemory limit1024 MB
Given n penguin groups with start a_i, step b_i, and size c_i, find the total count of penguins whose path passes through point k.
- Level
Easy2 of 10
- Topics
- Math, Implementation
- Solved
- No attempts yet
Problem
Stopping Doctor Octavius Sprut was not easy. But thanks to the courage, bravery and the doctor's super beam, the penguins finally managed it. Now they sit together with the North Wind and discuss the battle.
The skipper noticed that the monster penguins did not move at random but followed a certain algorithm: all penguins were split into groups, and the -th group had penguins. Imagine that all penguins move along a coordinate line. The -th group starts its attack at point and advances per step. So the path of the -th group is the sequence of points . Every one of the penguins in the -th group passes through each point of this sequence. The sequence can be considered infinite.
Secret praised the skipper for this observation and proposed that everyone think about this problem: given a number , how many penguins passed through the point with coordinate during the whole attack?
The penguins and the North Wind have been thinking about it for a long time without success. Help them!
Input
The first line contains two integers , (, ), the number of penguin groups and the coordinate of the point whose count is needed. Each of the next lines contains three integers (): the starting position of the -th group, the length of one jump, and the number of penguins in the group.
Output
Print one line with the number of penguins that passed through the point with coordinate .
Hint
In the first test, the first group passes through the points {1, 2, 3, 4, \ldots} and the second group passes through {1, 3, 5, 7, \ldots}. The first group has 2 penguins and the second has 3, so 5 penguins pass through the point 3 in total. In the second test, both groups pass through the same points, so 2 penguins pass through the point 4 in total.