Cod sursa(job #2090982)

Utilizator Teodor.mTeodor Marchitan Teodor.m Data 18 decembrie 2017 22:24:12
Problema Dezastru Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.69 kb
#include <bits/stdc++.h>

using namespace std;

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

const double eps = 0.000001;
int x[26], n, p, nr;
double a[26], prob;

void calcul()
{
	double prod = 1.0;
	for(int i = 1; i <= p; ++i)
		prod *= a[x[i]];

	prob += prod;
	nr++;
}

bool valid(int k)
{
	for(int i = 1; i < k; ++i)
		if(abs(x[i] - x[k]) < eps)
			return false;
	return true;
}

void solve()
{
	int k = 1;
	while(k > 0) {
		while(x[k] < n) {
			x[k]++;

			if(valid(k))
				if(k == p)
					calcul();
				else {
					k++;
					x[k] = 0;
				}
		}
		k--;
	}
}

int main()
{
	fin >> n >> p;

	for(int i = 1; i <= n; ++i)
		fin >> a[i];

	solve();

	fout << prob / (double)nr;

	return 0;
}