UVa 1639 (desired) Candy

Source: Internet
Author: User

Test instructions

Each of the two boxes has n sugar, the probability of p per day from the first box to take a sugar, the probability of 1-p from the second box to a sugar. Until one day opening a box suddenly found that there is no sugar, the rest of the box to find the number of sweets expected.

Analysis:

Purple book above has been analyzed very clearly, but also gives a solution to the problem of precision loss, is to take the logarithm first and then the next.

1#include <cstdio>2#include <cmath>3 4 Const intMAXN =200000+5;5 Long DoubleLOGF[MAXN *2+1];6 7 Long DoubleLOGC (intNintm)8 {9     returnLogf[n]-logf[m]-logf[n-m];Ten } One  A intMain () - { -     //freopen ("In.txt", "R", stdin); the      for(inti =1; I <= maxn*2; ++i) -Logf[i] = logf[i-1] +log (i); -  -     intN, Kase =0; +     Doublep; -      while(SCANF ("%D%LF", &n, &p) = =2) +     { A         DoubleAns =0.0; at          for(inti =1; I <= N; ++i) -         { -             Long Doublec = LOGC (2*n-I, n); -             Long DoubleV1 = C + (n+1) *log (P) + (n-i) *log (1-p); -             Long DoubleV2 = C + (n+1) *log (1-p) + (n-i) *log (p); -Ans + = i * (exp (v1) +exp (v2)); in         } -printf"Case %d:%.6f\n", ++Kase, ans); to     } +  -     return 0; the}
code June

UVa 1639 (desired) Candy

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.