HDU 4465 candy-Number of probability log combinations

Source: Internet
Author: User
/* each bottle contains N sweets; the probability of getting from the first bottle is P. When you get another candy, the expected calculation process of the remaining number of sweets in another bottle may cause overflow and overflow. */# include 
  
    # include 
   
     double lognjie [1, 400010]; double logc (int n, int m) {return lognjie [N]-lognjie [m]-lognjie [n-M]; // C (n, m) = n! /(N-m )! * M !) Log (C (n, m) = Log (N !) -Log (M !) -Log (m-N )!)} Int main () {int I, n, Index = 1; double p, q; lognjie [0] = 0; for (I = 1; I <= 400000; + + I) {lognjie [I] = lognjie [I-1] + Log (1.0 * I); // log (N !) = Log (n-1 )!) + Log (n)} while (scanf ("% d % lf", & N, & P )! = EOF) {double ret = 0; q = 1-P; for (I = 0; I <= N; ++ I) // The second box contains I. The other box has n + 1 times. {RET + = (n-I) * (exp (logc (n + I, I) + (n + 1) * log (1.0 * P) + I * log (1.0 * q) + exp (logc (n + I, I) + (n + 1) * log (1.0 * q) + I * log (1.0 * p);} printf ("case % d: %. 6lf \ n ", index ++, RET);} return 0 ;}
   
  
/* Expected formula percentile = Σ p * n p is the probability n is the number p = p * C (n, m) * PN * (1-p) m-n c (m, n) = C (m-1, n) * m/(m-N) probability m = 0 P ^ (n + 1) M = 1 P ^ (n + 1) QM = 2 P ^ (n + 1) q ^ 2q power through the loop, how to control P also needs to be supplemented */# include <math. h> # include <stdio. h> double Pro (int n, Double P) {double Zhong = 1, ret = N * P; For (int m = 1; m <= N; ++ m) // take M from the second bottle {Zhong * = p * (1-p) * (m + n)/m; RET + = (n-m) * Zhong; RET * = P;} return ret;} int main () {int N, Index = 1; Double P; while (~ Scanf ("% d % lf", & N, & P) {double ret = Pro (n, p) + Pro (n, 1-p ); printf ("case % d: %. 6lf \ n ", index ++, RET);} return 0 ;}

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.