UVa 10169 Urn-ball Probabilities !:機率

來源:互聯網
上載者:User

10169 - Urn-ball Probabilities !

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1110

看輸出要求:The floating-point number indicates the probability that you have picked up two red balls in at least one of your pick-ups and the second integer denotes how many consecutive zeros are there after decimal point in the probability value that all of your pick ups has both balls as red.

第一個值是在N次摸球中,至少有一次從兩個甕中都取出紅球的機率;第二個值是在N次摸球都是紅球的機率中,在小數點後有多少個連續的0。

思路:

1. 全機率公式。如果前面摸了球,後面不管摸不摸到紅球,機率都一樣;前面如果沒摸到球(1-P(n-1)),那麼就要乘上這次摸到球的機率1/ (n*(n+1))

所以P(n) = P(n-1) + (1-P(n-1)) / (n*(n+1))

我們可以事先打表計算P(n)。

2. 每次都摸到紅球的話,機率為(1*1/2)*(1/2*1/3)*...*(1/n*1/(n+1)),算連續0取對數即可。

求遞推式的產生函數?(待研究,坑)

完整代碼:

01./*0.108s*/02.  03.#include<cstdio>  04.#include<cmath>  05.const long long N = 1000001;  06.  07.double a[N], b[N];  08.  09.int main()  10.{  11.    int n, i;  12.    double ans = 0.0, count = 0.0, tmp;  13.    for (i = 1; i < N; ++i)  14.    {  15.        tmp = (double)i * (i + 1);  16.        ans += (1 - ans) / tmp;  17.        count += log10(tmp);  18.        a[i] = ans, b[i] = count;  19.    }  20.    while (~scanf("%d", &n))  21.        printf("%.6f %d\n", a[n], (int)b[n]);  22.    return 0;  23.}

查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.