ZOJ3380_Patchouli's Spell Cards_概率DP
生活随笔
收集整理的這篇文章主要介紹了
ZOJ3380_Patchouli's Spell Cards_概率DP
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目大意
用n個數填m個位置,每個位置上出現每個數的概率相同,求存在一個數再序列中出現的次數 >=l 的概率。
思路
求概率轉化為求 ? ->? 符合要求的序列的數量 / 總序列數量 -> ? (總序列數量 - 不符合要求的序列的數量) /?總序列數量
不符合要求的序列的數量即序列中每個數出現不超過l
dp[i][j] 表示用前i個數添m個位置上的任意j個位置的情況數量
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));}}}
總結
以上是生活随笔為你收集整理的ZOJ3380_Patchouli's Spell Cards_概率DP的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Xilinx FFT IP使用总结
- 下一篇: ConcurrentHashMap的实现