Welcome

首页 / 软件开发 / 数据结构与算法 / UVa 10169 Urn-ball Probabilities !:概率

UVa 10169 Urn-ball Probabilities !:概率2014-07-07 synapse7

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
", a[n], (int)b[n]);22.return 0;23.}