Cod sursa(job #541627)

Utilizator PavelRazvanPavel Razvan PavelRazvan Data 25 februarie 2011 12:38:49
Problema Light2 Scor 80
Compilator cpp Status done
Runda Romanian Master in Mathematics and Sciences 2011, Ziua 1 Marime 1.08 kb
#include<algorithm>
using namespace std;

#define DIM 25

long long a[DIM],n,sol;
int b[DIM],k;

inline long long cmmmc (int x)
{
    long long nr1,nr2,aux,rez=1;
    int i;

    if(x==1)
        return a[b[1]];

    rez=a[b[1]];
    for(i=2;i<=x;++i)
    {
        nr1=rez;
        nr2=a[b[i]];
        while(nr2)
        {
            aux=nr1%nr2;
            nr1=nr2;
            nr2=aux;
        }
        rez*=(long long)a[b[i]]/nr1;
    }
    return rez;
}

inline int smn (int x)
{
    if(x%2==1)
        return 1;
    return -1;
}

void back (int x,int poz)
{
    if(x==k+1)
        return ;
    else
    {
        int i;
        for(i=poz;i<=k;++i)
        {
            b[x]=i;
            sol+=(long long)smn(x)*(n/cmmmc(x))*(1<<(x-1));
            back(x+1,i+1);
        }
    }
}

int main ()
{
    freopen("light2.in","r",stdin);
    freopen("light2.out","w",stdout);
    int i;

    scanf("%lld\n%d\n",&n,&k);
    for(i=1;i<=k;++i)
        scanf("%lld",&a[i]);
    back(1,1);

    printf("%lld\n",sol);
    return 0;
}