CF 9A - Die Roll

We are asked to calculate the probability that Dot wins a simple dice game against Yakko and Wakko. Each character rolls a standard six-sided die. Dot wins if her roll is at least as large as the maximum of Yakko’s and Wakko’s rolls.

CF 9A - Die Roll

Rating: 800
Tags: math, probabilities
Solve time: 2m 43s
Verified: yes

Solution

Problem Understanding

We are asked to calculate the probability that Dot wins a simple dice game against Yakko and Wakko. Each character rolls a standard six-sided die. Dot wins if her roll is at least as large as the maximum of Yakko’s and Wakko’s rolls. Yakko and Wakko’s rolls are given as input, two integers between 1 and 6. We must output the probability that Dot wins in the form of an irreducible fraction.

The input consists of two numbers, Y and W, representing Yakko’s and Wakko’s rolls. The output is a fraction A/B where B is the total number of outcomes on a die (6) and A is the number of outcomes that allow Dot to match or exceed the higher of Y and W. The fraction must be reduced to lowest terms.

The main edge cases occur when Dot’s minimum winning roll is 6, where she can only win with one outcome, giving a probability of 1/6. Another is when both Y and W are 1, which makes Dot’s winning roll any value from 1 to 6, producing a probability of 1/1. A careless approach might calculate probability without considering the maximum of the two input values, leading to incorrect results when Yakko and Wakko differ.

Approaches

A naive approach would be to simulate all possible rolls for Dot and count the outcomes where Dot’s roll is at least as large as Yakko’s and Wakko’s maximum. This works because there are only six outcomes for Dot, making it feasible to iterate over all possibilities. The brute-force approach is correct, but simulating each die roll explicitly is unnecessary because the outcomes follow a uniform distribution and can be reasoned mathematically.

The key observation is that Dot only needs to roll the maximum of Y and W or higher. Let max_roll = max(Y, W). The number of favorable outcomes is 6 - max_roll + 1 since a die has faces from 1 to 6. The total possible outcomes are always 6. Once the number of favorable outcomes is known, we reduce the fraction to simplest form using the greatest common divisor (GCD).

Approach Time Complexity Space Complexity Verdict
Brute Force O(6) O(1) Accepted
Optimal O(1) O(1) Accepted

Algorithm Walkthrough

  1. Read the two integers from input representing Yakko’s and Wakko’s rolls.
  2. Compute the maximum roll between Y and W. This represents the minimum value Dot needs to match or exceed.
  3. Calculate the number of favorable outcomes for Dot as 6 - max_roll + 1.
  4. Determine the greatest common divisor of the numerator (favorable outcomes) and denominator (6) to reduce the fraction.
  5. Output the fraction in the form A/B using the reduced numerator and denominator.

Why it works: The invariant is that the probability depends solely on the maximum of Y and W because Dot only wins when her roll meets or exceeds this value. By calculating the number of outcomes that satisfy this and reducing the fraction, we guarantee correctness for all possible inputs.

Python Solution

import sys
import math
input = sys.stdin.readline

Y, W = map(int, input().split())

max_roll = max(Y, W)
favorable = 6 - max_roll + 1
gcd = math.gcd(favorable, 6)

print(f"{favorable // gcd}/{6 // gcd}")

The code reads two integers, computes the maximum roll, counts the favorable outcomes, and reduces the fraction using math.gcd. Dividing both numerator and denominator by the GCD ensures the output is irreducible. Using integer division avoids floating-point inaccuracies.

Worked Examples

Example 1: Input 4 2

max_roll = max(4, 2) = 4

favorable = 6 - 4 + 1 = 3

gcd = gcd(3, 6) = 3

Output 3/3 / 6/3 = 1/2

Step max_roll favorable gcd fraction
1 4 3 3 1/2

Dot can roll 4, 5, or 6 to win. This confirms the calculation.

Example 2: Input 1 1

max_roll = 1

favorable = 6 - 1 + 1 = 6

gcd = gcd(6, 6) = 6

Output 6/6 / 6/6 = 1/1

Step max_roll favorable gcd fraction
1 1 6 6 1/1

All rolls allow Dot to win, which matches the expected edge case.

Complexity Analysis

Measure Complexity Explanation
Time O(1) We perform a constant number of arithmetic operations and a single GCD computation.
Space O(1) Only a few integer variables are used.

The solution easily fits within the 1-second time limit and 64 MB memory constraint.

Test Cases

import sys, io
import math

def run(inp: str) -> str:
    sys.stdin = io.StringIO(inp)
    Y, W = map(int, input().split())
    max_roll = max(Y, W)
    favorable = 6 - max_roll + 1
    gcd = math.gcd(favorable, 6)
    return f"{favorable // gcd}/{6 // gcd}"

# Provided sample
assert run("4 2\n") == "1/2", "sample 1"

# Custom cases
assert run("1 1\n") == "1/1", "minimum rolls"
assert run("6 6\n") == "1/6", "maximum rolls"
assert run("2 5\n") == "1/2", "mixed rolls"
assert run("3 3\n") == "2/3", "equal mid rolls"
assert run("1 6\n") == "1/6", "extreme difference"
Test input Expected output What it validates
1 1 1/1 Dot wins in all cases
6 6 1/6 Only one winning outcome
2 5 1/2 Correct max selection and probability
3 3 2/3 Equal rolls calculation
1 6 1/6 Boundary condition with min and max

Edge Cases

If both Yakko and Wakko roll 6, Dot can only win by rolling 6. The algorithm computes max_roll = 6, then favorable = 6 - 6 + 1 = 1, gcd = gcd(1, 6) = 1, producing 1/6. If both roll 1, max_roll = 1, favorable = 6, gcd = 6, output 1/1. The method handles all intermediate values by subtracting the maximum from 6 and adding 1, so no off-by-one errors occur.