cppAcademy
← Learning PathLESSON 23 / 30 · MATHEMATICS · 35 MIN✕

Probability Fundamentals

Probability assigns weights to possible outcomes and adds them over events. Competitive problems often reduce a random process to counting equally likely outcomes or carefully multiplying conditional probabilities. Independence and mutual exclusion are different properties and must not be conflated. We calculate probabilities for two draws from a box of red and blue objects, comparing sampling with and without replacement.

Prerequisites: Fractions; combinations; floating-point types.

Build the idea

  1. 1. Define the sample space and an event

    An outcome describes all random choices being considered; an event is a set of outcomes. With equally likely outcomes, probability is favorable count divided by total count. For two ordered draws without replacement from T distinct objects, there are T(T−1) equally likely ordered outcomes. Color alone may merge several underlying outcomes, so do not assume all color sequences have equal probability.

    P(A)=|A||Ω|

  2. 2. Use the conditional multiplication rule

    P(A∩B)=P(A)P(B|A) when P(A)>0. Without replacement, if the first draw is red there are R−1 red among T−1 remaining objects, so both-red probability is (R/T)((R−1)/(T−1)). Handle R<2 as probability zero instead of interpreting a negative remaining red count. With replacement, the composition resets and both-red probability is (R/T)².

  3. 3. Distinguish independence from exclusion

    Independent events satisfy P(A∩B)=P(A)P(B). Mutually exclusive events have empty intersection. “First draw red” and “first draw blue” are exclusive; if both have positive probability they are not independent. “First draw red” and “second draw red” are generally dependent without replacement, but independent with replacement under uniform draws.

  4. 4. Apply addition and complements

    In general P(A∪B)=P(A)+P(B)−P(A∩B). For “at least one red” in two draws, it is simpler to subtract “both blue” from one. With R=B=2, both-red probability without replacement is (2/4)(1/3)=1/6. Both-blue is also 1/6, so at-least-one-red is 5/6. With replacement, both-red is 1/4.

  5. 5. Keep exact reasoning separate from decimal output

    The formulas describe exact rational probabilities; the program prints floating approximations with six decimal places. This is not a Monte Carlo estimate and does not become mathematically exact because six digits are displayed. Total T must be at least two for two draws without replacement. Degenerate all-red or all-blue boxes are valid and should give probabilities zero or one where appropriate.

C++17 implementation

This is a complete program. Compile with C++17 and enter the input described below.

// Key idea: 5. Keep exact reasoning separate from decimal output The formulas describe exact rational
// probabilities; the program prints floating approximations with six decimal places.
#include<iostream>
#include<iomanip>
int main(){
    long long red,blue;
    std::cin>>red>>blue;
    long double total=red+blue;
    long double bothRed=red<2?0.0L:(red/total)*((red-1)/(total-1));
    long double bothBlue=blue<2?0.0L:(blue/total)*((blue-1)/(total-1));
    long double withReplacement=(red/total)*(red/total);
    std::cout<<std::fixed<<std::setprecision(6)<<bothRed<<' '<<1-bothBlue<<' '
                <<withReplacement<<'\n';
}

Complexity analysis

O(1) arithmetic time and space. red,blue≤10^6 and total≥2 ensure denominators are positive and input addition is safe. long double provides approximate floating arithmetic whose exact precision is implementation-dependent; the mathematical derivation is rational.

Common mistakes and edge cases. Do not perform integer division before converting to a real type. Independent is not synonymous with mutually exclusive. Conditional probabilities require a valid positive conditioning probability. Without replacement the denominator and available counts change. Avoid a zero total or a second-draw denominator zero; the contract excludes both.

Example input and output

Read nonnegative red blue, each≤10^6, with red+blue≥2. Print approximate both-red without replacement, at-least-one-red without replacement, and both-red with replacement, to six decimal places.

Input

2 2

Output

0.166667 0.833333 0.250000

The exact probabilities are 1/6, 5/6 and 1/4 respectively.

Practical applications

Use for counting random outcomes, conditional processes, complements and probability DP foundations. For exact rational answers under a modulus, denominators need modular inverses under the problem’s assumptions. For ordinary decimal tasks, track numerical precision and required tolerance instead.

Check your understanding

Are two positive-probability mutually exclusive events independent?

Put it into practice

Enumerate labeled ordered draws for a tiny box to verify the formulas.

Show a hint

Count underlying objects, not merely color strings as equally likely.

Further reading