Knots
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 261 Accepted Submission(s): 182
Problem Description
An even number N of strands are stuck through a wall. On one side of the wall, a girl ties N/2 knots between disjoint pairs of strands. On the other side of the wall, the girl's groom-to-be also ties N/2 knots between disjoint pairs of strands. You are to find the probability that the knotted strands form one big loop (in which case the couple will be allowed to marry).
For example, suppose that N = 4 and you number the strands 1, 2, 3, 4. Also suppose that the girl has created the following pairs of strands by tying knots: {(1, 4), (2,3)}. Then the groom-to-be has two choices for tying the knots on his side: {(1,2), {3,4)} or {(1,3), (2,4)}.
For example, suppose that N = 4 and you number the strands 1, 2, 3, 4. Also suppose that the girl has created the following pairs of strands by tying knots: {(1, 4), (2,3)}. Then the groom-to-be has two choices for tying the knots on his side: {(1,2), {3,4)} or {(1,3), (2,4)}.
Input
The input file consists of one or more lines. Each line of the input file contains a positive even integer, less than or equal to 100. This integer represents the number of strands in the wall.
Output
For each line of input, the program will produce exactly one line of output: the probability that the knotted strands form one big loop, given the number of strands on the corresponding line of input. Print the probability to 5 decimal places.
Sample Input
4 20
Sample Output
0.66667 0.28377
题意:
有n条绳子(n为偶数),平行放置一排,在两头分别打n/2次节,且使用过的不能再次使用,问,最终使得所有绳子连接成一个大圈的概率
题解:
直接操作起来会比较麻烦,自己第一次见到的时候也是无从下手,阳历都过不去,今天重新看了一下题目,感觉思路清晰多了.
不难发现,对于两端进行操作是互不影响的,把绳子看成是一样的,那么无论第一个人如何操作,最终结果都一样,n 根绳子连成了 n/2 个绳子,然后另外一端进行选择的时候,选择哪个节点都是完全一样的,对某一节点一共有 n-1 种选择(其他n-1个端点),如果连接上这条绳子的另外一个端点肯定会形成环,连接其他的端点不会,那么这个概率就是(n-2)/(n-1),下面的问题就转化成了 n/2-1条绳子的子问题了....依次递推,就得到了最终结果....
看来这道题是可以用递归做的啊.....貌似也可以用dp做.....
综上,对于n,最终结果是:
(n-2)/(n-1)*(n-4)/(n-3)*......*2/3
如上述分解成子问题的话,可以直接循环累乘
/*
http://blog.csdn.net/liuke19950717
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int n;
while(~scanf("%d",&n))
{
double ans=1;
for(int i=n-1;i>1;i-=2)
{
ans*=(i-1)*1.0/i;
}
printf("%.5lf\n",ans);
}
return 0;
}