Play the Dragon

Time limit5sMemory limit512 MB

Summary
Given dragon and knight stats, find the fewest turns of attack, buff, cure, or debuff actions to defeat the knight, or report it is impossible.
Level

Medium7 of 10

Topics
Brute force, Greedy, Simulation, Implementation
Solved
No attempts yet

Problem

You are a friendly dragon defending your lair from a greedy knight. You have HdH_d health points and an attack power of AdA_d, and the knight has HkH_k health points and an attack power of AkA_k. If your health drops to 0 or below at any point, you are knocked out and you lose immediately. If the knight's health drops to 0 or below at any point, the knight is knocked out and you win.

The battle runs in turns. On each turn you act first, and you choose exactly one of the following actions.

  • Attack: reduce the opponent's health by your own attack power.
  • Buff: increase your attack power by BB for the rest of the battle.
  • Cure: set your health to HdH_d.
  • Debuff: decrease the opponent's attack power by DD for the rest of the battle. If this would make the opponent's attack power less than 0, it becomes 0 instead.

After your action, if the knight's health is greater than 0, the knight attacks, and then the turn ends. A turn in which you defeat the knight still counts as a turn, even though the knight does not get to act.

Buffs stack, so every buff adds another BB to your attack power. Debuffs stack the same way.

You want to defeat the knight as fast as possible, because you do not want to be late for roasting marshmallows with the villagers at tonight's festival. Find the minimum number of turns in which you can defeat the knight, or report that defeating the knight is impossible.

Input

The first line contains the number of test cases TT. Each of the next TT lines contains six integers HdH_d, AdA_d, HkH_k, AkA_k, BB, and DD, separated by spaces.

Limits

  • 1≤T≤1001 \le T \le 100
  • 1≤Hd≤1041 \le H_d \le 10^4
  • 1≤Ad≤1041 \le A_d \le 10^4
  • 1≤Hk≤1041 \le H_k \le 10^4
  • 1≤Ak≤1041 \le A_k \le 10^4
  • 0≤B≤1040 \le B \le 10^4
  • 0≤D≤1040 \le D \le 10^4

Output

For each test case, print one line in the form Case #x: y, where x is the test case number starting from 1 and y is the minimum number of turns needed to defeat the knight. If the knight cannot be defeated, print IMPOSSIBLE in place of y.

Hint

In the first test case you have 11 health and 5 attack, and the knight has 16 health and 5 attack. One optimal sequence of actions is:

  • Turn 1: Attack, reducing the knight's health to 11. The knight then attacks and reduces your health to 6.
  • Turn 2: Attack, reducing the knight's health to 6. The knight then attacks and reduces your health to 1.
  • Turn 3: Cure, restoring your health to 11. The knight then attacks and reduces your health to 6. (If you attacked this turn instead, the knight's next attack would knock you out.)
  • Turn 4: Attack, reducing the knight's health to 1. The knight then attacks and reduces your health to 1.
  • Turn 5: Attack, reducing the knight's health to -4. You win immediately and the knight does not attack again.

In the second test case one optimal sequence of actions is:

  • Turn 1: Buff, raising your attack power to 3. The knight then attacks and reduces your health to 1.
  • Turn 2: Attack, reducing the knight's health to 0. You win immediately and the knight does not attack again.

In the third test case the knight needs only two attacks to knock you out, and you cannot deal enough damage fast enough. Using Cure after every attack keeps the battle going forever, but the knight can never be defeated.

In the fourth test case one optimal sequence of actions is Attack, Debuff, Buff, Attack, Attack.

Examples1

  1. Example 1

    Input
    4
    11 5 16 5 0 0
    3 1 3 2 2 0
    3 1 3 2 1 0
    2 1 5 1 1 1
    
    Expected output
    Case #1: 5
    Case #2: 2
    Case #3: IMPOSSIBLE
    Case #4: 5