Cod sursa(job #2922513)

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

using namespace std;

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(){
  cin >> n >> m;
  for (int i = 1; i <= n; i++){
    cin >> a[i];
  }
  back(1, 1);
  cout << fixed << setprecision(10) << ans / cnt << '\n';
}