uva 10169 Urn-ball Probabilities!

来源:互联网 发布:centos 7开机密码忘记 编辑:程序博客网 时间:2024/06/14 06:49

原题:
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
1
2
20
Sample Output
0.500000 0
0.583333 1
0.688850 38
大意:
给你两个缸,开始时第一缸里有一个红球,第二个缸里有一个红球一个白球。初始状态算是第一个状态,从第一个状态开始每次分别在两个缸中各挑出一个球,然后放回去,在分别向两个缸里各放一个白球。现在问你两个问题,一个是n次取球中至少有一次是两个都是红球的概率,第二个是每次都是两个红球的时的概率小数点后有多少个连续的0。

#include <bits/stdc++.h>using namespace std;double node[1000000];double zeros[1000000];int main(){    ios::sync_with_stdio(false);    node[1]=0.5;    zeros[1]=log10(2);    for(int i=2;i<1000000;i++)    {        node[i]=1-(1-node[i-1])*(1-(1.0/i)*(1.0/(i+1.0)));        zeros[i]=zeros[i-1]+log10(1.0*i*(i+1));//必须乘以1.0否则答案错误    }    int n;    while(cin>>n)    {        cout<<fixed<<node[n]<<" ";        cout<<(int)zeros[n]<<endl;    }    return 0;}

解答:
第一个问还是挺简单的,用递推的方法,node[i]保存的是第i次取球的时候至少有一次是两个都是红球的概率。
那么node[i]就等于1-p(~r1,~r1)p(~r2,~r2)……p(~ri-1,ri-1)其中p(~ri,~ri)的意思是在两个缸里拿球都不是红色的概率。整理一下就恩给你得到公式node[i]=1-(1-node[i-1])(1-(1.0/i)(1.0/(i+1.0)));

第二个问也挺容易,但是精度要考虑。问你小数点后面有多少个连续的0,相当于问你这个数的倒数是一个多少位的数。
计算每次都是两个红球的时的概率的公式是

(1/1)*(1/2)*(1/2)*(1/3)*(1/3)*.....(i/1)*(1/(i+1))

计算倒数,然后用log10去计算位数保存为double型,这里注意log10(1.0*i*(i+1));//必须乘以1.0否则答案错误。

0 0
原创粉丝点击