1639 benchmark time limit for binding shoelaces: 1 second space limit: 131072 kb score: 20 difficulty: 3 levels of algorithm questions favorites followed by N shoelaces mixed together, now repeat n times the following operations: randomly take two shoes to take the lead and tie them together. As you can imagine, after N times, there will be no independent shoes taking the lead, and N shoelaces will be tied into some rings. So what is the probability that all these shoelaces form only one ring? Input
Only one row contains an integer N (2 <=n <= 1000 ).
Output
Output a line, indicating the probability of just forming a ring.
Input example
2
Output example
0.666667
Question
Considering that $ I $ has been completed,
So there are $2 * N-2 * I $ shoes at the moment,
Where you hold one, there are $2 * N-2 * I-1 $ shoes to take the lead,
Only one of these shoes (on the same rope you hold) cannot be knotted,
So this step can hit a qualified knot probability $ \ frac {2 * N-2 * I-2} {2 * N-2 * I-1} $.
$ I $ loops from $0 $ to $ N-2 $, stops when $ n-1 $ knot is hit (because $ n-1 $ knot is already a chain ).
The answer is the result of multiplication.
1 /* 2 C++ 3 15 ms 4 2108 KB 5 Accepted 6 2018/10/26 7 17:01:37 8 */ 9 #include<iostream>10 #include<cstdio>11 using namespace std;12 int main()13 {14 //freopen("a.in","r",stdin);15 int n;16 scanf("%d",&n);17 double ans=1;18 for(int i=0;i<n-1;++i)19 {20 ans*=(double)(2*n-(2*i)-2)/(2*n-(2*i)-1);21 }22 cout<<ans;23 return 0;24 }
「 51nod1639 」 shoelaces (Probability