HDU 4465 candy (quick arrangement and combination)

Source: Internet
Author: User

Candy Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2115 accepted submission (s): 910
Special Judge


Problem descriptionlazychild is a lazy child who likes candy very much. despite being very young, he has two large candy boxes, each contains N candies initially. everyday he chooses one box and open it. he chooses the first box with probability P and the second box with probability (1-P ). for the chosen box, if there are still candies in it, he eats one of them; otherwise, he will be sad and then open the other box.
He has been eating one candy a day for several days. but one day, when opening a box, he finds no candy left. before opening the other box, he wants to know the expected number of candies left in the other box. can you help him?
Inputthere are several test cases.
For each test case, there is a single line containing an integer N (1 ≤ n ≤ 2x105) and a real number P (0 ≤ p ≤ 1, with 6 digits after the decimal ).
Input is terminated by EOF.
Outputfor each test case, output one line "case X: Y" where X is the test case number (starting from 1) and Y is a real number indicating the desired answer.
Any answer with an absolute error less than or equal to 10-4 wocould be accepted.
Sample Input
10 0.400000100 0.500000124 0.432650325 0.325100532 0.4875202276 0.720000
 
Sample output
Case 1: 3.528175Case 2: 10.326044Case 3: 28.861945Case 4: 167.965476Case 5: 32.601816Case 6: 1390.500000


Get the candy from two boxes until you find that the candy in one box has been obtained, and ask for the remaining candy in the other box.

Because n + 1 is selected for the obtained box, it is P ^ (n + 1). The accuracy problem can be solved by logarithm.

# Include <stdio. h> # include <math. h> # include <string. h> # include <stdlib. h >#include <algorithm> using namespace STD; # define ll _ int64 # define n 400005 const ll mod = 1000000007; double F [N]; double logc (INT m, int N) // The Composite Function C (n, m) = exp (lggc (n, m )); {return f [m]-f [N]-f [M-N];} int main () {double p, q; int N, I, K, CNT = 1; F [0] = 0; for (I = 1; I <n; I ++) f [I] = f [I-1] + Log (I * 1.0); While (~ Scanf ("% d % lf", & N, & P) {q = 1.0-p; double ans = 0; For (k = 0; k <n; k ++) {ans + = (n-k) * (exp (logc (n + k, k) + (n + 1) * log (p) + K * log (q )) + exp (logc (n + k, k) + (n + 1) * log (q) + K * log (p);} printf ("case % d: %. 6f \ n ", CNT ++, ANS);} return 0 ;}


HDU 4465 candy (quick arrangement and combination)

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.