Cod sursa(job #1551609)

Utilizator alex_HarryBabalau Alexandru alex_Harry Data 16 decembrie 2015 09:10:48
Problema Light2 Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("light2.in");
ofstream g("light2.out");
int N,K;
long long D[25],x[25],cm=1,sol;
long long CMMDC(long long a,long long b)
{
    long long r=a%b;
    while(r!=0)
    {
        a=b;
        b=r;
        r=a%b;
    }
    return b;
}
void Back(int k,long long cm)
{
    int sign=1;
    for(int i=x[k-1]+1;i<=K;i++)
    {
        x[k]=i;
        long long aux=cm;
        cm=(cm*D[x[k]])/CMMDC(cm,D[x[k]]);
        if(cm>N)
            return;
        if(k%2==0)
        {
            sol-=(1<<(k-1))*(N/cm);
        }
        else
            sol+=(1<<(k-1))*(N/cm);
        if(k<K)
            Back(k+1,cm);
        cm=aux;
    }
}
void Read()
{
    f>>N>>K;
    for(int i=1;i<=K;i++)
        f>>D[i];
    sort(D+1,D+K+1);
}
int main()
{
    Read();
    Back(1,1);
    g<<sol<<'\n';
    return 0;
}