★ZOJ 3380 Patchouli's Spell Cards 详细题解 (递推+组合数求方案数)

来源:互联网 发布:啥叫大数据 编辑:程序博客网 时间:2024/05/16 03:19

Patchouli's Spell Cards

Time Limit: 7 Seconds      Memory Limit: 65536 KB

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 ≤ mnl ≤ 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~
跟:http://blog.csdn.net/qq_34374664/article/details/65935929对比理解下

 题目意思:有m个位置,每个位置填入一个数,数的范围是1~n,问至少有L个位置的数一样的概率
 输出要是最简分数的形式,所以用大数JAVA

大致思路:

首先如果考虑有L, L + 1, .... m个位置上是一样的方案数不好考虑, 但是可以从反面考虑, 计算只有1, 2, ... L - 1个位置有相同元素的方案数, 用总方案数n^m减去即可

如果用dp[i][j]表示用前i种元素填了j个位置(不一定是前j个), 且每个元素都没有使用达到L个, 的方案数的话有

dp[i][j] = sigma(dp[i - 1][j - k]*C[m - (j - k)][k]) (0 <= k <= min(m, l))

那么最后的答案就是 (n^m-dp[1~n][m])/(n^m)

即用前i个元素填充j个位置的方案数相当于前i - 1个元素填充了j - k个位置, 第i个元素要填充k个位置, 这k个位置可以来自于剩下的m - (j - k)个位置中的其中k个

那么不满足题意的方案总数是 sigma(dp[i][m]) (1 <= i <= n) 用所有可能的排列方案减去即可得到可行的方案数.

初始化dp[0][0] = 1, 其他值为0即可,这样有些不符合的情况自动变成0了

很明显只有当l > m时才会没有可能的方案

考虑到计算时数据很大, 使用java方便一些

讲道理 java真的是劲啊。。。 

import java.math.BigInteger;import java.util.BitSet;import java.util.Scanner;public class Main{static BigInteger[][] dp = new BigInteger[110][110];static BigInteger[][] c = new BigInteger[110][110];public static void main(String[] args){Scanner sc = new Scanner(System.in);for(int i = 0; i < 105; i++){c[i][0] = c[i][i] = BigInteger.valueOf(1);for(int j = 1; j < i; j++)c[i][j] = c[i-1][j-1].add(c[i-1][j]);}int n, m, l;while(sc.hasNext()){m = sc.nextInt();n = sc.nextInt();l = sc.nextInt();BigInteger tot = BigInteger.valueOf(n).pow(m);if(l > m){System.out.println("mukyu~");continue;}//if(l>m/2)//这个时候可以直接用组合数求出来//            {//                BigInteger ans=BigInteger.ZERO;//                for(int i=l;i<=m;i++)//                    ans=ans.add(c[m][i].multiply(BigInteger.valueOf(n-1).pow(m-i)));//                ans=ans.multiply(BigInteger.valueOf(n));//                BigInteger gcd=ans.gcd(tot);//                System.out.println(ans.divide(gcd)+"/"+tot.divide(gcd));//                continue;//            }for(int i = 0; i <= n; i++)for(int j = 0; j <= m; j++)dp[i][j] = BigInteger.valueOf(0);dp[0][0] = BigInteger.ONE;  //0个元素房0个位置的概率肯定是1 啊for(int i = 1; i <= n; i++)  //n个元素for(int j = 1; j <= m; j++)  //j个位置,不一定连续for(int k = 0; k < l && k <= j; k++) //代表第i个元素不放,房1个,放两个。。。最多放k个dp[i][j] = dp[i][j].add(dp[i-1][j-k].multiply(c[m-(j-k)][k]));//System.out.println(c[4][2]);BigInteger ans = BigInteger.ZERO;for(int i = 1; i <= n; i++)ans = ans.add(dp[i][m]);ans = tot.subtract(ans);BigInteger gcd = ans.gcd(tot);System.out.println(ans.divide(gcd)+"/"+tot.divide(gcd));}}}





0 0