Problem B
Urn-ball Probabilities!
Input: standard input
Output: standard output
Time Limit: 3 seconds
Assume that you have two urns before you. Initially, one urn has one ball and the other urn has two balls and exactly one ball in each urn is red. At this initial stage you are asked to pick up two balls, one from each urn. Then one white ball is added in each urn and you are again asked to pick up one ball from each urn then again one white ball is added in each urn. This process continues for a certain time. Remember that you place the picked ball back to the urn after each pick up. You will have to determine the probability that in any of your pickups both of the picked balls were red and also the probability that all of your picked balls were red after certain steps.
Input
The input file contains several lines of inputs. Each line of the input file contains an unsigned integer N (N<1000000) indicating how many times you will pick up. Of course after each pick up an increment in balls occurs as described previously.
Output
For each line of input print a single line of output containing a floating point number and an integer. 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.
Sample Input:
12
20
Sample Output:
0.500000 00.583333 1
0.688850 38
题意:两个罐子,开始一个有一个红球,一个有一个红球一个白球,每次取一个球(放回),然后多放一个白球进去。给定n,求出取n次出现取得两个红球的概率。和取n次都是两个红球的概率的小数点后面的0有几位。
思路:第一个问题:当前次取到两个红球概率等于 p之前没取到两红球 * p这次取到两红球,这样叠乘上去的 pi = (1 - pi - 1) * (1 / (i + 1) * i)。
第二个问题:每次都取到两红球概率不断叠乘上去 pi = pi - 1 * (1 / (1 + i) * i); 位数为 - lg(pi)
要先打表预处理不然会超时。。在处理第二个问题的时候,由于有精度误差,我们可以转化为 0 - lg(pi) = lg1 - lg 1 / ((1 + i) * i = lg(i * (1 + i);这样,注意类型要用longlong 不然100W^2 int是存不下的
代码:
#include <stdio.h>
#include <math.h>
const long long N = 1000005;
long long n, i;
double ans, p, a[N], count;
long long b[N];
int main() {
count = 0; ans = p = 0;
for (i = 1; i <= 1000000; i ++) {
p = 1.0 / (i * (i + 1));
ans += (1 - ans) * p;
count += log10(i * (i + 1));
a[i] = ans; b[i] = (int)count;
}
while (~scanf("%lld", &n)) {
printf("%.6lf %lld\n", a[n], b[n]);
}
return 0;
}