Cod sursa(job #2922515)

Utilizator raresgherasaRares Gherasa raresgherasa Data 8 septembrie 2022 19:19:57
Problema Dezastru Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("dezastru.in");
ofstream fout ("dezastru.out");

const int NM = 27;

int n, m, x[NM];
long double ans, cnt, p, a[NM], q = 1;

void solve (long double p){
  long double r = 1;
  for (int i = 1; i <= m; i++){
    r *= a[x[i]];
  }
  ans += r;
}

void back (int k, long double p){
  for (int i = x[k - 1] + 1; i <= n; i++){
    if (k <= m){
      x[k] = i;
      if (k == 1){
        p = a[i];
      }
      else{
        p *= a[i];
      }
      if (k == m){
        solve(p);
        cnt += 1;
      }
      else{
        back(k + 1, p);
      }
    }
  }
}

int main(){
  ios_base::sync_with_stdio(false);
  fin.tie(0);
  fout.tie(0);
  fin >> n >> m;
  for (int i = 1; i <= n; i++){
    fin >> a[i];
  }
  back(1, 1);
  fout << fixed << setprecision(10) << ans / cnt << '\n';
}