CF BUDDY
← Problems·

1902B · Getting Points

1100 · binary search, brute force, greedy

Problem: Monocarp is a student at Berland State University. Due to recent changes in the Berland education system, Monocarp has to study only one subject — programming.

The academic term consists of nn days, and in order not to get expelled, Monocarp has to earn at least PP points during those nn days. There are two ways to earn points — completing practical tasks and attending lessons. For each practical task Monocarp fulfills, he earns tt points, and for each lesson he attends, he earns ll points.

Practical tasks are unlocked "each week" as the term goes on: the first task is unlocked on day 11 (and can be completed on any day from 11 to nn), the second task is unlocked on day 88 (and can be completed on any day from 88 to nn), the third task is unlocked on day 1515, and so on.

Every day from 11 to nn, there is a lesson which can be attended by Monocarp. And every day, Monocarp chooses whether to study or to rest the whole day. When Monocarp decides to study, he attends a lesson and can complete no more than 22 tasks, which are already unlocked and not completed yet. If Monocarp rests the whole day, he skips a lesson and ignores tasks.

Monocarp wants to have as many days off as possible, i. e. he wants to maximize the number of days he rests. Help him calculate the maximum number of days he can rest!

Input Format: The first line contains a single integer tctc (1tc1041 \le tc \le 10^4) — the number of test cases. The description of the test cases follows.

The only line of each test case contains four integers nn, PP, ll and tt (1n,l,t1091 \le n, l, t \le 10^9; 1P10181 \le P \le 10^{18}) — the number of days, the minimum total points Monocarp has to earn, the points for attending one lesson and points for completing one task.

It's guaranteed for each test case that it's possible not to be expelled if Monocarp will attend all lessons and will complete all tasks.

Output Format: For each test, print one integer — the maximum number of days Monocarp can rest without being expelled from University.

Note: In the first test case, the term lasts for 11 day, so Monocarp should attend at day 11. Since attending one lesson already gives 55 points (5P5 \ge P), so it doesn't matter, will Monocarp complete the task or not.

In the second test case, Monocarp can, for example, study at days 88 and 99: at day 88 he will attend a lesson for 10910^9 points and complete two tasks for another 5108+51085 \cdot 10^8 + 5 \cdot 10^8 points. And at day 99 he only attends a lesson for another 10910^9 points.

In the third test case, Monocarp can, for example, study at day 4242: attending a lesson gives him 11 point and solving 22 out of 66 available tasks gives him another 2102 \cdot 10 points.

In the fourth test case, Monocarp has to attend all lessons and complete all tasks to get 810+220=1208 \cdot 10 + 2 \cdot 20 = 120 points.

In the fifth test case, Monocarp can, for example, study at days: 88 — one lesson and first and second tasks; 1515 — one lesson and the third task; 2222 — one lesson and the fourth task; 2929 — one lesson and the fifth task; 3636 — one lesson and the sixth task.

Sample Cases

Case 1

Input

5
1 5 5 2
14 3000000000 1000000000 500000000
100 20 1 10
8 120 10 20
42 280 13 37

Output

0
12
99
0
37

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback