ZOJ 3380 Patchouli's Spell Cards
方案数,$dp$。
总的方案数有$n^m$种,符合要求的直接算不好算,可以算反面,即不符合要求的。
设$dp[i][j]$表示前$i$种等级填了$j$个位置,那么$dp[i][j]=sum(dp[i-1][j-k]*c[m-(j-k)][k])$。初始化$dp[0][0]=1$。
符合要求的方案数为$n^m-dp[n][m]$。数字会爆$long$ $long$,上$java$。
import java.math.BigInteger;
import java.util.Scanner; public class Main { public static BigInteger GCD(BigInteger a,BigInteger b)
{
if(b.equals(BigInteger.ZERO)) return a;
return GCD(b,a.mod(b));
} public static void main(String [] args)
{
Scanner cin = new Scanner(System.in);
int M,N,L; BigInteger c[][] = new BigInteger[105][105];
BigInteger dp[][] = new BigInteger[105][105]; for(int i=0;i<=100;i++) c[i][0] = c[i][i] = BigInteger.ONE;
for(int i=1;i<=100;i++)
{
for(int j=1;j<i;j++)
{
c[i][j]= c[i-1][j-1].add(c[i-1][j]);
}
for(int j=i+1;j<=100;j++) c[i][j] = BigInteger.ZERO;
} while(cin.hasNext())
{
M=cin.nextInt();
N=cin.nextInt();
L=cin.nextInt(); if(L>M) System.out.println("mukyu~");
else
{ BigInteger n = BigInteger.valueOf(N); BigInteger fm = n.pow(M);
BigInteger fz = new BigInteger("0"); 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=0;j<=M;j++)
{
for(int k=0;k<=L-1;k++)
{
if(j-k<0) continue;
if(M-(j-k)<0) continue; dp[i][j] = dp[i][j].add(dp[i-1][j-k].multiply(c[M-(j-k)][k]));
}
}
} fz=dp[N][M]; fz = fm.subtract(fz);
BigInteger gcd = GCD(fz,fm);
fz=fz.divide(gcd); fm=fm.divide(gcd);
System.out.println(fz+"/"+fm); }
}
}
}
ZOJ 3380 Patchouli's Spell Cards的更多相关文章
- zoj 3380 Patchouli's Spell Cards 概率DP
题意:1-n个位置中,每个位置填一个数,问至少有l个数是相同的概率. 可以转化求最多有l-1个数是相同的. dp[i][j]表示前i个位置填充j个位置的方案数,并且要满足上面的条件. 则: dp[i] ...
- 【ZOJ】3380 Patchouli's Spell Cards
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3957 题意:m个位置,每个位置填1~n的数,求至少有L个位置的数一样的概率(1 ...
- ZOJ-3380 Patchouli’s Spell Cards DP, 组合计数
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3380 题意:有m种不同的元素,每种元素都有n种不同的相位,现在假 ...
- 概率DP
POJ 3744 Scout YYF I 这就是一个乱搞题,暴力发现TLE了,然后看了看discuss里说可以矩阵加速,想了一会才想明白怎么用矩阵,分着算的啊.先算f[num[i]-1]之类的,代码太 ...
- 概率dp专场
专题链接 第一题--poj3744 Scout YYF I 链接 (简单题) 算是递推题 如果直接推的话 会TLE 会发现 在两个长距离陷阱中间 很长一部分都是重复的 我用 a表示到达i-2步的概率 ...
- [转]概率DP总结 by kuangbin
概率类题目一直比较弱,准备把kuangbin大师傅总结的这篇题刷一下! 我把下面的代码换成了自己的代码! 原文地址:http://www.cnblogs.com/kuangbin/archive/20 ...
- CF#335 Board Game
Board Game time limit per test 2.5 seconds memory limit per test 256 megabytes input standard input ...
- POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)
POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...
- zoj 2734 Exchange Cards【dfs+剪枝】
Exchange Cards Time Limit: 2 Seconds Memory Limit: 65536 KB As a basketball fan, Mike is also f ...
随机推荐
- JSON使用(4)
把JSON文本转换为JavaScript对象 JSON最常见的用法之一,是从web服务器上读取JSON数据(作为文件或作为HttpRequest),将JSON数据转换为JavaScript对象,然后在 ...
- [SCOI2010]序列操作 线段树
---题面--- 题解: 在考场上打的这道题,出人意料的很快就打完了?! 直接用线段树,维护几个东西: 1,lazy标记 : 表示区间赋值 2,mark标记:表示区间翻转 3,l1:前缀最长连续的1的 ...
- sublime text : The emmet plugin doesn't work when tab key was pressed
Today, I switched my sublime text to version 3. And then I found that the emmet plugin doesn't work ...
- 【BZOJ 2553】[BeiJing2011]禁忌 AC自动机+期望概率dp
我一开始想的是倒着来,发现太屎,后来想到了一种神奇的方法——我们带着一个既有期望又有概率的矩阵,偶数(2*id)代表期望,奇数(2*id+1)代表概率,初始答案矩阵一列,1的位置为1(起点为0),工具 ...
- nc用法小记
By francis_hao Jun 30,2017 ncat:连接和重定向套接字 概要 ncat [OPTIONS...] [hostname] [port] 描述 ncat 是一个集 ...
- 从零开始学习MXnet(四)计算图和粗细粒度以及自动求导
这篇其实跟使用MXnet的关系不大,但对于我们理解深度学习的框架设计还是很有帮助的. 首先还是对promgramming models的一个简单介绍,这个东西实际上是在编译里面经常出现的东西,我们在编 ...
- JSOI2004 平衡点 / 吊打XXX [模拟退火]
题目描述 如图:有n个重物,每个重物系在一条足够长的绳子上.每条绳子自上而下穿过桌面上的洞,然后系在一起.图中X处就是公共的绳结.假设绳子是完全弹性的(不会造成能量损失),桌子足够高(因而重物不会垂到 ...
- Codeforces Round #487 (Div. 2) A Mist of Florescence (暴力构造)
C. A Mist of Florescence time limit per test 1 second memory limit per test 256 megabytes input stan ...
- poj 2104 (划分树模板)
Description You are working for Macrohard company in data structures department. After failing your ...
- eclipse 主题文件配置
eclipse市场搜索 Eclipse Color Theme ----用于控制文本域主题 Eclipse 4 Chrome Theme chrome风格的主题 最新的:Jeeeyul's Them ...