#include <iostream>
#include <string>
#include <cmath>
#include<iomanip>

using namespace std;

unsigned long long int fact(int n);
unsigned long long int C(int n, int r);

int main() {

    cout << setfill('-') << setw(80) << "-" << endl;

    cout << "You can use this to find the probabilities for a binomial expansion." << '\n';
    cout << "Please check that the individual probabilities add up to 1." << '\n';
    cout << setfill('-') << setw(80) << "-" << endl;

    int n;
    while (cout << "Please enter the sample size:\n", cin >> n)
    {
        double p;
        cout << "Enter the probability of a certain behaviour occurring:\n";
        cin >> p;
     

        long double k = 0;
        int i;
        long double j;

        for (i = 0; i <= n; i++) {

            j = C(n, i)*pow(p, i)*pow(1 - p, n - i);

            const unsigned label_width = n < 100 ? 11 : 13;
            const unsigned prec = 6;
            const unsigned num_width = p < 1.0 ? prec+3 : prec+7;
            const unsigned spacer = 10;

            const std::string label1 = "P(x = "  + std::to_string(i) + ")";
            const std::string label2 = "P(x <= " + std::to_string(i) + ")";

            std::cout << setfill(' ') << std::fixed << std::setprecision(prec);

            cout << left << setw(label_width) << label1 << " = ";
            cout << right << setw(num_width) << j;
            cout << setw(spacer) << "";
            cout << left << setw(label_width) << label2 << " = ";
            cout << right << setw(num_width) << j + k << '\n';

            k = j + k;

        }
    }

    return 0;

}

unsigned long long int fact(int n) {

    unsigned long long int t;
    unsigned long long int answer;

    answer = 1;
    for (t = 1; t <= n; t++) answer = answer * t;

    return(answer);
}

unsigned long long int C(int n, int r) {

    unsigned long long int a = 1;

    unsigned long long int p = n - r;

    for (n; n>p; n--) { a = a*n; }

    a = a / fact(r);

    return a;

}