This page is still under construction.

Parts of this page are still being built. What you see may change.

Rocket

Time limit1sMemory limit1024 MB

Summary
Find, for each rocket, the minimum fuel so its total climb with velocity floor(K/(M+T))-g reaches at least the target height H.
Level

Hard8 of 10

Topics
Binary search, Math, Simulation
Solved
No attempts yet

Problem

On the planet Diskretigravija, whose gravity behaves unlike the usual, the inhabitants tune and test how efficient their rockets are. They have built NN rockets and want each one to reach a given height while spending as little fuel as possible.

A rocket behaves as follows. As long as it still has fuel, every second it ejects exactly one unit of fuel and its vertical velocity changes by

⌊KM+T⌋−g\left\lfloor \frac{K}{M+T} \right\rfloor - g

where

  • KK is that rocket's fuel quality,
  • MM is the rocket's mass without fuel,
  • TT is the amount of fuel left immediately after this one unit was ejected,
  • gg is the planet's free-fall acceleration,
  • ⌊x⌋\lfloor x \rfloor is the floor of xx. A negative velocity means the rocket is falling.

Each second the rocket moves by its current velocity, so its height changes by the current velocity every second. Once the fuel runs out, the velocity decreases by gg every second. The height a rocket reaches is the highest point of its flight.

For example, take K=19K = 19, g=2g = 2, M=3M = 3 and a rocket that starts with 33 units of fuel. At the start of the first second it ejects the first unit and for exactly one second rises with velocity ⌊193+2⌋−2=1\left\lfloor \frac{19}{3+2} \right\rfloor - 2 = 1. After that the velocity grows by ⌊193+1⌋−2=2\left\lfloor \frac{19}{3+1} \right\rfloor - 2 = 2 to 33 units per second, and after burning the last unit of fuel it grows by ⌊193+0⌋−2=4\left\lfloor \frac{19}{3+0} \right\rfloor - 2 = 4 to 77 units per second. With the fuel gone, the velocity now drops by 22 each second, so in total the rocket climbs to a height of 1+3+7+5+3+1=201 + 3 + 7 + 5 + 3 + 1 = 20.

Help the testers determine the least amount of fuel each rocket needs in order to reach its desired height.

Input

The first line contains two integers: the number of rockets NN and the planet's free-fall acceleration gg.

Each of the next NN lines describes one rocket. Line i+1i+1 contains three integers KiK_i, MiM_i and HiH_i — the fuel quality, the mass, and the height that rocket ii must reach.

Output

Output NN lines, one integer each. On line ii print the smallest amount of fuel with which rocket ii can rise to a height of at least HiH_i, or −1-1 if that is impossible.

Constraints

  • 1≤g1 \le g
  • 1≤Mi≤Ki≤1081 \le M_i \le K_i \le 10^8
  • 1≤Hi≤10181 \le H_i \le 10^{18}
  • 1≤N≤2001 \le N \le 200

Examples4

  1. Example 1

    Input
    2 2
    19 3 20
    19 3 28
    
    Expected output
    3
    -1
    
  2. Example 2

    Input
    1 2
    19 3 20
    
    Expected output
    3
    
  3. Example 3

    Input
    1 2
    19 3 27
    
    Expected output
    4
    
  4. Example 4

    Input
    3 1
    100 1 1
    5 5 1
    1000000 1 1
    
    Expected output
    1
    -1
    1