TopCoder problem "AntiMatter" used in SRM 179 (Division I Level Three)

Problem Statement

    We are studying sub-atomic particles. At regular time intervals, all the particles simultaneously undergo a transformation. The state of each particle can be represented by an integer value (which can be arbitrarily large), and each transformation changes the state of a particle by one of 4 values. At each transformation time, each of the particles independently undergoes one of the 4 changes. Each of the four possibilities is equally likely to occur to each particle.

We believe that whenever two adjacent particles transform to have identical states, they annihilate each other. We will call a pair of particles "unstable" if there is a sequence of transformations that could lead to an annihilation.

We need to estimate the probability that two adjacent particles are unstable. To make this specific, we will choose two particles and assume that each is equally likely to have any initial state between -4999 and 5000 inclusive (a state of 0 is possible). Create a class AntiMatter that contains a method unstable that receives a int[] xform containing the 4 legal transformation amounts and returns the probability that there exists a sequence of transformations for the two particles that would lead to mutual annihilation.

The method should return the probability as a String with a decimal point, exactly eight digits to the right of the decimal point, and no leading zeros. Since there are 100,000,000 equally likely pairs, this probability will be exact.



Method signature:String unstable(int[] xform)
(be sure your method is public)


-xform will contain exactly 4 elements (not necessarily distinct).
-Each element in xform will be between -10,000 and 10,000 inclusive.


Returns: ".00010000"
Every transition of each particle increases its state by 6, so unless the two particles start with identical states and annihilate immediately, they can never reach identical states. There are 10,000 pairs of particles that immediately annihilate.
Returns: ".50000000"
If two particles initially have states that differ by an odd number, then after every transition they will still differ by an odd number, so they can never annihilate. But if they start with an even difference in their states, then the lower one can always increase its state by 1 while the higher one decreases by 1, leading eventually to annihilation. Since half of the 100,000,000 pairs are either even-even or odd-odd, 50,000,000 are unstable.
Returns: "1.00000000"
The lower particle can stay at its initial state by transforming by 0 each time, while the higher particle transforms by -1 until annihilation occurs.
Returns: ".00126448"

Problem url:

Problem stats url:




lbackstrom , brett1479

Problem categories:

Dynamic Programming