luogu#P4774. [NOI2018] 屠龙勇士
[NOI2018] 屠龙勇士
Problem Description
Xiao D recently found a mini game online. The rules are as follows:
- The goal of the game is to kill dragons in order . Each dragon has an initial health value . At the same time, each dragon has a recovery ability: when it uses recovery, its health increases by each time until its health becomes non-negative. The dragon will only die when the attack ends and its health is exactly .
- At the start of the game, the player has swords with known attack power. Each time facing a dragon, the player can only choose one sword. After killing the dragon, that sword disappears, but as a reward, the player obtains a brand-new sword.
Xiao D thinks this game is very boring, but the fastest player to clear it can get a qualification for ION2018, so Xiao D decides to write a simple robot to help her clear the game. Her robot follows these rules:
- Each time it faces a dragon, the robot chooses, among the swords it currently has, the one with the maximum attack power that does not exceed the dragon’s initial health as its weapon. If there is no such sword, it chooses the sword with the minimum attack power as its weapon.
- For each dragon, the robot uses the sword chosen in the previous step to attack the dragon a fixed times, reducing the dragon’s health by .
- Then, the dragon will keep using its recovery ability, recovering health each time. If before using recovery, or after some recovery, its health becomes , then the dragon dies and the player clears this stage.
So obviously, the number of attacks is the key to whether the game can be cleared as fast as possible. Xiao D now knows all attributes of each dragon and wants to test you: what value should the robot’s attack count be set to, so that the game can be cleared with the minimum number of attacks?
Of course, if the game cannot be cleared no matter what it is set to, output .
Input Format
The first line contains an integer , the number of test cases.
Then there are test cases, each containing lines.
- The first line of each test case contains two integers and , representing the number of dragons and the number of initial swords;
- The next line contains positive integers; the -th number is the initial health of the -th dragon;
- The next line contains positive integers; the -th number is the recovery value of the -th dragon;
- The next line contains positive integers; the -th number is the attack power of the sword rewarded after killing the -th dragon;
- The next line contains positive integers, the attack powers of the initial swords.
Output Format
Output lines in total.
The -th line contains one integer, indicating for the -th test case the minimum attack count that allows the robot to clear the game. If no such answer exists, output .
2
3 3
3 5 7
4 6 10
7 3 9
1 9 1000
3 2
3 5 6
4 8 7
1 1 1
1 1
59
-1
Hint
More Samples
Please download more samples in the attached file.
Sample 2
See dragon2.in and dragon2.ans in the attached file.
Explanation for Sample 1
Test case 1:
-
The initial sword attack powers are . The health of dragon is , so the sword with attack power is chosen. Attack times, dealing damage. The dragon’s health becomes . After recovering times, its health becomes exactly , so it dies.
-
The sword with attack power disappears, and a sword with attack power is picked up. Now the sword attack powers are . The health of dragon is , so the sword with attack power is chosen. Attack times, dealing damage. The dragon’s health becomes . After recovering times, its health becomes exactly , so it dies.
-
Now the sword attack powers are . The health of dragon is , so the sword with attack power is chosen. Attack times, dealing damage. The dragon’s health becomes . After recovering times, its health becomes exactly , so it dies.
-
There is no way to clear the game with fewer than attacks, so the answer is .
Test case 2: There is no method that can kill both the first dragon and the second dragon, so the game cannot be cleared. Output .
Subtasks
::cute-table{tuack}
| Test Point ID | Attack Power | Other Restrictions | ||||
|---|---|---|---|---|---|---|
| 1 | None | |||||
| 2 | ^ | ^ | ^ | ^ | ||
| 3 | ||||||
| 4 | ^ | |||||
| 5 | Property 1, Property 2 | |||||
| 6 | ^ | ^ | ||||
| 7 | ||||||
| 8 | Property 1 | |||||
| 9 | ^ | ^ | ^ | ^ | ||
| 10 | ||||||
| 11 | ||||||
| 12 | ||||||
| 13 | ||||||
| 14 | No special restrictions | |||||
| 15 | ^ | ^ | ^ | ^ | ||
| 16 | All are primes | Property 1 | ||||
| 17 | ^ | ^ | ^ | ^ | ||
| 18 | No special restrictions | |||||
| 19 | ^ | |||||
| 20 | ||||||
Property 1 means: for any , .
Property 2 means: , i.e. the least common multiple of all is at most .
For all test points, , all weapon attack powers , and the least common multiple of all is .
It is guaranteed that , , and are all positive integers.
Notes
The intermediate results you use may be very large, so pay attention to the variable types used to store them.
Translated by ChatGPT 5