首页 > 代码库 > UESTC 890 Card Trick(DP 纸牌魔术)
UESTC 890 Card Trick(DP 纸牌魔术)
c[i]表示第i张牌的面值 没被掀开的牌的面值都是未知的c[i]=0 可能为2-A中的任意一个 令d[i]表示从你的终点到达第i张牌的概率 那么所有掀开过的牌的概率都为1 然后从终点向前递推 当p[i]=0时 p[i]=sum{p[i+j]} j为2-A中任意一张牌 注意10,j,q,k的时候都是10 最后的答案就是1到10的结果加起来除以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 Trick
Time Limit: 2999/999MS (Java/Others) Memory Limit: 65432/65432KB (Java/Others)
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 J
), Queen (Q
), and King (K
) count as A
) counts as
Alice stops this procedure as soon as there is no card at position
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 2
-10
, J
, Q
, K
, and A
).
Illustration of first sample input: my starting position is Q
). The final Q
card is followed by Q
counts as
Input
For each test case:
- A line containing two integers
n (1≤n≤100) andm (1≤m≤10 ) wheren is the number of selected cards andm is the1 -based position of my first selected card. - A line with
n tokens that specify then selected card faces (in order, including the final card). Each card face is given either as an integerx (2≤x≤10 ) or as a single character (J
,Q
,K
, orA
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
Sample input and output
Sample Input | Sample Output |
---|---|
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 | 0.4871377757023325348071573 0.1000000000000000000000000 0.1000000000000000000000000 0.1748923357025314239697490 0.5830713210321767445117468 0.6279229611115749556280350 0.3346565827603272001891974 |
UESTC 890 Card Trick(DP 纸牌魔术)