UESTC 890 Card Trick (DP Card magic), uestctrick

Source: Internet
Author: User

UESTC 890 Card Trick (DP Card magic), uestctrick
Here are some cards for you. All the cards are placed on the front of the table. You can choose a starting point. Open the number on the card. Just a few steps forward. J, Q, K is moving forward 10 steps A Moving Forward 11 steps know that the number of steps corresponding to moving forward exceeds the end point input n m and n number indicates that you start with the m card as the starting point to open n cards in turn you can't hide it again. Then Alice starts from one to ten cards and calculates the probability that Alice's last endpoint is the same as yours.


C [I] indicates that the nominal value of the card I was not opened is unknown. c [I] = 0 may be any order in 2-A. d [I] indicates from the probability that your terminal will reach the card I, then the probability of all open cards will be 1, and then the recurrence will start from the end when p [I] = 0 p [I] = sum {p [I + j]} j is any card in 2-A. Note 10, j, q, and k are all 10. The final answer is the sum of the results from 1 to 10 divided by 10.



#include<cstdio>#include<cstring>using namespace std;const int N = 1500;int main(){    char s[3];    int n, m, l;    double p[N], ans;    while (~scanf ("%d%d", &n, &m))    {        memset (p, 0, sizeof (p));        l = m;        for (int i = 1; i <= n; ++i)        {            scanf ("%s", s);            p[l] = 1;            if (s[0]<'A' && s[1]!='0')  l += s[0] - '0';            else if (s[0] == 'A')  l += 11;            else l+= 10;        }        ans = 0;        for (int i = l ; i >= 1; --i)        {            if (p[i] == 0)            {                for (int j = 2; j <= 11; ++j)                {                    int t = (j == 10 ? 4 : 1);                    p[i] += t * p[i + j];                }                p[i] /= 13;            }            if (i <= 10) ans += p[i];        }        printf ("%.8f\n", ans / 10);    }    return 0;}

Card TrickTime Limit: 2999/999 MS (Java/Others) Memory Limit: 65432/65432 KB (Java/Others) Submit  Status

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 variables 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 X0 , A secret number 1 And 10 Implicit) and skips cards repeatedly as follows: after having selected a card at position Xi With a number C (xi) On its face, she will select the card at position Xi + 1 = xi + c (xi) . Jack (J), Queen (Q), And King (K) Count 10 , Ace (A) Counts 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 Xi + c (xi) . 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 ( 1 And 10 Aggressive ). 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.2-10,J,Q,K, AndA).

Authentication of first sample input: my starting position is 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:Q). The finalQCard is followed 0 To 9 Unknown cards, sinceQCounts 10 .

Input

For each test case:

  • A line containing two integers N   (1≤n ≤100) And M ( 1 ≤ m ≤ 10 ) Where N Is the number of selected cards and M Is 1 -Based position of my first selected card.
  • A line N Tokens that specify N Selected card faces (in order, including the final card). Each card face is given either as an integer X ( 2 ≤ x ≤ 10 ) Or as a single character (J,Q,K, OrAAs 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 shoshould have an absolute error of at most 10−7 .

Sample input and output
Sample Input Sample Output
5 22 3 5 3 Q1 1A1 2A1 10A6 12 2 2 2 2 27 12 2 2 2 2 2 23 1010 J K
0.48713777570233253480715730.10000000000000000000000000.10000000000000000000000000.17489233570253142396974900.58307132103217674451174680.62792296111157495562803500.3346565827603272001891974



Zookeeper


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.