ZOJ 3380 Patchouli's Spell Cards( 概率DP)
题意:用n个数填充m个位置,问有大于等于l个相同数字的概率。
这个题看了很多题解,大部分代码都是有点问题的,一开始dp的状态都是一样的。(具体见下)
思路:设dp[i][j] 为用前i个数填充j个位置的方案数。
则dp[i][j] = sigma(dp[i-1][j-k] * C[m-j+k][k]) (k >= 0 && k < l && k <= j)。
最后的结果应该是(n^m - dp[n][m]) / n^m。
注意这里,结果不是(n^m - dp[1~n][m]) / n^m。
但至于为什么都能AC,原因在于大部分题解的j是从1开始循环的,在计算的过程中会有情况漏掉,因为从1循环,dp[i][0] = 0,但把1~n取和后正好补全了,不过为什么能恰好补全,这个不是很好想。
正确的应该是j从0开始取,这样对应的每个dp[i][0]都为1。
我的代码:
import java.math.*;
import java.util.*;
import java.io.*;public class Main{static BigInteger[][] dp = new BigInteger[105][105];static BigInteger[][] C = new BigInteger[105][105];public static void main(String args[]){Scanner cin = new Scanner(new BufferedInputStream(System.in));for(int i=0; i < 105 ;i++){C[i][0] = C[i][i] = BigInteger.ONE;for(int j = 1; j < i ; j++)C[i][j] = C[i-1][j].add(C[i-1][j-1]);}int n,m,l;while(cin.hasNext()){m = cin.nextInt();n = cin.nextInt();l = cin.nextInt();BigInteger tol = BigInteger.valueOf(n).pow(m);if(l > m){System.out.println("mukyu~");}else{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 <= j && k < l ; k++)dp[i][j] = dp[i][j].add(dp[i-1][j-k].multiply(C[m-j+k][k]));}System.out.println(dp[n][m]);BigInteger ans = tol.subtract(dp[n][m]);BigInteger con = ans.gcd(tol);System.out.println(ans.divide(con)+"/"+tol.divide(con));}} }
}
ZOJ 3380 Patchouli's Spell Cards( 概率DP)相关推荐
- ZOJ 3380 Patchouli's Spell Cards 概率DP
题意:给你m个位置,每个位置放一个数,区间为1-n,问你至少有L个位置是一样的数的概率,结果用分数表示 思路:这题我们从反向入手,dp[i][j]代表前i个数,放到j个位置,且没有L个或以上的位置有相 ...
- ZOJ 3380 Patchouli's Spell Cards [基础DP+大数]
Description Patchouli Knowledge, the unmoving great library, is a magician who has settled down in t ...
- ZOJ 3380 Patchouli's Spell Cards(DP,大数)
转载请注明出处,谢谢http://blog.csdn.net/acm_cxlove/article/details/7854526 by---cxlove 题目:有m个位置,每个位置填入一 ...
- ZOJ 3380 Patchouli's Spell Cards(概率+大数)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3957 题意:m个位置,每个位置可以放n种数字(1-n).问至少有L个 ...
- ZOJ 3380 Patchouli's Spell Cards(概率DP)
Patchouli's Spell Cards Time Limit: 7 Seconds Memory Limit: 65536 KB Patchouli Knowledge, the u ...
- ZOJ 3380 Patchouli's Spell Cards [基础概率DP+大数]
Patchouli's Spell Cards Time Limit: 7 Seconds Memory Limit: 65536 KB Patchouli Knowledge, the u ...
- ★ZOJ 3380 Patchouli's Spell Cards 详细题解 (递推+组合数求方案数)
Patchouli's Spell Cards Time Limit: 7 Seconds Memory Limit: 65536 KB Patchouli Knowledge, the u ...
- 【概率DP】 ZOJ 3380 Patchouli's Spell Cards
通道 题意:有m个位置,每个位置填入一个数,数的范围是1~n,问至少有L个位置的数一样的概率 思路: 总数是n^m,我们求没有L个位置一样的数的概率* 设 dp[i][j]表示用前i个数,填充j个位置 ...
- ZOJ 3380 Patchouli's Spell Cards——组合数+概率dp
题意: m个位置,每个位置可以等概率填n种数,规定一个数字出现次数不能超过L次(注意不是连续L次),填完m个位置后满足约束的概率是多少. 思路: 定义dp[i][j]为前i种数字填充j个位置(j个位置 ...
最新文章
- P4619 [SDOI2018]旧试题(莫比乌斯反演,建图优化三重枚举,三元环计数,神仙好题,超级清晰易懂)
- BDD100K:最经典大规模、多样化的自动驾驶视频数据集
- 物联网通信协议全解析
- scrollview中嵌套listview产生冲突问题
- maven spring profile 协同
- tomcat linux dump,Linux下Tomcat常用命令与配置
- 【图文详解】第一个Java程序HelloWorld详解,写Java程序需要注意什么
- 关于 SAP Spartacus 电商云 UI feature level 的测试步骤
- 演练 五家限购专卖店
- 【Linux】【MySQL】CentOS7安装最新版MySQL8.0.13(最新版MySQL从安装到运行)
- SQL Server里的自旋锁介绍
- redhat solutions
- Eclipse手动添加dtd文件使xml能自动提示
- 获取人脸图片和训练人脸
- psp能装安卓软件吗_psp移植手机游戏大全 安卓手机玩psp游戏排行榜
- mw150r 虚拟服务器,水星MW150R V1.3路由器刷DD-WRT使用WIWIZ或wifiap做web认证登录
- qq音乐mp3解析php源码,QQ音乐PHP解析源码
- 2022 年排名前 10 的聊天机器人[示例]
- Live800:在线客服系统排名是怎么样的?
- 保险私有云 IaaS 资源池选型与演进之路 | SmartX 客户实践