Cod sursa(job #2765780)

Utilizator DragosC1Dragos DragosC1 Data 29 iulie 2021 20:56:41
Problema Dezastru Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <fstream>
#include <iomanip>
using namespace std;
 
int n, k;
double a[26];
 
void read() {
    int i;
    ifstream f("dezastru.in");
    f >> n >> k;
    for (i = 1; i <= n; i++)
        f >> a[i];
    f.close();
}
 
double rez, C;
short comb[26];
 
void back(int niv, double prod) {
    for (int i = comb[niv - 1] + 1; i <= n - k + niv; i++) {
        comb[niv] = i;
        if (niv < k)
            back(niv + 1, prod * a[i]);
        else rez += prod * a[i] * C;
    }
}
 
void solve() {
    int i;
    C = 1;
    for (i = 1; i <= k; i++)
        C *= i;
    for (i = n - k + 1; i <= n; i++)
        C = C * 1.0 / i;
    back(1, 1);
}
 
void output() {
    ofstream g("dezastru.out");
    g << setprecision(7) << fixed << rez;
    g.close();
}
 
int main() {
    read();
    solve();
    output();
    return 0;
}