Editorial for Facebook Hacker Cup '17 Qualifying Round P3 - Fighting the Zombie


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

For each attack, we need to compute the probability that it rolls at least H damage. We can compute this using dynamic programming.

Let f(D,K) be the probability of dealing at least K damage with D dice. For a given input X Y Z, we want to compute f(X,HZ). We can use the following recursive definition:

  • f(D,K)=1 for K0 (We can always do at least 0 damage)
  • f(0,K)=0 for K>0 (We can't do a positive amount of damage with 0 dice)
  • f(D,K)=1Yd=1Yf(D1,Kd)

This last formula combines the outcomes of all possible die rolls for a single die, and weights them evenly by 1Y.

In this way, we can compute the probability of success for each attack in O(XY(HZ)) time.

Since the most damage we can do is XY, we can trivially reject any case where HZ>XY. That means we can also consider the time complexity to be O(XYXY)=O(X2Y2).


Comments

There are no comments at the moment.