Throwing Dice(概率dp)

C - Throwing Dice

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

LightOJ 1064 uDebug

Description

n common cubic dice are thrown. What is the probability that the sum of all thrown dice is at least x?

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each test case contains two integers n (1 ≤ n < 25) and x (0 ≤ x < 150). The meanings of n and x are given in the problem statement.

Output

For each case, output the case number and the probability in ‘p/q‘ form where p and q are relatively prime. If q equals 1 then print p only.

Sample Input

7

3 9

1 7

24 24

15 76

24 143

23 81

7 38

Sample Output

Case 1: 20/27

Case 2: 0

Case 3: 1

Case 4: 11703055/78364164096

Case 5: 25/4738381338321616896

Case 6: 1/2

Case 7: 55/46656

//比赛没看懂题,这是一个简单概率dp,第一行是案例数 T ,然后 n , m 是n个骰子掷出至少为 m 的概率。

dp[i][j]代表 i 个骰子,掷出 j 种数

dp[i+1][j+k]=sigma(dp[i][j]) (1<=k<=6)

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 using namespace std;
 5
 6 __int64 dp[30][155];
 7
 8 __int64 gcd(__int64 x,__int64 y)
 9 {
10     return y?gcd(y,x%y):x;
11 }
12
13 int main()
14 {
15     __int64 up,down,g;
16     int T,n,x,i,j,k;
17
18     for(i=1;i<=6;i++)//一个骰子
19         dp[1][i]=1;
20     for(i=1;i<25;i++)//骰子
21     {
22         for (j=i;j<=6*i;j++)//这个骰子所有的点数
23         {
24             for (k=1;k<=6;k++)//下个骰子的点数
25                 dp[i+1][j+k]+=dp[i][j];
26         }
27     }
28
29     scanf("%d",&T);
30     for(int cas=1;cas<=T;cas++)
31     {
32         scanf("%d%d",&n,&x);
33         if(x>n*6)
34         {
35             printf("Case %d: 0\n",cas);
36             continue;
37         }
38         if(x<=n)
39         {
40             printf("Case %d: 1\n",cas);
41             continue;
42         }
43         up=0,down=1;
44         for(i=x;i<=n*6;i++)
45             up+=dp[n][i];
46         for(j=0;j<n;j++) down*=6;//所有骰子可能的情况
47         g=gcd(up,down);
48         printf("Case %d: %I64d/%I64d\n",cas,up/g,down/g);
49     }
50     return 0;
51 }

时间: 2024-10-11 13:27:01

Throwing Dice(概率dp)的相关文章

HDU 4599 Dice (概率DP+数学+快速幂)

题意:给定三个表达式,问你求出最小的m1,m2,满足G(m1) >= F(n), G(m2) >= G(n). 析:这个题是一个概率DP,但是并没有那么简单,运算过程很麻烦. 先分析F(n),这个用DP来推公式,d[i],表示抛 i 次连续的点数还要抛多少次才能完成.那么状态转移方程就是 d[i] = 1/6*(1+d[i+1]) + 5/6*(1+d[1]), 意思就是说在第 i 次抛和上次相同的概率是1/6,然后加上上次抛的和这一次,再加上和上次不同的,并且又得从第1次开始计算. 边界就是

LightOJ1064 Throwing Dice(DP)

第一眼以为是概率DP,我还不会.不过看题目那么短就读读,其实这应该还不是概率DP,只是个水水的DP.. dp[n][s]表示掷n次骰子点数和为s的情况数 dp[0][0]=1 dp[i][j]=∑dp[i-1][j-k] (k∈[1,6] 且 j-k>=0) 要求的概率就是情况数/掷n次骰子的总情况数,掷n次骰子总情况数就是6n. 最后的结果分子分母要互质,而分母的质因子只有2和3,所以只要同除以2和3就能化简. 1 #include<cstdio> 2 using namespace

HDU 4652 Dice (概率DP)

B - Dice Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4652 Description You have a dice with m faces, each face contains a distinct number. We assume when we tossing the dice, each face will o

HDU 4652 Dice (概率DP)

Dice Problem Description You have a dice with m faces, each face contains a distinct number. We assume when we tossing the dice, each face will occur randomly and uniformly. Now you have T query to answer, each query has one of the following form: 0

Light OJ 1317 Throwing Balls into the Baskets 概率DP

?n个人 m个篮子 每一轮每一个人能够选m个篮子中一个扔球 扔中的概率都是p 求k轮后全部篮子里面球数量的期望值 依据全期望公式 进行一轮球数量的期望值为dp[1]*1+dp[2]*2+...+dp[n]*n 记为w 当中dp[i]为i个人扔中的概率 dp[i] = C(n, i)*p^i*(1-p)^(n-i) 终于答案为w*k #include <cstdio> #include <cstring> using namespace std; double dp[20]; dou

LightOJ1030 Discovering Gold 概率DP 水题

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Description You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell of the cave can contain any amount of gold. Initially you are in position 1.

hdu4405概率dp入门

Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1122    Accepted Submission(s): 762 Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids lab

Aeroplane chess(简单概率dp)

Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to N. Hzz starts at grid 0. For each step he throws a dice(a dice have six faces with equal probability to face up and the numbers on the faces are 1,2,3,4,5,6). Whe

HDU4405-Aeroplane chess(概率DP求期望)

Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1182    Accepted Submission(s): 802 Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids lab