problems / 184

Dice Race Between Alice and Bob

medium probability dp games modular-arithmetic 1000 ms · 256 MB

Dice Race Between Alice and Bob

Alice and Bob are locked in a dice-rolling contest. They take turns rolling a fair six-sided die, starting with Alice, and each keeps a running total of their rolls. Alice wins if she reaches a sum of at least n before Bob reaches a sum of at least m. What is the probability that Alice wins?

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 100).

Output

Print the probability that Alice wins as a fraction modulo 10⁹ + 7. If the probability is p/q in lowest terms, output p · q⁻¹ (mod 10⁹ + 7), where q⁻¹ is the modular inverse of q modulo 10⁹ + 7.

Notes

  • A player wins as soon as their running total reaches or exceeds their target (n for Alice, m for Bob).
  • The probability should be output as a single integer representing the fraction modulo 10⁹ + 7.
  • Use Fermat's Little Theorem to compute modular inverses: q⁻¹ ≡ q^(10⁹+5) (mod 10⁹+7).

Example 1

Input

1 1

Output

1

Example 2

Input

2 2

Output

861111118

Hints · reveal one at a time if you're stuck

Hint 1

Let the state be (Alice's total, Bob's total, whose turn). Which states are already decided, and what is Alice's chance of winning from a decided state?

Hint 2

A roll that pushes a total to its target ends the game at once, so only states where both totals are still below their targets need a value. Each such state is the average over the six die faces of the state it moves to, so fill in values from the largest totals downward.

Hint 3

Since the answer is a fraction modulo 10^9 + 7, each die face contributes a factor of 1/6, which is a modular inverse. Keep all arithmetic in modular form.

report an issue Prefer your own editor? sooks prep 184 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.