ZOJ-3380 Patchouli's Spell Cards(概率DP&&大数)
来源:互联网 发布:数据加密不属于计算 编辑:程序博客网 时间:2024/05/22 14:19
Patchouli Knowledge, the unmoving great library, is a magician who has settled down in the Scarlet Devil Mansion (紅魔館). Her specialty is elemental magic employing the seven elements fire, water, wood, metal, earth, sun, and moon. So she can cast different spell cards like Water Sign "Princess Undine", Moon Sign "Silent Selene" and Sun Sign "Royal Flare". In addition, she can combine the elements as well. So she can also cast high-level spell cards like Metal & Water Sign "Mercury Poison" and Fire, Water, Wood, Metal & Earth Sign "Philosopher's Stones" .
Assume that there are m different elements in total, each element has n different phase. Patchouli can use many different elements in a single spell card, as long as these elements have the same phases. The level of a spell card is determined by the number of different elements used in it. When Patchouli is going to have a fight, she will choose m different elements, each of which will have a random phase with the same probability. What's the probability that she can cast a spell card of which the level is no less than l, namely a spell card using at least l different elements.
Input
There are multiple cases. Each case contains three integers 1 ≤ m, n, l ≤ 100. Process to the end of file.
Output
For each case, output the probability as irreducible fraction. If it is impossible, output "mukyu~" instead.
Sample Input
7 6 57 7 77 8 9
Sample Output
187/155521/117649mukyu~
References
题目大意:有m种不同的元素,每个元素有n种不同的阶段,只有处于相同阶段的元素才能放入咒语中,求一个咒语中至少有l种不同的元素的概率?
题目大概直译就是这样,但是完全没有思路
看了题解后发现大神总结抽象能力好强,抽象成:有m个位置,在每个位置随机填上1~n个数,求相同的数至少有l的概率?
即使这样还是没有思路,认为是容斥什么的直接计算,完全想不到概率DP
大致思路如下:正难则反,统计填满数后相同的数小于l的情况
设dp[i][j]表示前i个数占据j个位置的方案数(且每个数占据的位置小于l)
该状态可由前i-1个数占据max(j-l+1,0) ~ j个位置转移而来,保证第i个数占据的位置数小于l
则状态转移方程为:dp[i][j]=∑dp[i-1][j-k]*c(m-j+k,k) (k<=j&&k<l)
则∑dp[i][m] 表示用i个数填满后,每个数出现次数都小于ll时的方案数
则n^m-∑dp[i][m] 为相同的数至少有l的的方案数
import java.util.*;import java.math.*;public class Main {static BigInteger[][] dp=new BigInteger[105][105];static BigInteger[][] c=new BigInteger[105][105];static BigInteger fact,deno,gcd;public static void main(String[] argv) {for(int i=0;i<=100;++i) {c[i][0]=c[i][i]=BigInteger.ONE;for(int j=1;j<i;++j) {c[i][j]=c[i-1][j-1].add(c[i-1][j]);//杨辉三角形计算组合数}}Scanner cin=new Scanner(System.in);int m,n,l;while(cin.hasNext()) {m=cin.nextInt();n=cin.nextInt();l=cin.nextInt();if(l>m) {System.out.println("mukyu~");}else if(l>m/2) {//如果至少需要的阶段相同的元素超过一半,则必定只有一种相同的阶段fact=BigInteger.ZERO;deno=BigInteger.valueOf(n).pow(m);//合法的总方案数for(int i=l;i<=m;++i) {//枚举阶段相同的元素个数fact=fact.add(c[m][i].multiply(BigInteger.valueOf(n-1).pow(m-i)));//从m个元素中选择i个元素其阶段相同,其余m-i个元素可在剩下的n-1个阶段任选}fact=fact.multiply(BigInteger.valueOf(n));//不同元素的相同阶段共有n种gcd=fact.gcd(deno);//分子分母的最大公约数System.out.println(fact.divide(gcd)+"/"+deno.divide(gcd));//输出最简分数}else {//存在多种相同的阶段的元素个数都超过l时,进行DPfor(int i=0;i<=n;++i) {for(int j=0;j<=m;++j) {dp[i][j]=BigInteger.ZERO;}}dp[0][0]=BigInteger.ONE;for(int i=1;i<=n;++i) {//枚举阶段for(int j=1;j<=m;++j) {//枚举元素for(int k=0;k<l&&k<=j;++k) {//枚举阶段相同的元素dp[i][j]=dp[i][j].add(dp[i-1][j-k].multiply(c[m-j+k][k]));}}}fact=BigInteger.ZERO;deno=BigInteger.valueOf(n).pow(m);//合法的总方案数for(int i=1;i<=n;++i) {fact=fact.add(dp[i][m]);}fact=deno.subtract(fact);gcd=fact.gcd(deno);//分子分母的最大公约数System.out.println(fact.divide(gcd)+"/"+deno.divide(gcd));//输出最简分数}}}}
- ZOJ-3380 Patchouli's Spell Cards(概率DP&&大数)
- ZOJ 3380 Patchouli's Spell Cards [基础概率DP+大数]
- ZOJ 3380 Patchouli's Spell Cards (概率dp)
- ZOJ 3380 Patchouli's Spell Cards(概率DP)
- ZOJ 3380 Patchouli's Spell Cards(概率DP)
- ZOJ 3380 Patchouli's Spell Cards(DP,大数)
- ZOJ 3380 Patchouli's Spell Cards 概率DP
- Zoj 3380 Patchouli's Spell Cards (概率dp)
- ZOJ 3380 Patchouli's Spell Cards( 概率DP)
- ZOJ 3380 Patchouli's Spell Cards 概率DP
- ZOJ 3380 Patchouli's Spell Cards [基础DP+大数]
- ZOJ3380- Patchouli's Spell Cards(概率DP+计数)
- zoj3380 Patchouli's Spell Cards
- ★ZOJ 3380 Patchouli's Spell Cards 详细题解 (递推+组合数求方案数)
- BNU Cards 概率dp
- ZOJ--3822(概率dp)
- zoj 2852 Deck of Cards扑克牌(三维dp)
- ZOJ 3551 Bloodsucker (概率DP)
- OO设计原则总结
- 毕业设计之android混合模式开发第一天--具有下拉刷新和页面加载等待的WebView搭建
- 第9周深复制体验3
- Tomcat 设置管理员密码
- c++写的简单计算器
- ZOJ-3380 Patchouli's Spell Cards(概率DP&&大数)
- android 用户登陆注册UI设计之 edittext (就是输入用户名和密码的文本框)
- SQL 养成一个好习惯是一笔财富
- 打开命令行的特殊姿势
- vim编辑器入门教程
- leetcode:Coin Change
- poj1053 模拟
- jQuery的选择器
- 【步兵 c++】 多态&虚函数