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 be the survival probaility of bears under the assumption that bears, ninjas and bears left. Also let us consider "meetings" as uniformaly consectivly choosen two inhabitants. Let be -th choice bear/ninja/hunter. The following equality consists of union of pairwise disjunkt occurances and it holds.
We can use it to construct an equation for :
Now let's make it more readable:
In addition to that, we have 3 base cases: , and . Using a DP table, the code looks like this and runs in :