UVA 10169 Urn-ball probabilities!

Source: Internet
Author: User

Original question:
Assume that there are urns before you. Initially, one urn has a ball and the other urn have both balls and exactly one ball in each urn is red. At this initial stage you is asked to pick up and the balls,one from each urn. Then one white ball was added in each urn and again asked to pick up one ball from each urn then again one white BA ll is added in each urn. This process is continues for a certain time. Remember that-you-place-the-picked ball-back-to-the-urn after all pick up. You'll have to determine the probability it in any of your pickups both of the picked balls were red and also the prob Ability 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'll pick up. Of course after each pick up an increment
In balls occurs as described previously.
Output
For each line of the input print a single line of output containing a floating point number and a integer.
The floating-point number indicates the probability that's picked up and the red balls in at least
One of your pick-ups and the second integer denotes how many consecutive zeros is there after decimal
Point in the probability value, all of your pick ups have both balls as red.
Sample Input
1
2
20
Sample Output
0.500000 0
0.583333 1
0.688850 38
Effect:
Give you two cylinders, start with a red ball in the first cylinder, and a red ball in the second jar with a white ball. The initial state is considered the first state, starting from the first state each time each of the two cylinders to pick out a ball, and then put back, in each of the two cylinders to put a white ball. Now ask you two questions, one is n time to take the ball at least once is two is the probability of the red ball, the second is each time is two red ball when the probability of a decimal point after the number of consecutive 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]) * (n (1.0/i) * (1.0/(i+1.0)));
        Zeros[i]=zeros[i-1]+log10 (1.0*i* (i+1));//Must be multiplied by 1.0 otherwise the answer error
    }
    int n;
    while (Cin>>n)
    {
        cout<<fixed<<node[n]<< "";
        cout<< (int) zeros[n]<<endl;
    }
    return 0;
}

Answer:
The first question is quite simple, using the recursive method, Node[i] Save is the first time to take the ball at least once is two is the probability of a red ball.
Then Node[i] is equal to 1-p (~R1,~R1) p (~r2,~r2) ... p (~ri-1,ri-1) where P (~ri,~ri) means that holding the ball in two cylinders is not a red probability. Just tidy up and give you the formula Node[i]=1-(1-node[i-1]) (N (1.0/i) (1.0/(i+1.0)));

The second question is also quite easy, but the accuracy should be considered. Ask you how many consecutive 0 of the decimal point you have, which is equivalent to asking you how many digits the count is.
The formula for calculating the probability of a two red ball each time is

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

Calculate the reciprocal, and then use LOG10 to calculate the number of bits saved to double type, here note log10 (1.0*i* (i+1));//Must be multiplied by 1.0 otherwise the answer is wrong.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.