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.
There are multiple cases. Each case contains three integers 1 ≤ m, n, l ≤ 100. Process to the end of file.
For each case, output the probability as irreducible fraction. If it is impossible, output "mukyu~" instead.
7 6 5 7 7 7 7 8 9
187/15552 1/117649 mukyu~
题目大意:有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时,进行DP for(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));//输出最简分数 } } } }