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/