The Battle for Wesnoth
Time limit0.1sMemory limit1024 MB
Choose positive d and b with d*b <= m to maximize the chance that the total damage of b independent blows, each landing with probability p/100 and dealing d, kills a unit with h hitpoints; report the smallest-d, smallest-b optimum or 1 1 if impossible.
- Level
Hard8 of 10
- Topics
- Probability, Math, Combinatorics, Binary search
- Solved
- No attempts yet
Problem
The Battle for Wesnoth is a turn based fantasy strategy game with elves, orcs, undead, dwarves and drakes. Only two facts about it matter here. Every unit has a number of hitpoints, and combat takes hitpoints away. We look at the simplest kind of combat, a plain attack on a defenseless unit.
An attack is described by three integers:
- , the damage of one successful blow;
- , the number of blows;
- , the probability in percent that a single blow succeeds.
Each of the blows succeeds independently with probability , and every successful blow takes away hitpoints. In the game and are properties of the attacker, while usually comes from the terrain the defender stands on, but here is a property of the attacker as well, the way magical attacks work.
Take an attack with , and . It has three outcomes:
- with probability both blows miss and no damage is done;
- with probability exactly one blow lands and the damage is hitpoints;
- with probability both blows land and the damage is hitpoints.
A unit dies once its hitpoints become zero or less.
David plays an add-on in which the Elvish Princess has a special magical attack. She is described by a single integer instead of and : when she attacks, the player may choose any positive integers and with .
David regularly needs the Elvish Princess to kill an ugly skeleton or a stinking orcish warrior, so he wants to know which choice of and gives the highest probability of killing the enemy. Find that choice for every unit he needs to kill.
Input
The first line contains two integers () and (), the parameters of the attacker. The second line contains an integer (), the number of units to be killed. The third line contains integers (), where is the number of hitpoints of the -th unit to be killed.
Output
For each unit print one line with two integers and , the choice that maximizes the probability of killing that unit.
Several choices can reach the same maximum probability. In that case print the one with the smallest , and among those the one with the smallest . When the unit cannot be killed at all, so that every choice has probability , print 1 1.
Hint
For and , a unit with hitpoints is killed with probability , a unit with hitpoints with probability , and a unit with hitpoints with probability .
Double precision is enough for the whole computation, but watch out for overflow and underflow, and avoid unsafe operations such as adding numbers of very different magnitude. For and , the sum evaluates to .