This page is still under construction.

Parts of this page are still being built. What you see may change.

The Battle for Wesnoth

Time limit0.1sMemory limit1024 MB

Summary
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:

  • dd, the damage of one successful blow;
  • bb, the number of blows;
  • pp, the probability in percent that a single blow succeeds.

Each of the bb blows succeeds independently with probability p/100p/100, and every successful blow takes away dd hitpoints. In the game dd and bb are properties of the attacker, while pp usually comes from the terrain the defender stands on, but here pp is a property of the attacker as well, the way magical attacks work.

Take an attack with d=6d=6, b=2b=2 and p=60p=60. It has three outcomes:

  • with probability 16%16\% both blows miss and no damage is done;
  • with probability 48%48\% exactly one blow lands and the damage is 66 hitpoints;
  • with probability 36%36\% both blows land and the damage is 1212 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 mm instead of dd and bb: when she attacks, the player may choose any positive integers dd and bb with d×b≤md \times b \le m.

David regularly needs the Elvish Princess to kill an ugly skeleton or a stinking orcish warrior, so he wants to know which choice of dd and bb 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 mm (1≤m≤1061 \le m \le 10^6) and pp (1≤p≤991 \le p \le 99), the parameters of the attacker. The second line contains an integer nn (1≤n≤1051 \le n \le 10^5), the number of units to be killed. The third line contains nn integers h1,h2,…,hnh_1, h_2, \dots, h_n (1≤hi≤1061 \le h_i \le 10^6), where hih_i is the number of hitpoints of the ii-th unit to be killed.

Output

For each unit print one line with two integers dd and bb, 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 dd, and among those the one with the smallest bb. When the unit cannot be killed at all, so that every choice has probability 00, print 1 1.

Hint

For m=10m=10 and p=60p=60, a unit with 55 hitpoints is killed with probability 84%84\%, a unit with 66 hitpoints with probability 68.256%68.256\%, and a unit with 77 hitpoints with probability 60%60\%.

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 a=0.5a=0.5 and b=10−100b=10^{-100}, the sum a+ba+b evaluates to aa.

Examples2

  1. Example 1

    Input
    10 60
    3
    5 6 7
    
    Expected output
    5 2
    2 5
    7 1
    
  2. Example 2

    Input
    5 50
    4
    1 5 6 1000000
    
    Expected output
    1 5
    5 1
    1 1
    1 1