lightoj 1064 动态规划 爱被打了一巴掌 2022-07-28 01:09 129阅读 0赞 1064 - Throwing Dice <table> <tbody> <tr> <td title="normal judge"><a href="http://lightoj.com/volume_submit.php?problem=1064" rel="nofollow"><img src="http://lightoj.com/images/submit1.png" alt=""> </a> <a href="http://udebug.com/LOJ/1064" rel="nofollow"><img src="http://lightoj.com/images/udebug.png" alt=""> </a></td> <td><a href="http://lightoj.com/volume_showproblem.php?problem=1064&language=english&type=pdf" rel="nofollow">PDF (English) </a></td> <td><a href="http://lightoj.com/volume_problemstat.php?problem=1064" rel="nofollow">Statistics </a></td> <td><a href="http://lightoj.com/forum_showproblem.php?problem=1064" rel="nofollow">Forum </a></td> </tr> </tbody> </table> <table> <tbody> <tr> <td>Time Limit: <span style="color:#B45F04">2 second(s)</span></td> <td>Memory Limit: <span style="color:#B45F04">32 MB</span></td> </tr> </tbody> </table> **n** common cubic dice are thrown. What is theprobability 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. <table> <tbody> <tr> <td> <h1>Sample Input</h1> </td> <td> <h1>Output for Sample Input</h1> </td> </tr> <tr> <td> <p>7</p> <p>3 9</p> <p>1 7</p> <p>24 24</p> <p>15 76</p> <p>24 143</p> <p>23 81</p> <p>7 38</p> </td> <td> <p>Case 1: 20/27</p> <p>Case 2: 0</p> <p>Case 3: 1</p> <p>Case 4: 11703055/78364164096</p> <p>Case 5: 25/4738381338321616896</p> <p>Case 6: 1/2</p> <p>Case 7: 55/46656</p> </td> </tr> </tbody> </table> 题意: 有n个骰子,把他们扔出去, 求朝上的面的数的和相加至少是x 的概率是多少; 分析: 很有意思的题目, 有n个骰子, 总的情况肯定就是6的n次方. 然后求符合条件的情况; 我们用dp来求解这个情况, 用dp\[i\]\[j\] 来表示扔第i个骰子总和为j的数目, 那么就会有 dp\[i\]\[j\] = dp\[i-1\]\[j-k\] (1<=k<=6); #include<bitset> #include<map> #include<vector> #include<cstdio> #include<iostream> #include<cstring> #include<string> #include<algorithm> #include<cmath> #include<stack> #include<queue> #include<set> #define inf 0x3f3f3f3f #define mem(a,x) memset(a,x,sizeof(a)) #define F first #define S second using namespace std; typedef unsigned long long ll; typedef pair<int,int> pii; inline int in() { int res=0;char c;int f=1; while((c=getchar())<'0' || c>'9')if(c=='-')f=-1; while(c>='0' && c<='9')res=res*10+c-'0',c=getchar(); return res*f; } const int N=100010; int n,x; ll dp[25][151]; ll gcd(ll a,ll b) { return b? gcd(b,a%b):a; } ll power(ll a,int n) { ll ret=1; while(n) { if(n & 1) ret *= a; a *= a; n>>=1; } return ret; } int main() { int T=in(),ca=1; while(T--) { n=in(),x=in(); mem(dp,0); for(int i=1;i<=6;i++) dp[1][i]=1; for(int i=1;i<n;i++) { for(int j=1;j<=150;j++) { if(!dp[i][j]) continue; for(int k=1;k<=6;k++) { dp[i+1][j+k] += dp[i][j]; } } } ll all=power(6,n),t=0; for(int i=x;i<=150;i++) { t += dp[n][i]; } if(t==all) printf("Case %d: 1\n",ca++); else if(t==0) printf("Case %d: 0\n",ca++); else{ ll g=gcd(t,all); printf("Case %d: %llu/%llu\n",ca++,(t/g),(all/g)); } } return 0; }
相关 动态规划 一:概念: 和分冶法相似,不同之处在于前者是把问题划分为互不相交的子问题,而动态规划会出现子问题重叠的情况。通常用来求解最优化问题。。 基本步骤: ①:刻画一个最优 我就是我/ 2022年12月01日 11:51/ 0 赞/ 73 阅读
相关 动态规划 -------------------- 动态规划的设计思想 动态规划(DP)\[1\]通过分解成子问题解决了给定复杂的问题,并存储子问题的结果,以避免再次计算相同的结 柔情只为你懂/ 2022年09月25日 11:15/ 0 赞/ 291 阅读
相关 动态规划 作者:Hawstein 出处: [http://hawstein.com/posts/dp-novice-to-advanced.html][http_hawstein.c 淡淡的烟草味﹌/ 2022年09月24日 12:17/ 0 赞/ 246 阅读
相关 动态规划 1. 首先,动态规划不是一个特定的算法,它代表的是一种思想,一种手段 2. 动态规划方法往往用于求解“最优化问题”,能用动态规划求解的问题的前提是问题具有“最优子结构性质” 刺骨的言语ヽ痛彻心扉/ 2022年07月29日 11:46/ 0 赞/ 145 阅读
相关 lightoj 1064 动态规划 1064 - Throwing Dice <table> <tbody> <tr> <td title="normal judge"><a href="ht 爱被打了一巴掌/ 2022年07月28日 01:09/ 0 赞/ 130 阅读
相关 动态规划 首先是概念:动态规划就是将原问题划分为简单的几个小问题(有点类似与分治法?但是分治法中的各个小问题是相互独立的,彼此之间不会产生影响,但是动态规划中所得的值和前面相关,当前的解 青旅半醒/ 2022年07月12日 05:01/ 0 赞/ 362 阅读
相关 动态规划 基本思想: 动态规划算法与分治法类似,其基本思想也是将待求解问题分解成若干个子问题,先求解子问题,然后从这些子问题的解得到原问题的解。 区别: 与 ゞ 浴缸里的玫瑰/ 2022年05月25日 09:44/ 0 赞/ 483 阅读
相关 动态规划 等我有时间了,一定要把《算法导论》啃完,这本书的印刷质量实在太好了,就是烧脑子,滑稽。 适合应用动态规划求解的最优化问题应该具备两个要素: 最优子结构:一个问题的最优解包含 短命女/ 2022年05月17日 09:45/ 0 赞/ 381 阅读
相关 动态规划 1.题目:最长递增子序列(Longest Increasing Subsequence) 问题描述: > 给定长度为N的数组A,计算A的最长单调递增的子序列(不一定连续) 妖狐艹你老母/ 2022年05月11日 04:56/ 0 赞/ 324 阅读
相关 动态规划 算法题中动态规划是真的难,写一篇总结,彻底解决动态规划。参考:[https://blog.csdn.net/u013309870/article/details/7519359 左手的ㄟ右手/ 2021年11月19日 14:14/ 0 赞/ 556 阅读
还没有评论,来说两句吧...