eolymp
bolt
Попробуйте наш новый интерфейс для отправки задач
Задачи

Card Trick

Card Trick

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 \textbf{x_0}, a secret number between \textbf{1} and \textbf{10} inclusive) and skips cards repeatedly as follows: after having selected a card at position \textbf{x_i} with a number \textbf{c(x_i)} on its face, she will select the card at position \textbf{x_\{i+1\} = x_i + c(x_i)}. Jack (\textbf{J}), Queen (\textbf{Q}), and King (\textbf{K}) count as \textbf{10}, Ace (\textbf{A}) counts as \textbf{11}. 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 \textbf{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 \textbf{1} and \textbf{10} 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. \textbf{2-10}, \textbf{J}, \textbf{Q}, \textbf{K}, and \textbf{A}). \includegraphics{https://static.e-olymp.com/content/70/700116db82aa28cbd66098adb1f425b47dc85ed8.jpg} \textit{\textbf{Figure 1}} -- Illustration of rst sample input: my starting position is \textbf{2}, 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: \textbf{Q}). The final \textbf{Q} card is followed by \textbf{0} to \textbf{9} unknown cards, since \textbf{Q} counts as \textbf{10}. \InputFile For each test case: \begin{itemize} \item A line containing two integers \textbf{n} (\textbf{1} ≤ \textbf{n} ≤ \textbf{100}) and \textbf{m} (\textbf{1} ≤ \textbf{m} ≤ \textbf{10}) where \textbf{n} is the number of selected cards and \textbf{m} is the \textbf{1}-based position of my first selected card. \item A line with \textbf{n} tokens that specify the \textbf{n} selected card faces (in order, including the final card). Each card face is given either as an integer \textbf{x} (\textbf{2} ≤ \textbf{x} ≤ \textbf{10}) or as a single character (\textbf{J}, \textbf{Q}, \textbf{K}, or \textbf{A} as specified above). \end{itemize} \OutputFile 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 \textbf{10^\{-7\}}.
Лимит времени 1 секунда
Лимит использования памяти 64 MiB
Входные данные #1
5 2
2 3 5 3 Q
1 1
A
1 2
A
1 10
A
6 1
2 2 2 2 2 2
7 1
2 2 2 2 2 2 2
3 10
10 J K
Выходные данные #1
0.4871377757023325348071573
0.1000000000000000000000000
0.1000000000000000000000000
0.1748923357025314239697490
0.5830713210321767445117468
0.6279229611115749556280350
0.3346565827603272001891974
Источник ACM ICPC NWERC 2013