Card Trick#7015

Card Trick

1 second
64 megabytes
Medium

I am learning magic tricks to impress my girlfriend Alice. My latest trick is a probabilistic one, i.e. it does work in most cases, but not in every case. To perform the trick, I first shuffle a set of many playing cards and put them all in one line with faces up on the table. Then Alice secretly selects one of the first ten cards (i.e. she chooses x_0, a secret number between and inclusive) and skips cards repeatedly as follows: after having selected a card at position x_i with a number c(x_i) on its face, she will select the card at position x_{i+1} = x_i + c(x_i). Jack (J), Queen (Q), and King (K) count as , Ace (A) counts as . You may assume that there are at least ten cards on the table.

Alice stops this procedure as soon as there is no card at position x_i + c(x_i). I then perform the same procedure from a randomly selected starting position that may be different from the position selected by Alice. It turns out that often, I end up at the same position. Alice is very impressed by this trick.

However, I am more interested in the underlying math. Given my randomly selected starting position and the card faces of every selected card (including my final one), can you compute the probability that Alice chose a starting position ending up on the same final card? You may assume that her starting position is randomly chosen with uniform probability (between and inclusive). I forgot to note the cards that I skipped, so these cards are unknown. You may assume that the card face of every single of the unknown cards is independent of the other card faces and random with uniform probability out of the possible card faces (i.e. -, J, Q, K, and A).

Figure 1 – Illustration of first sample input: my starting position is , so I start selecting that card. Then I keep skipping cards depending on the card's face. This process iterates until there are not enough cards to skip (in this sample: Q). The final Q card is followed by to unknown cards, since Q counts as .

Input

For each test case:

  • A line containing two integers (1 \le n \le 100) and (1 \le m \le 10) where is the number of selected cards and is the -based position of my first selected card.

  • A line with tokens that specify the selected card faces (in order, including the final card). Each card face is given either as an integer (2 \le x \le 10) or as a single character (J, Q, K, or A as specified above).

Output

For each test case, print one line containing the probability that Alice chooses a starting position that leads to the same final card. Your output should have an absolute error of at most 10^{-7}.

Examples

Example 1

Loading
Loading