「 51nod1639 」 shoelaces (Probability

Source: Internet
Author: User

 

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.