LIGHT OJ 1064 - Throwing Dice 【dp数塔+打表】
来源:互联网 发布:淘宝网能不能微信支付 编辑:程序博客网 时间:2024/05/16 01:57
1064 - Throwing Dice
PDF (English)StatisticsForum
Time Limit: 2 second(s)Memory Limit: 32 MB
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
Output for Sample Input
7
3 9
1 7
24 24
15 76
24 143
23 81
7 38
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
题意:给你n个塞子,问你置处所有的点数之和大于x的概率;
思路:每个塞子6种可能,最多24个塞子,直接枚举一遍肯定不行,那就需要优化时间,中间状态枚举了多次,这是没有必要的,只枚举一次就够了,下一次用到时不需要在枚举相同的了,所以我们应该将之前枚举过的状态都记录下来,方便下次访问直接读取,时间得到优化,因为题目要求概率,N个筛子的出现的情况有6^N中,只要找到某一点数出现的次数即可,dp[i][j]:前i个筛子总点数为j的方案数,dp[i]j]=dp[i-1][j-1]+.....+dp[i-1][j-6],查询的结果在一个集合之中,集合不变,可以打一个表预处理,输入直接查询即可;
失误:想麻烦了,想着用要求条件比较多,于是设了一个三维的数组,没戏了,一个三维数组处理起来好麻烦,一会就把自己搞晕了,记住吧:轻易不要用三维的最多用二维,能简单表示别麻烦,麻烦了不好弄;
AC代码:
#include<cstdio>#include<cstring>using namespace std;typedef long long LL;LL dp[27][24*7],p[26];void init(){LL i=0,j=0,k=0;memset(dp,0,sizeof(dp));for(i=1;i<=6;++i) dp[1][i]=1;for(i=2;i<=24;++i){for(j=i;j<=i*6;++j){for(k=1;k<=6;++k){dp[i][j]+=dp[i-1][j-k];}} } p[0]=1; for(i=1;i<=24;++i) p[i]=p[i-1]*6;}LL Gcd(LL a,LL b){return !b? a:Gcd(b,a%b);}int main(){init(); LL T,N,X,i,ans,Kase=0;scanf("%lld",&T);while(T--){scanf("%lld %lld",&N,&X);ans=0;for(i=X;i<=N*6;++i){ans+=dp[N][i];}printf("Case %lld: ",++Kase);if(!ans) printf("0\n");else {if(N>=X) printf("1\n");else{LL tem=p[N];LL d=Gcd(tem,ans);ans/=d; tem/=d;printf("%lld/%lld\n",ans,tem);} }}return 0; }
0 0
- LIGHT OJ 1064 - Throwing Dice 【dp数塔+打表】
- Light oj 1064 Throwing Dice (概率dp)
- Light OJ 1064 Throwing Dice(简单DP)
- 【LightOJ】1064 - Throwing Dice(dp打表)
- lightoj1064 - Throwing Dice【dp打表】
- LightOJ 1064 Throwing Dice (DP)
- 【LightOJ 1064 】Throwing Dice + dp
- lightoj - 1064 - Throwing Dice - dp / 概率dp
- light oj 1145 - Dice (I) DP计数
- Light OJ 1248 - Dice (III) 概率DP
- Light OJ 1145 Dice (I) (DP)
- Light OJ 1193 Dice (II) (DP)
- LightOJ 1064 Throwing Dice 概率DP
- LightOJ 1064 - Throwing Dice (dp)
- LightOJ 1064 - Throwing Dice(概率DP)
- LightOj 1064 Throwing Dice(概率dp)
- lightoj 1064 - Throwing Dice 简单DP
- Light OJ 1317 Throwing Balls into the Baskets 概率DP
- 占位
- 占位
- Java比较器接口的使用
- 基于Node.js的Java虚拟机:node-jvm
- 如何用手机刷开门禁
- LIGHT OJ 1064 - Throwing Dice 【dp数塔+打表】
- C# FrameWork版本兼容 App.config配置列表
- 模式之观察者模式
- ListView优化
- asp.net 关于CodeFile和CodeBehind
- 浅谈C#的垃圾回收-关于GC、析构函数、Dispose、and Finalize
- 自定义org.apache.commons.beanutils的类型转换器
- IIS无法正常运行解析.net4.0的错误问题
- 在digitalocean中的CentOS7.2系统上搭建JDK&Tomcat详细步骤