ZOJ3380_Patchouli's Spell Cards_概率DP
生活随笔
收集整理的這篇文章主要介紹了
ZOJ3380_Patchouli's Spell Cards_概率DP
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目大意
用n個(gè)數(shù)填m個(gè)位置,每個(gè)位置上出現(xiàn)每個(gè)數(shù)的概率相同,求存在一個(gè)數(shù)再序列中出現(xiàn)的次數(shù) >=l 的概率。
思路
求概率轉(zhuǎn)化為求 ? ->? 符合要求的序列的數(shù)量 / 總序列數(shù)量 -> ? (總序列數(shù)量 - 不符合要求的序列的數(shù)量) /?總序列數(shù)量
不符合要求的序列的數(shù)量即序列中每個(gè)數(shù)出現(xiàn)不超過l
dp[i][j] 表示用前i個(gè)數(shù)添m個(gè)位置上的任意j個(gè)位置的情況數(shù)量
dp[i][j] = Σdp[i - 1][j - k] * C( m - (j - k), k),? k >= 0 && k <= j && k < l.
邊界條件: dp[i][0] = 1,? i >= 0 && i <= n.
則 ans = ( n^m - dp[n][m] ) / n^m
import java.awt.List; import java.math.BigInteger; import java.util.Scanner; public class Main {static int MAXN = 110;static BigInteger[][] comb = new BigInteger[MAXN][MAXN];static BigInteger[][] dp = new BigInteger[MAXN][MAXN];public static void main(String[] args){int n, m, l;for (int i = 0; i < MAXN; i++){comb[i][0] = comb[i][i] = BigInteger.valueOf(1);for (int j = 1; j < i; j++){comb[i][j] = comb[i - 1][j].add(comb[i - 1][j - 1]);}}Scanner in = new Scanner(System.in);while (in.hasNext()){m = in.nextInt();n = in.nextInt();l = in.nextInt();if (l > m){System.out.println("mukyu~");continue;}for (int i = 0; i <= n; i++)for (int j = 0; j <= m; j++)if (j == 0)dp[i][j] = BigInteger.ONE;elsedp[i][j] = BigInteger.ZERO;for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++)for (int k = 0; k <= j && k < l; k++)dp[i][j] = dp[i][j].add(dp[i - 1][j - k].multiply(comb[m - (j - k)][k]));BigInteger tmp = BigInteger.valueOf(n).pow(m);BigInteger a = tmp.subtract(dp[n][m]);BigInteger s = a.gcd(tmp);System.out.println(a.divide(s) + "/" + tmp.divide(s));}}}
import java.awt.List; import java.math.BigInteger; import java.util.Scanner; public class Main {static int MAXN = 110;static BigInteger[][] comb = new BigInteger[MAXN][MAXN];static BigInteger[][] dp = new BigInteger[MAXN][MAXN];public static void main(String[] args){int n, m, l;for (int i = 0; i < MAXN; i++){comb[i][0] = comb[i][i] = BigInteger.valueOf(1);for (int j = 1; j < i; j++){comb[i][j] = comb[i - 1][j].add(comb[i - 1][j - 1]);}}Scanner in = new Scanner(System.in);while (in.hasNext()){m = in.nextInt();n = in.nextInt();l = in.nextInt();if (l > m){System.out.println("mukyu~");continue;}for (int i = 0; i <= n; i++)for (int j = 0; j <= m; j++)if (j == 0)dp[i][j] = BigInteger.ONE;elsedp[i][j] = BigInteger.ZERO;for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++)for (int k = 0; k <= j && k < l; k++)dp[i][j] = dp[i][j].add(dp[i - 1][j - k].multiply(comb[m - (j - k)][k]));BigInteger tmp = BigInteger.valueOf(n).pow(m);BigInteger a = tmp.subtract(dp[n][m]);BigInteger s = a.gcd(tmp);System.out.println(a.divide(s) + "/" + tmp.divide(s));}}}
總結(jié)
以上是生活随笔為你收集整理的ZOJ3380_Patchouli's Spell Cards_概率DP的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Xilinx FFT IP使用总结
- 下一篇: ConcurrentHashMap的实现