Pagini recente » Cod sursa (job #280688) | Cod sursa (job #331985) | Cod sursa (job #1674782) | Cod sursa (job #2703101) | Cod sursa (job #2228655)
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
double prob[30];
int perm[30];
bool block[30];
double ans;
int n, k;
void backtrack(int pos){
if(pos == k + 1){
double totprob = 1.0000000;
for(int i = 1; i <= k; ++i)
totprob *= prob[perm[i]];
ans += totprob;
}
for(int i = 1; i <= n; ++i){
if(!block[i]){
perm[pos] = i;
block[i] = 1;
backtrack(pos + 1);
perm[pos] = 0;
block[i] = 0;
}
}
return;
}
int main()
{
ifstream fin("dezastru.in");
ofstream fout("dezastru.out");
fin >> n >> k;
int total = 1;
for(int i = 1; i <= n; ++i){
fin >> prob[i];
total *= i;
}
backtrack(1);
ans /= total;
fout << fixed << setprecision(6) << ans;
return 0;
}