luogu#P4774. [NOI2018] 屠龙勇士

    ID: 3766 远端评测题 2000ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2018NOIO2优化不定方程中国剩余定理 CRT逆元

[NOI2018] 屠龙勇士

Problem Description

Xiao D recently found a mini game online. The rules are as follows:

  • The goal of the game is to kill nn dragons in order 1n1 \rightarrow n. Each dragon has an initial health value aia_i. At the same time, each dragon has a recovery ability: when it uses recovery, its health increases by pip_i each time until its health becomes non-negative. The dragon will only die when the attack ends and its health is exactly 00.
  • At the start of the game, the player has mm 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 xx times, reducing the dragon’s health by x×ATKx \times ATK.
  • Then, the dragon will keep using its recovery ability, recovering pip_i health each time. If before using recovery, or after some recovery, its health becomes 00, 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 xx 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 1-1.

Input Format

The first line contains an integer TT, the number of test cases.

Then there are TT test cases, each containing 55 lines.

  • The first line of each test case contains two integers nn and mm, representing the number of dragons and the number of initial swords;
  • The next line contains nn positive integers; the ii-th number is the initial health aia_i of the ii-th dragon;
  • The next line contains nn positive integers; the ii-th number is the recovery value pip_i of the ii-th dragon;
  • The next line contains nn positive integers; the ii-th number is the attack power of the sword rewarded after killing the ii-th dragon;
  • The next line contains mm positive integers, the attack powers of the mm initial swords.

Output Format

Output TT lines in total.

The ii-th line contains one integer, indicating for the ii-th test case the minimum attack count xx that allows the robot to clear the game. If no such answer exists, output 1-1.

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 {1,9,1000}\{1,9,1000\}. The health of dragon 11 is 33, so the sword with attack power 11 is chosen. Attack 5959 times, dealing 5959 damage. The dragon’s health becomes 56-56. After recovering 1414 times, its health becomes exactly 00, so it dies.

  • The sword with attack power 11 disappears, and a sword with attack power 77 is picked up. Now the sword attack powers are {7,9,1000}\{7,9,1000\}. The health of dragon 22 is 55, so the sword with attack power 77 is chosen. Attack 5959 times, dealing 413413 damage. The dragon’s health becomes 408-408. After recovering 6868 times, its health becomes exactly 00, so it dies.

  • Now the sword attack powers are {3,9,1000}\{3,9,1000\}. The health of dragon 33 is 77, so the sword with attack power 33 is chosen. Attack 5959 times, dealing 177177 damage. The dragon’s health becomes 170-170. After recovering 1717 times, its health becomes exactly 00, so it dies.

  • There is no way to clear the game with fewer than 5959 attacks, so the answer is 5959.

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 1-1.

Subtasks

::cute-table{tuack}

Test Point ID nn mm pip_i aia_i Attack Power Other Restrictions
1 105\le 10^5 =1=1 105\le 10^5 =1=1 None
2 ^ ^ ^ ^
3 105\le 10^5
4 ^
5 103\le 10^3 105\le 10^5 Property 1, Property 2
6 ^ ^
7
8 =1=1 108\le 10^8 106\le 10^6 Property 1
9 ^ ^ ^ ^
10
11
12
13
14 =105=10^5 =1=1 No special restrictions
15 ^ ^ ^ ^
16 105\le 10^5 All pip_i are primes 1012\le 10^{12} Property 1
17 ^ ^ ^ ^
18 No special restrictions
19 ^
20

Property 1 means: for any ii, aipia_i \le p_i.

Property 2 means: lcm(pi)106\operatorname{lcm}(p_i) \le 10^6, i.e. the least common multiple of all pip_i is at most 10610^6.

For all test points, T5T \le 5, all weapon attack powers 106\le 10^6, and the least common multiple of all pip_i is 1012\le 10^{12}.

It is guaranteed that TT, nn, and mm 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