转载请注明出处,谢谢http://blog.csdn.net/acm_cxlove/article/details/7854526
by---cxlove
题目:给出N个房间,每个房间的钥匙随机放在某个房间内,概率相同。有K次炸门的机会,求能进入所有房间的可能性为多大。
http://acm.hdu.edu.cn/showproblem.php?pid=3625
钥匙与门的对应关系呈现出环。打开一个门之后,环内的所有房间都可以进入。也就是说N个房间形成1--K个环的可能有多大。N个房间N个钥匙的总数为N!。
之后是求N个房间形成i个环的总数。
题目还有个特殊要求,不能破1号的门。
也就是说1号不能独立成环,否则就失败。
第一类斯特林数S(P,K)=(P-1)*S(P-1,K)+S(P-1,K-1)表示的正是N个元素形个K个非空循环排列的方法数。
枚举形成的环,但是要除掉1号独立成环的可能。
S(N,M)-S(N-1,M-1),N个元素形成 M个环,减去除了1之外的N-1个元素形成M-1个环,也就是1独立成环。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define eps 1e-7
#define LL long long
using namespace std;
LL fac[21]={1};
LL stir1[21][21];
int main(){
for(int i=1;i<21;i++)
fac[i]=fac[i-1]*i;
for(int i=1;i<=20;i++){
stir1[i][0]=0;
stir1[i][i]=1;
for(int j=1;j<i;j++)
stir1[i][j]=stir1[i-1][j-1]+(i-1)*stir1[i-1][j];
}
int t,n,k;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&k);
if(n==1||k==0){
printf("0.0000\n");
continue;
}
LL sum=0;
for(int i=1;i<=k;i++)
sum+=stir1[n][i]-stir1[n-1][i-1];
printf("%.4f\n",(double)sum/fac[n]);
}
return 0;
}
分享到:
相关推荐
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
杭电ACMhdu1163
HDU1059的代码
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
hdu2101AC代码
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
搜索 dfs 解题代码 hdu1241
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
hdu 1166线段树代码
自己做的HDU ACM已经AC的题目
HDU最全ac代码
ACM HDU题目分类,我自己总结的大概只有十来个吧
hdu动态规划算法集锦
hdu题目分类
HDUACM201303版_10搜索入门
杭电 hdu acm 第1084题的解法,ac过了,是一位学长教我的.内有一些中文说明.
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105. Input Input...