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

探讨了在给定元素数量和相位变化的情况下,计算能够施放出特定等级符卡的概率问题。通过反向思维计算不可行方案数,最终得出可用符卡的概率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 5
7 7 7
7 8 9
Sample Output
187/15552
1/117649
mukyu~
 
跟:http://blog.youkuaiyun.com/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));
						
		}
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值