Timer
Time limit2sMemory limit256 MB
Each use of the exploit rounds the timer up to the next multiple of y; with at most k uses over t seconds, find the largest value reachable.
- Level
Medium7 of 10
- Topics
- Greedy, Math, Implementation, Brute force
- Solved
- No attempts yet
Problem
As punishment for acting up at school, Petya is serving a sentence under house arrest. In his room there is a timer that shows how long Petya has already been serving his punishment. Every second the number on the timer increases by one. Wanting to shorten his punishment, Petya found a vulnerability in the timer. If the timer currently shows seconds, then, using the vulnerability, Petya can make it show seconds, where is the smallest number greater than or equal to that is divisible by a given number .
Petya wants the timer to show as many seconds as possible. However, Petya also does not want to get caught, so he will not use the vulnerability more than times. What is the maximum number on the timer that Petya can get after real seconds? At the moment Petya discovered the vulnerability, the timer showed the number .
For example, suppose that at the moment the vulnerability was discovered the timer showed , , Petya plans to use the vulnerability at most times, and . Then the maximum number Petya can get on the timer is 30, and to do this he must act as follows. Immediately using the vulnerability, he gets the value 10 on the timer. After 1 second the timer will show 11, and by using the vulnerability Petya gets 20 on it. After another second it will be 21, and by using the vulnerability Petya will get the value 30.
Input
The first line contains the integer (), the number of test cases. Each of the following lines contains four integers: , , , and . (All numbers are between 1 and inclusive.)
Output
For each of the test cases, output a single number: the maximum possible number the timer can show after seconds.