Cod sursa(job #2505053)

Utilizator mihaicivMihai Vlad mihaiciv Data 6 decembrie 2019 08:51:07
Problema Dezastru Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.62 kb
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int n, k;
double a[100];
int vis[100], v[100];
double total_sum;
double total_ans;
double total_prod;
double prod;

ifstream f("dezastru.in");
ofstream g("dezastru.out");

bool valid(int x) {
    if (vis[x] == 1) return false;
    return true;
}

void calculare(int tip) {

    if (tip == 1) {
        double current_val = 1;
        for (int i = 1; i <= k; ++i) {
            //cout << v[i] << " ";
            current_val *= a[v[i]];
        }
        total_sum += current_val;
        //cout << "\n";
        total_ans ++;
    } else {
        double current_val = 1;
        for (int i = 1; i <= n; ++i) {
            if (vis[i]) continue;
            current_val *= a[i];
        }
        total_sum += current_val;
        total_ans ++;
    }
}

void bk(int x) {

    for (int i = v[x - 1] + 1; i <= n; ++i) {
        v[x] = i;
        prod = prod * a[i];
        if (x == k) {
            total_sum += prod;
            ++total_ans;
        } else if (k - x <= n - i) {
            bk(x + 1);
        }
        prod = prod / a[i];
        total_prod = total_prod * a[i];
        //vis[i] = 0;

    }

}

int main() {

    f >> n >> k;
    total_prod = 1;
    for (int i = 1; i <= n; ++i) {
        f >> a[i];
        //total_prod *= a[i];
    }
    prod = 1;
    /*if (k > n / 2) {
        k = n - k;
        bk(1, 1, 0);
        g << setprecision(6) << (total_sum / total_ans);
        return 0;
    }*/
    bk(1);

    g << setprecision(6) << (total_sum / total_ans);

    return 0;
}