Cod sursa(job #1906003)

Utilizator danstefanDamian Dan Stefan danstefan Data 6 martie 2017 12:00:29
Problema Dezastru Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <bits/stdc++.h>
using namespace std;
long long nrr;
int n,k,a,b,i,j,V[30],K,in;
double v[30],p,s;
void Sbm(int nr)
{
    if(nr==K)
    {
        p=1;
        for(in=1; in<=K; ++in)
            p*=v[V[in]];
        s+=p/nrr;
    }
    else
    {
        for(int i=V[nr]+1; i<=n-(K-nr-1); ++i)
        {
            V[nr+1]=i;
            Sbm(nr+1);
        }
    }
}
int main()
{
    ifstream f ("dezastru.in");
    ofstream g ("dezastru.out");
    f>>n>>K;
    for(i=1; i<=n; ++i)
        f>>v[i];
    if(n-K>K)a=n-K,b=K;
    else a=K,b=n-K;
    nrr=1;
    for(j=1,i=a+1; i<=n||j<=b; ++i,++j)
    {
        if(i<=n)  nrr*=i;
        if(j<=b)  nrr/=j;
    }
    /*
    for(i=1; i<(1<<n); ++i)
    {
        k=0;
        for(j=0; j<n; ++j)
            if(i&(1<<j))V[++k]=j+1;
        if(k==K)
        {
            p=1;
            for(in=1; in<=k; ++in)
                p*=v[V[in]];
            s+=p/nr;
        }
    }
    g<<s;*/
    Sbm(0);
    g<<s;
    return 0;
}