P4368 [Code+#4] Meow

Description

There is a neat row of $n$ cherry trees along the roadside, each exactly of height $h$. One day, a cute kitten happened to climb onto the trees to play, but she is a bit allergic to cherry blossoms, so she now wants to leave these cherry trees as soon as possible. Specifically, the kitten is currently at height $y$ on the $x$-th cherry tree. She can leave the cherry trees by using two types of jumps: Light jump: Move $a$ trees to the left or right while decreasing the height by $b$. That is, in one light jump, she can move from the starting point to height $y-b$ on the $(x+a)$-th or $(x-a)$-th tree. Note that when the kitten’s current height is not greater than $b$, a light jump cannot be used. Strong jump: Move $a$ trees to the left or right while increasing the height by $b$. That is, in one strong jump, she can move from the starting point to height $y+b$ on the $(x+a)$-th or $(x-a)$-th tree. Note that when the kitten’s current height is greater than $h-b$, a strong jump cannot be used. To leave this stretch of cherry trees, the kitten needs to move to either the $1$-st or the $n$-th tree, at height $h$ or $1$ (so that she can breathe fresh air or just walk away from the trees). Now she wants to know the minimum number of jumps required to achieve the goal. Since she is a cute kitten, she hopes you will tell her.

Input Format

Read from standard input. The first line contains a positive integer $T$, the number of groups of testdata. There are no blank lines between groups. Then follow $T$ lines, each describing one group of data: Each group consists of one line containing six positive integers $n,h,x,y,a,b$, whose meanings are as described in the problem statement.

Output Format

Output to standard output. The output consists of $T$ groups of results, with no blank lines between groups. For each group of data: If the kitten can successfully leave the cherry trees, output the minimum number of jumps she needs. If the kitten cannot leave the cherry trees using the given jump methods, please output -1.

Explanation/Hint

| Test point ID | Range of $n,h$ | Special note | | :----------: | :----------: | :----------: | | $1,2$ | $\le 10$ | $b=1$ | | $3,4$ | $\le 10$ | $a=b=1$ | | $5,6,7,8$ | $\le 10$ | None | | $9,10$ | $\le 500$ | $b=1$ | | $11,12$ | $\le 500$ | $a=b=1$ | | $13,14,15$ | $\le 500$ | None | | $16,17$ | $\le 10^5$ | $h \le 10$ | | $18,19,20$ | $\le 10^5$ | None | | $21,22$ | $\le 10^{15}$ | $h \le 10$ | | $23,24,25$ | $\le 10^{15}$ | None | For all testdata, it is guaranteed that $n \ge 2$, $a < n$, $b \le h$. Credit: https://www.luogu.org/discuss/show/38908 Translated by ChatGPT 5