Try   HackMD

I thought this was relatively harder so wanted to analyse this as well.

The task goes as follows: The inhabitants of an island are divided into three tribes: Bears, Hunters, and Ninjas. They are very hostile, and whenever two members of different tribes meet, the stronger one always eliminates the weaker one. It is known that a bear is stronger than a ninja, a ninja is stronger than a hunter, a hunter is stronger than a bear. The hostility goes on until only one tribe remains. What is the probability that each tribe is the last one standing, given initial population size?

Once again, before coding let's get into the calculation and without the loss of generality, let's consider the probability chance of bear. Let

Pr[Bi,j,k] be the survival probaility of bears under the assumption that
i
bears,
j
ninjas and
k
bears left. Also let us consider "meetings" as uniformaly consectivly choosen two inhabitants. Let
Xi/Yi/Zi
be
i∈{1,2}
-th choice bear/ninja/hunter. The following equality consists of union of pairwise disjunkt occurances and it holds.

Pr[(Y2∩X1)βˆͺ(Z2∩X1)βˆͺ(X2∩X1)βˆͺ(Y2∩Y1)βˆͺ(Z2∩Y1)βˆͺ(X2∩Y1)βˆͺ(Z2∩Z1)βˆͺ(X2∩Z1)βˆͺ(Y2∩Z1)]=Pr[(X1∩(Y2βˆͺZ2βˆͺX2))βˆͺ(Y1∩(Y2βˆͺZ2βˆͺX2))βˆͺ(Z1∩(Y2βˆͺZ2βˆͺX2))]=Pr[(Y2βˆͺZ2βˆͺX2)∩(X1βˆͺY1βˆͺZ1)]=Pr[X1βˆͺY1βˆͺZ1]Pr[(Y2βˆͺZ2βˆͺX2)|(X1βˆͺY1βˆͺZ1)]=1

We can use it to construct an equation for

Pr[Bi,j,k]:
Pr[Bi,j,k]=Pr[X1]Pr[Y2|X1]Pr[Bi,j,k|Y2∩X1]+Pr[X1]Pr[Z2|X1]Pr[Bi,j,k|Z2∩X1]+Pr[X1]Pr[X2|X1]Pr[Bi,j,k|X2∩X1]+Pr[Y1]Pr[Y2|Y1]Pr[Bi,j,k|Y2∩Y1]+Pr[Y1]Pr[Z2|Y1]Pr[Bi,j,k|Z2∩Y1]+Pr[Y1]Pr[X2|Y1]Pr[Bi,j,k|X2∩Y1]+Pr[Z1]Pr[Z2|Z1]Pr[Bi,j,k|Z2∩Z1]+Pr[Z1]Pr[X2|Z1]Pr[Bi,j,k|X2∩Z1]+Pr[Z1]Pr[Y2|Z1]Pr[Bi,j,k|Y2∩Z1]

Now let's make it more readable:
Pr[Bi,j,k]=ii+j+kβ‹…ji+j+kβˆ’1Pr[Bi,jβˆ’1,k]+ii+j+kβ‹…ki+j+kβˆ’1Pr[Biβˆ’1,j,k]+ii+j+kβ‹…iβˆ’1i+j+kβˆ’1Pr[Bi,j,k]+ji+j+kβ‹…jβˆ’1i+j+kβˆ’1Pr[Bi,j,k]+ji+j+kβ‹…ki+j+kβˆ’1Pr[Bi,j,kβˆ’1]+ji+j+kβ‹…ii+j+kβˆ’1Pr[Bi,jβˆ’1,k]+ki+j+kβ‹…kβˆ’1i+j+kβˆ’1Pr[Bi,j,k]+ki+j+kβ‹…ii+j+kβˆ’1Pr[Biβˆ’1,j,k]+ki+j+kβ‹…ji+j+kβˆ’1Pr[Bi,j,kβˆ’1]=2ijβ‹…Pr[Bi,jβˆ’1,k]+2ikβ‹…Pr[Biβˆ’1,j,k]+2jkβ‹…Pr[Bi,j,kβˆ’1](i+j+k)(i+j+kβˆ’1)+i2βˆ’i+j2βˆ’j+k2βˆ’k(i+j+k)(i+j+kβˆ’1)Pr[Bi,j,k]⇔Pr[Bi,j,k]=2ijβ‹…Pr[Bi,jβˆ’1,k]+2ikβ‹…Pr[Biβˆ’1,j,k]+2jkβ‹…Pr[Bi,j,kβˆ’1]ij+ik+ji+jk+ki+kj

In addition to that, we have 3 base cases:
Pr[Bi>0,j,0]=1
,
Pr[Bi=0,j,k]=0
and
Pr[Bi,j=0,k>0]=0
. Using a DP table, the code looks like this and runs in
O(bnh)
:
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More β†’