I'm Attacking the Darkness!

Interview

Time limit1sMemory limit128 MB

Summary
Parse a dice expression with up to six dice and integer modifiers, then compute the reduced fraction of outcomes whose total meets or beats a target value.
Level

Medium6 of 10

Topics
Dynamic programming, Probability, Combinatorics, Implementation
Solved
No attempts yet

Problem

Many tabletop role-playing games (RPGs), including a rather famous one involving dragons and dungeons, use dice to simulate the random events in the game. Unlike most board games that use six-sided dice, RPGs typically use several polyhedral dice with 4, 6, 8, 12, or 20 sides. A player is often required to make a skill check: roll a set of dice, add the individual rolls together, and compare the sum against a pre-determined target value. If the total is equal to or greater than the target value, the skill check succeeds.

A dice roll is written using dice notation, which indicates how many and what kind of dice to roll. For example, 1d4+2d8 tells the player to roll three dice — one 4-sided die plus two 8-sided dice — and add the three results together. A player may also add or subtract a modifier (a constant integer) to the total. For instance, a +3+3 bonus may be written as 1d4+2d8+3, while a −5-5 penalty may be written as 1d4−5+2d8. The faces of each die are numbered from 11 up to the number of sides, so every face is unique; for example, a 4-sided die has faces numbered 1,2,3,41, 2, 3, 4.

Because failing a skill check is usually bad for the character, it is important to know the chance that a skill check succeeds. Write a program that reads a target value and a dice-roll expression, then prints the probability of passing the skill check.

Input

The input begins with a line containing a single integer NN (1≤N≤1001 \le N \le 100), the number of data sets. Each data set is a single line of the form T X, where TT (0≤T≤1000 \le T \le 100) is the target value and XX is a string containing the dice notation. XX contains no spaces.

The dice notation XX contains one or more terms separated by a plus + or minus − sign. Each term is either an integer MM (1≤M≤101 \le M \le 10) or an expression of the form NdS, where NN (1≤N≤61 \le N \le 6) is the number of dice and S∈{4,6,8,12,20}S \in \{4, 6, 8, 12, 20\} is the number of sides. The total number of dice rolled in any one expression does not exceed 66.

Output

For each data set, print a single line containing the probability that the dice roll is greater than or equal to the target value TT. If the target value can never be achieved, print 0. If the target value is always achieved by every possible dice roll (for example, because of modifiers), print 1. Otherwise, print the probability as a fraction reduced to lowest terms.

Examples3

  1. Example 1

    Input
    2
    7 1d6
    7 2d4+1
    
    Expected output
    0
    3/8
    
  2. Example 2

    Input
    3
    3 1d6
    1 1d6
    21 1d20
    
    Expected output
    2/3
    1
    0
    
  3. Example 3

    Input
    5
    0 1d4
    2 1d4
    5 1d4
    1 3
    0 2
    
    Expected output
    1
    3/4
    0
    1
    1