HDU 4465 Candy

來源:互聯網
上載者:User

標籤:

Candy

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2520    Accepted Submission(s): 1100
Special Judge

 

Problem Description
LazyChild 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?

Input
There are several test cases.
For each test case, there is a single line containing an integer $n (1 \leq n \leq 2 \times 10^5) $and a real number p ($0\leq p \leq 1$, with 6 digits after the decimal).
Input is terminated by EOF.

Output
For 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 would be accepted.

Sample Input10 0.400000 100 0.500000 124 0.432650 325 0.325100 532 0.487520 2276 0.720000 

 

Sample OutputCase 1: 3.528175Case 2: 10.326044Case 3: 28.861945Case 4: 167.965476Case 5: 32.601816Case 6: 1390.500000 

 

Source2012 Asia Chengdu Regional Contest

 解題:公式題

 首先可以算出公式

\[\sum_{i = 0}^{n-1}\binom{n}{n+i}\times (p^{n+1}\times (1-p)^i + (1-p)^{n+1}\times p^i)\times (n-i)\]

我們至少要拿n個,才會出現一個盒子裡面的糖果空,所以假設第一個為空白,枚舉第二個盒子剩餘的糖果數量,或者假設第二個盒子為空白,枚舉第一個盒子剩餘的糖果數量。

但是,更加狗血的事情發生了,會導致double溢出,你說用long double啊?呵呵,naive!還是要溢出,日了整個世界,好吧,使用log吧。換成對數,把*變成+

 算$\binom{n}{n+i}$ 是可以滾動著算的

可以發現在取對數後有:

$$C_{0} = 0,C_i = C_{i-1} + log(n + i) - log(i)$$

 1 #include <bits/stdc++.h> 2 using namespace std; 3 int main() { 4     int n,cs = 1; 5     double p; 6     while(~scanf("%d%lf",&n,&p)) { 7         double ret = 0,tmp,c = 0,logp = log(p),log_p = log(1 - p); 8         for(int i = 0; i < n; ++i) { 9             double logn_i = log(n-i);10             tmp = c + (n + 1)* logp + i*log_p + logn_i;11             ret += exp(tmp);12             tmp = c + (n + 1)*log_p + i*logp + logn_i;13             ret += exp(tmp);14             c += log(n + i + 1.0) - log(i + 1.0);15         }16         printf("Case %d: %.6f\n",cs++,ret);17     }18     return 0;19 }
View Code

 

HDU 4465 Candy

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.