Residue Distribution of Dice Sums and 2×2 Stochastic Determinants
Math & Bit ManipulationAuto-graded · 19 test casesLast updated
Tests whether a candidate can compute modular probability distributions over two-dice outcomes and derive the determinant formula for a constrained matrix class.
Part 1: Two Dice, Sum Modulo 3
You roll two independent fair six-sided dice. Each face shows an integer from 1 to 6 with equal probability. Let T be the total of the two face values.
Compute the full probability distribution of T mod 3 — that is, find P(T mod 3 = k) for each k in {0, 1, 2}.
Return the three probabilities as a list indexed by residue: index 0 holds P(T mod 3 = 0), index 1 holds P(T mod 3 = 1), and index 2 holds P(T mod 3 = 2).
Part 2: Eigenvalue and Determinant of a Balanced Square Matrix
A square matrix is called balanced if every entry is non-negative and every row and every column sums to exactly 1.
Part 2a — Fixed eigenvalue
Show that 1 is always an eigenvalue of any balanced matrix. Your function should return True for the key "eigenvalue_one" to indicate this fact holds unconditionally.
Part 2b — Determinant of a 2×2 balanced matrix
Every 2×2 balanced matrix is completely determined by its top-left entry a, where 0 ≤ a ≤ 1. The matrix must take the form:
M = [[a, 1-a],
[1-a, a]]
Given a, compute the determinant of M and return it under the key "determinant".
What to return
Return a dictionary with exactly these three keys:
| Key | Type | Meaning |
|---|---|---|
"dice_mod_3" | list[float] | [P(T mod 3=0), P(T mod 3=1), P(T mod 3=2)] |
"eigenvalue_one" | bool | Always True |
"determinant" | float | det(M) for the given a |
Constraints
- The dice outcomes are always a standard pair of fair six-sided dice (faces 1–6); no input controls this.
ais afloat. The function must handle any real value ofa; validation ofa ∈ [0, 1]is not required.- Each entry in
"dice_mod_3"must equal exactly1/3(as a Python float). "eigenvalue_one"must always beTrue."determinant"must equal2*a - 1to floating-point precision.
Examples
Example 1
Input: dice_and_balanced_matrix(0.5)
Output: {'dice_mod_3': [0.3333333333333333, 0.3333333333333333, 0.3333333333333333], 'eigenvalue_one': True, 'determinant': 0}
Example 2
Input: dice_and_balanced_matrix(0)
Output: {'dice_mod_3': [0.3333333333333333, 0.3333333333333333, 0.3333333333333333], 'eigenvalue_one': True, 'determinant': -1}
Example 3
Input: dice_and_balanced_matrix(1)
Output: {'dice_mod_3': [0.3333333333333333, 0.3333333333333333, 0.3333333333333333], 'eigenvalue_one': True, 'determinant': 1}
Example 4
Input: dice_and_balanced_matrix(0.25)
Output: {'dice_mod_3': [0.3333333333333333, 0.3333333333333333, 0.3333333333333333], 'eigenvalue_one': True, 'determinant': -0.5}
Hints
Hint 1
For Part 1, think about what values a single fair die can take modulo 3. Each residue class {0, 1, 2} is hit by exactly two faces. What does that say about the distribution of one die mod 3, and therefore about the sum of two independent dice mod 3?
Hint 2
For Part 2a, try multiplying any balanced matrix by the all-ones column vector. What does each row sum tell you about the result?
Hint 3
For Part 2b, write out all four constraints (two row sums, two column sums) in terms of a, b, c, d. You will find that b, c, and d are each fully determined by a alone, which gives you a one-parameter family. Then compute ad - bc.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Part 1: Distribution of T mod 3
The key observation is that a single fair six-sided die is uniform modulo 3. The six faces split evenly across the three residue classes:
- Residue 0: faces {3, 6}
- Residue 1: faces {1, 4}
- Residue 2: faces {2, 5}
So each die independently takes each value in {0, 1, 2} mod 3 with probability exactly 1/3.
Now fix any outcome r for D1 mod 3. The sum T ≡ 0 (mod 3) requires D2 ≡ −r (mod 3), which happens with probability 1/3 regardless of r. The same argument applies to residues 1 and 2. Therefore T mod 3 is also uniform on {0, 1, 2}, giving P(T mod 3 = k) = 1/3 for every k.
You can confirm by brute force: of the 36 equally likely (D1, D2) pairs, exactly 12 produce each residue (sums divisible by 3 are T ∈ {3, 6, 9, 12}, with pair-counts 2, 5, 4, 1, totalling 12).
A common mistake is to start from the raw triangular distribution of T over {2, …, 12} and try to group by residue without first noticing the mod-3 uniformity. That path is messier and easy to get wrong.
Part 2a: Why 1 is always an eigenvalue
Let v be the all-ones column vector. For any balanced matrix M, row i of Mv is the sum of entries in row i, which equals 1 by definition. So Mv = v, meaning v is an eigenvector with eigenvalue 1. Only the row-sum condition is needed; the column condition gives the symmetric left-eigenvector result vᵀM = vᵀ.
Part 2b: Determinant as a function of a
Applying the four sum constraints to M = [[a, b], [c, d]]:
- Row 1: b = 1 − a
- Column 1: c = 1 − a
- Row 2: d = 1 − c = a
- Column 2: b + d = (1 − a) + a = 1 ✓ (automatically satisfied)
So b = c = 1 − a and d = a, leaving a as the single free parameter. The determinant is:
det M = a·d − b·c = a² − (1−a)² = a² − 1 + 2a − a² = 2a − 1
Since a ∈ [0, 1], this ranges from −1 (at a = 0, the swap/permutation matrix) to 1 (at a = 1, the identity). The midpoint a = 0.5 gives a rank-1 matrix with determinant 0.
Complexity
- Part 1: a fixed double loop over 36 outcomes — O(1) time and O(1) space. For k-sided dice it would be O(k²), or O(k) using residue-count convolution.
- Part 2: two arithmetic operations — O(1) time and O(1) space.
Reference solution
from typing import Dict
def dice_and_balanced_matrix(a: float) -> Dict[str, object]:
# Part 1: enumerate all 36 equally likely outcomes and tally residues.
counts = [0, 0, 0]
for d1 in range(1, 7):
for d2 in range(1, 7):
counts[(d1 + d2) % 3] += 1
distribution = [count / 36 for count in counts] # each entry = 12/36 = 1/3
# Part 2b: M = [[a, 1-a], [1-a, a]]
# det = a*a - (1-a)*(1-a) = a^2 - 1 + 2a - a^2 = 2a - 1
determinant = 2 * a - 1
return {
"dice_mod_3": distribution,
"eigenvalue_one": True, # M @ [1,1,...,1]^T = [1,1,...,1]^T always
"determinant": determinant,
}