Four Weighted Dice: Lowest Three Add Up to Three
Math & Bit ManipulationAuto-graded · 18 test casesLast updated
Compute the exact reduced probability that the three smallest of four independent weighted dice sum to 3, which reduces to counting weighted outcomes with at least three ones.
Four distinguishable dice are rolled independently. Every die is identical. Its faces are numbered 1, 2, ..., m, where m = len(weights). Face i + 1 comes up with probability weights[i] / denominator.
Task
A roll is an ordered 4-tuple (r1, r2, r3, r4) of face values. Its probability is the product of the four individual face probabilities.
For a roll, sort the four values in non-decreasing order and take the first three entries. Duplicates are kept as separate entries, so the roll (1, 1, 2, 2) gives the three values 1, 1, 2. Call the sum of these three values the low sum of the roll.
Return the total probability of all ordered rolls whose low sum is exactly 3, as a fraction in lowest terms.
Rules
- Use
weightsanddenominatorexactly as given. Do not normalize or rescale them, and do not assume that the weights add up todenominator. The probability of facei + 1is defined to beweights[i] / denominator, and the answer is the formula above applied to these numbers. - Rolls are ordered.
(1, 1, 1, 2)and(2, 1, 1, 1)are different rolls, and each is counted separately with its own probability. - Each roll is counted at most once, so a roll such as
(1, 1, 1, 1)contributes its probability a single time. - The result is a list
[numerator, denominator]withgcd(numerator, denominator) = 1anddenominator > 0. A probability of zero is returned as[0, 1]. - Return plain Python integers in a list of length 2.
Constraints
1 <= len(weights), typically 6.- Each
weights[i]is a non-negative integer. denominatoris a positive integer.- Values are small enough that exact integer arithmetic is fast.
Examples
Example 1
Input: three_lowest_sum_to_three([1, 1, 1, 1, 1, 1], 6)
Output: [7, 432]
Example 2
Input: three_lowest_sum_to_three([2, 1, 1, 1, 1, 1], 6)
Output: [11, 81]
Hints
Hint 1
Every face value is at least 1, so the three smallest values can total 3 only if all three of them equal 1.
Hint 2
That is the same as asking for at least three of the four dice to show a 1. Only 'is a 1' versus 'is not a 1' matters, so merge all other faces into one weight.
Hint 3
Split into disjoint cases to avoid double counting: exactly three ones (4 positions for the non-1 die) and exactly four ones. Divide by denominator^4 and reduce with Fraction or gcd.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Every face value is at least 1, so the three smallest values sum to at least 3. The sum equals 3 only when all three of them are 1. This happens exactly when at least three of the four dice show a 1. The fourth die is the largest, so it never affects the sum, and ties between values are irrelevant.
Only "is it a 1 or not" matters. Let a = weights[0] be the weight of face 1 and b = sum(weights[1:]) the combined weight of all other faces. Split the event into two disjoint cases:
- Exactly three ones: choose which die is not a 1 (4 ways). The weight is
4 * a^3 * b. - Exactly four ones: one arrangement, with weight
a^4.
Each die contributes a factor of 1 / denominator, so the probability is
(4 * a^3 * b + a^4) / denominator^4.
Reduce it with Fraction (which divides by the gcd) and return [numerator, denominator].
Why it is correct
The two cases are mutually exclusive and together cover every roll with at least three ones. Dice are independent, so the weight of a specific ordered outcome is the product of its per-die weights, and summing over outcomes gives the formula. Writing "three ones plus any fourth die" as 4 * a^3 * (a + b) would count (1,1,1,1) four times, which is why the all-ones case is handled separately.
For fair six-sided dice, a = 1, b = 5, and the denominator is 6. The result is (4 * 5 + 1) / 6^4 = 21/1296 = 7/432.
Complexity
Time is O(m) to sum the m weights, plus a constant amount of big-integer arithmetic and one gcd. Space is O(1).
Reference solution
from fractions import Fraction
from typing import List
def three_lowest_sum_to_three(weights: List[int], denominator: int) -> List[int]:
w1 = weights[0]
other = sum(weights[1:])
# Exactly three ones: 4 position choices, the remaining die is not a 1.
# Exactly four ones: a single arrangement.
numerator = 4 * w1 ** 3 * other + w1 ** 4
p = Fraction(numerator, denominator ** 4)
return [p.numerator, p.denominator]