Cod sursa(job #365105)

Utilizator popoiu.georgeGeorge Popoiu popoiu.george Data 17 noiembrie 2009 21:28:25
Problema Dezastru Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.05 kb
#include<iostream>
#include<fstream>
#define inf "dezastru.in"
#define outf "dezastru.out"
#define MaxN 26
using namespace std;

fstream f(inf,ios::in),g(outf,ios::out);

int N,P,k,st[MaxN];
float prob[MaxN],suma,produs;
int fact1,fact2,nrc=0;

void Citire()
{
f>>N>>P;
for(int i=1;i<=N;i++)f>>prob[i];
}

void Init()
{
if(k==1)st[k]=0;
else st[k]=st[k-1];
}

int Succesor()
{
if(st[k]<N-P+k)
	{
	st[k]++;
	return 1;
	}
return 0;
}

int Valid()
{
return 1;
}

int Solutie()
{
return (k==P);
}

void Back()
{
int AS;
k=1;
Init();
while(k>0)
	{
	do{}while ( (AS=Succesor()) && !Valid() );
	if(AS)
		{
		if(Solutie())
			{
			nrc++;
			produs=1;
			for(int i=1;i<=P;i++)produs*=prob[st[i]];
			suma+=produs;
			}
		else {k++;Init();}
		}
	else k--;
	}
}

void CalcFact()
{
fact1=fact2=1;
for(int i=P+1;i<=N;i++)fact1*=i;
for(int i=N-P+1;i<=fact1;i++)fact2*=i;
}

int main()
{
Citire();
Back();
CalcFact();
//g<<fact1<<" "<<fact2<<endl;
g<< suma / (float)(nrc);
f.close();
g.close();
return 0;
}