Cod sursa(job #2380370)

Utilizator ShumaherAdasga Shumaher Data 14 martie 2019 20:48:59
Problema Dezastru Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("dezastru.in");
ofstream out("dezastru.out");

float a[26], prob = 0;
int N, K, st[26], total = 1;

void afis() {
    float t = 1;
    for(int i = 1; i <= K; i++)
        t *= a[st[i]];
    prob = prob + t;

}

bool ok(int k) {
    if(k == 1)
        return 1;
    else
        for(int i = 1; i < k; i++)
            if(st[i] == st[k])
                return 0;
    return 1;

}

void backtrack(int k) {
    for(int i = 1; i <= N; i++) {
        st[k] = i;
        if(ok(k))
            if(k == K)
                afis();
            else
                backtrack(k + 1);
        st[k] = 1;
    }
}
int main() {
    in >> N >> K;
    for(int i = 1; i <= N; i++) {
        in >> a[i];
        total = total * i;
    }
    backtrack(1);
    out << prob / total;
    return 0;
}