Examining the Rooms
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1138 Accepted Submission(s): 686
Problem Description
A murder happened in the hotel. As the best detective in the town, you should examine all the N rooms of the hotel immediately. However, all the doors of the rooms are locked, and the keys are just locked in the rooms, what a trap! You know that there is exactly
one key in each room, and all the possible distributions are of equal possibility. For example, if N = 3, there are 6 possible distributions, the possibility of each is 1/6. For convenience, we number the rooms from 1 to N, and the key for Room 1 is numbered
Key 1, the key for Room 2 is Key 2, etc.
To examine all the rooms, you have to destroy some doors by force. But you don’t want to destroy too many, so you take the following strategy: At first, you have no keys in hand, so you randomly destroy a locked door, get into the room, examine it and fetch the key in it. Then maybe you can open another room with the new key, examine it and get the second key. Repeat this until you can’t open any new rooms. If there are still rooms un-examined, you have to randomly pick another unopened door to destroy by force, then repeat the procedure above, until all the rooms are examined.
Now you are only allowed to destroy at most K doors by force. What’s more, there lives a Very Important Person in Room 1. You are not allowed to destroy the doors of Room 1, that is, the only way to examine Room 1 is opening it with the corresponding key. You want to know what is the possibility of that you can examine all the rooms finally.
To examine all the rooms, you have to destroy some doors by force. But you don’t want to destroy too many, so you take the following strategy: At first, you have no keys in hand, so you randomly destroy a locked door, get into the room, examine it and fetch the key in it. Then maybe you can open another room with the new key, examine it and get the second key. Repeat this until you can’t open any new rooms. If there are still rooms un-examined, you have to randomly pick another unopened door to destroy by force, then repeat the procedure above, until all the rooms are examined.
Now you are only allowed to destroy at most K doors by force. What’s more, there lives a Very Important Person in Room 1. You are not allowed to destroy the doors of Room 1, that is, the only way to examine Room 1 is opening it with the corresponding key. You want to know what is the possibility of that you can examine all the rooms finally.
Input
The first line of the input contains an integer T (T ≤ 200), indicating the number of test cases. Then T cases follow. Each case contains a line with two numbers N and K. (1 < N ≤ 20, 1 ≤ K < N)
Output
Output one line for each case, indicating the corresponding possibility. Four digits after decimal point are preserved by rounding.
Sample Input
3 3 1 3 2 4 2
Sample Output
0.3333 0.6667 0.6250HintSample Explanation When N = 3, there are 6 possible distributions of keys: Room 1 Room 2 Room 3 Destroy Times #1 Key 1 Key 2 Key 3 Impossible #2 Key 1 Key 3 Key 2 Impossible #3 Key 2 Key 1 Key 3 Two #4 Key 3 Key 2 Key 1 Two #5 Key 2 Key 3 Key 1 One #6 Key 3 Key 1 Key 2 One In the first two distributions, because Key 1 is locked in Room 1 itself and you can’t destroy Room 1, it is impossible to open Room 1. In the third and forth distributions, you have to destroy Room 2 and 3 both. In the last two distributions, you only need to destroy one of Room 2 or Room
题目解法就是:
若k把钥匙可打开 就是判断时候有多少种情况有k个环,且1不能独自成环.
此时我们引入第一类斯特林数:
s(p,k)的一个的组合学解释是:将p个物体排成k个非空循环排列的方法数。
抛开1不能独自成环这点考虑的话
s(p,k)即为我们所求了!
如果考虑1不能独自成环 利用容斥原理就好了 s(p,k)-s(p-1,k-1)(总数-1独自成环的数)
思路就是如此
现在我们来研究第一类斯特林数的递推公式
s(p,k)=(p-1)*s(p-1,k)+s(p-1,k-1)
递推关系的说明:
考虑第p个物品,p可以单独构成一个非空循环排列,这样前p-1种物品构成k-1个非空循环排列,方法数为s(p-1,k-1);
也可以前p-1种物品构成k个非空循环排列,而第p个物品插入第i个物品的左边,这有(p-1)*s(p-1,k)种方法。
至此结束 附上代码(本人非这种方法 所以没写了)
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn=20; long long f[25],stir[25][25]; int solve() { int i,j; f[0]=1; for(i=1;i<=maxn;i++) f[i]=i*f[i-1]; //因为N有N!种排列顺序,这作为总数 //计算概率 for(i=1;i<=maxn;i++) stir[i][0]=0; stir[1][1]=1; for(i=1;i<=maxn;i++) for(j=1;j<=i;j++) { if(i==j) stir[i][j]=1; else stir[i][j]=stir[i-1][j-1]+(i-1)*stir[i-1][j]; } for(i=1;i<=maxn;i++) for(j=1;j<=maxn;j++) if(stir[i][j]<0) stir[i][j]=-stir[i][j]; return 0; } int main() { int cas,n,i,k; long long sum; solve(); scanf("%d",&cas); while(cas--) { scanf("%d %d",&n,&k); sum=0; for(i=1;i<=k;i++) sum+=stir[n][i]-stir[n-1][i-1]; printf("%.4lf ",1.0*sum/f[n]); //因为写成printf("%.4lf ",(double)sum/f[n]); //run time error! 下次一定记好了! } return 0; }
接下来是我的方法!
从没接触过斯特林数 纯粹自己乱组合出了做法
首先考虑一下几个问题
1.N个元素成环的排列数目是多少?
答:(N-1)!
2.N个元素成k个环呢?
答 枚举k个环的元素个数 利用1乘法原理,再累加起来(加法原理) ,注意 1不能独自成环的情况
虽然 复杂度很高 但是因为不涉及高精度 所以n,k很小 所以纯粹的枚举也可以
(枚举的时候 利用dfs 不要傻傻的用for for for)
下面贴代码
#include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <ctime> #include <algorithm> #include <iostream> #include <sstream> #include <string> #define oo 0x13131313 using namespace std; long long H[21]; long long F[21][21]; long long T[21]; long long C[21][21]; void CLT(int n,int len) { long long tot=n; long long k=1; for(int i=1;i<=len;i++) { if(T[i]==1) { k*=C[tot-1][1]; tot-=T[i]; } else { k*=(C[tot][T[i]]*H[T[i]-1]); tot-=T[i]; } } tot=1; T[len+1]=0; for(int i=2;i<=len+1;i++) { if(T[i]==T[i-1]) { tot++; } else { k=k/H[tot]; tot=1; } } F[n][len]+=k; } int getxulie(int n,long long tot,int prev,int len,int deep) { T[deep]=prev; tot+=prev; if(deep==len) if(tot==n) { CLT(n,len); return 1; } else return 0; for(int i=prev;i<=n-len+1;i++) getxulie(n,tot,i,len,deep+1); return 0; } void YCLYCL() { H[0]=1; for(int i=1;i<=20;i++) { H[i]=H[i-1]*i; } for(int i=1;i<=20;i++) C[i][0]=1; C[1][1]=1; for(int i=2;i<=20;i++) for(int j=1;j<=i;j++) { C[i][j]=C[i-1][j-1]+C[i-1][j]; } } void YCL() { YCLYCL(); for(int i=2;i<=20;i++) for(int j=1;j<=i;j++) { for(int k=1;k<=i-j+1;k++) { memset(T,0,sizeof(T)); getxulie(i,0,k,j,1); } } for(int i=2;i<=20;i++) for(int j=1;j<=i;j++) { F[i][j]+=F[i][j-1]; } } void inin() { freopen("a.in","r",stdin); freopen("a.out","w",stdout); } int main() { //inin(); YCL(); int K; cin>>K; int a;int b; while(K--) { cin>>a>>b; printf("%.4lf ",((double)F[a][b]/(double)H[a])); } }