Example 10-17 Candy UVa1639

Source: Internet
Author: User
Tags natural logarithm

1. Title Description: Click to open the link

2. Solution thinking: According to test instructions, may wish to open the first box, at this time the second box and I candy, so this has opened the n+ (n-i) times box, where n times to take the box 1,n-i times to take box 2, the method of a total of C (2*n-i,n) species, So the probability of the box 2 remaining I candy is C (2*n-i,n) *p^ (n+1) * (1-p) ^ (n-i). Note that the probability of opening the box 1 at a time is also counted. Given the probabilities, it is not difficult to find the answer according to the desired definition. However, it is important to note that because N can be very large, the logarithm is used to calculate and prevent excessive precision loss. In order to improve the efficiency of the program, it is possible to calculate the natural logarithm of n! in advance, which is convenient for subsequent calculation. In addition, the boundary of P may be 0 or 1, so it is handled separately.

3. Code:

#define _crt_secure_no_warnings #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <functional>using namespace std; #define N 200000long Double v1, V2;long Double log[2*n+100];void init ()//preprocessing, calculates n! 's natural pair value {for (int i = 1; I <= N * 2; i++) log[i] = Log[i-1] + Log (i);} int main () {//freopen ("test.txt", "R", stdin); int rnd = 0;int n;double p;init (); while (CIN >> n >> p) {Double A s = 0.0;printf ("Case%d:", ++rnd); if (p = = 0 | | p = = 1) ans = n;//special handling Else{long Double p1 = log (p), long double q1 = log (1-p), for (int i = 1; I <= n; i++) {long double c = log[2 * N-i]-log[n]-LOG[N-I];V1 = C + (n + 1) *p1 + (n-i) *q1;v2 = C + (n + 1) *q1 + (n-i) *p1;ans + = (double) i* (exp (v1) + exp (v2));}} PriNTF ("%.6lf\n", ans); return 0;}

Example 10-17 Candy UVa1639

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.