Hiding Acorns
Time limit1sMemory limit128 MB
Given K arithmetic-progression rules marking boxes, find the box number where the D-th acorn is placed, counting boxes in increasing order.
- Level
Medium6 of 10
- Topics
- Binary search, Prefix sum, Math
- Solved
- No attempts yet
Problem
Suhyeong won acorns as the first prize at the HEPC contest. To keep the other squirrels from taking them, he hides every acorn in boxes numbered 1 through .
There are too many boxes for him to remember which ones hold acorns, so he writes rules instead. A rule is three numbers , , , and it means put one acorn into box , box , box , and so on, using every such box that does not go past box . He is afraid that one rule gives away everything, so he writes of them.
For example, take a rule that runs from box 100 to box 150 in steps of 10 and a rule that runs from box 110 to box 150 in steps of 15. Acorns then go into boxes 100, 110, 120, 125, 130, 140 and 150, and boxes 110 and 140 hold two acorns each.
A box holds any number of acorns. Suhyeong fills the positions marked by the rules one acorn at a time, in increasing order of box number, until all acorns are placed. Print the number of the box that receives the last acorn.
Input
The first line contains the number of boxes , the number of rules and the number of acorns , separated by spaces. (, , )
Each of the next lines contains three integers , and describing one rule, separated by spaces. ()
is at most the total number of positions marked by the rules.
Output
Print the number of the box that receives the last acorn.