ZOJ 3380 Patchouli's Spell Cards(DP,大数)
2012-10-01 13:20
477 查看
转载请注明出处,谢谢/article/2566293.html
by---cxlove
题目:有m个位置,每个位置填入一个数,数的范围为1-n,问至少有i个位置的数字一样的概率为多少
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3957
完全就是仿造zeroclock的做法的。。。主要是练习J***A
至少有i个位置一样,也就是i+1,i+2,……m个位置一样,不好处理
我们知道总的方案数,n^m也就是分母
可以dp处理有没有i个位置的数字一样的概率
dp[i][j]表示当前已经放了j个位置,用到了第i种颜色
即dp[i][j]=dp[i-1][j-k]*C(m-(j-k),k) (k<=j&&k<l) 表示我们找出k个位置填入数字i
因为如果l>m则必然是无解的,l>m/2可以直接用组合计数算出,时间减少一半
数字很大,也没有取模,需要大数处理
by---cxlove
题目:有m个位置,每个位置填入一个数,数的范围为1-n,问至少有i个位置的数字一样的概率为多少
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3957
完全就是仿造zeroclock的做法的。。。主要是练习J***A
至少有i个位置一样,也就是i+1,i+2,……m个位置一样,不好处理
我们知道总的方案数,n^m也就是分母
可以dp处理有没有i个位置的数字一样的概率
dp[i][j]表示当前已经放了j个位置,用到了第i种颜色
即dp[i][j]=dp[i-1][j-k]*C(m-(j-k),k) (k<=j&&k<l) 表示我们找出k个位置填入数字i
因为如果l>m则必然是无解的,l>m/2可以直接用组合计数算出,时间减少一半
数字很大,也没有取模,需要大数处理
import java.util.*; import java.math.*; 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(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-1].add(c[i-1][j]); } } int m,n,l; while(cin.hasNext()) { m=cin.nextInt(); n=cin.nextInt(); l=cin.nextInt(); BigInteger total=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(total); System.out.println(ans.divide(gcd)+"/"+total.divide(gcd)); continue; } 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<l&&k<=j;k++) { dp[i][j]=dp[i][j].add(dp[i-1][j-k].multiply(c[m-(j-k)][k])); } } } BigInteger ans=new BigInteger("0"); for(int i=1;i<=n;i++) { ans=ans.add(dp[i][m]); } ans=total.subtract(ans); BigInteger gcd=ans.gcd(total); System.out.println(ans.divide(gcd)+"/" + total.divide(gcd)); } } }
相关文章推荐
- ZOJ-3380 Patchouli's Spell Cards(概率DP&&大数)
- ZOJ 3380 Patchouli's Spell Cards [基础DP+大数]
- ZOJ 3380 Patchouli's Spell Cards( 概率DP)
- ZOJ 3380 Patchouli's Spell Cards (概率dp)
- Zoj 3380 Patchouli's Spell Cards (概率dp)
- ZOJ3380- Patchouli's Spell Cards(概率DP+计数)
- ZOJ 3380 Patchouli's Spell Cards 概率DP
- ZOJ 3380 Patchouli's Spell Cards [基础概率DP+大数]
- ZOJ 3380 Patchouli's Spell Cards 概率DP
- ZOJ 3380 Patchouli's Spell Cards(概率+大数)
- ZOJ 3380 Patchouli's Spell Cards(概率DP)
- ZOJ 3380 Patchouli's Spell Cards(概率DP)
- zoj 3380 Patchouli's Spell Cards 概率DP
- 【ZOJ】3380 Patchouli's Spell Cards
- poj 2411 && zoj 1100 Mondriaan's Dream ———状态压缩dp
- ZOJ 3644 Kitty's Game(状态简化 & DP)
- ZOJ 3644 Kitty's Game(DP)
- ZOJ 1108 FatMouse's Speed (HDU 1160) DP
- ★ZOJ 3380 Patchouli's Spell Cards 详细题解 (递推+组合数求方案数)
- ZOJ 3644 Kitty's Game(DP)