Cod sursa(job #1534893)

Utilizator fluture.godlikeGafton Mihnea Alexandru fluture.godlike Data 24 noiembrie 2015 01:04:16
Problema Light2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <cstdio>

#define in "light2.in"
#define out "light2.out"
#define LL long long
#define BULB 30

using namespace std;
int v[BULB], v1[BULB], m;
LL v2[BULB], ans, n, tmp, nr = 1;

LL gcd(const LL &a, const LL &b)
{
    if(b == 0) return a;
    return gcd(b, a%b);
}
LL cmmmc(const LL &a, const LL &b)
{
    return (a*b)/gcd(a, b);
}
inline void init()
{
    tmp = m;
    ans = n/v[m];
    v1[m-1] = 1;
    v2[nr] = v[m-1];
}
inline void solve()
{
    while(tmp)
    {
        /*if(tmp == m)
        {
            if(nr&1) ans += (n/v2[nr]) * (1LL<<(nr-1)) - (n/cmmmc(v2[nr], v[tmp])) * (1LL<<nr);
            else ans -= (n/v2[nr]) * (1LL<<(nr-1)) - (n/cmmmc(v2[nr], v[tmp])) * (1LL<<nr);
            --tmp;
            continue;
        }*/
        if(!v1[tmp])
        {
            v1[tmp] = 1;
            v2[++nr] = cmmmc(v2[nr-1], v[tmp]);
            tmp = m;
            continue;
        }
        v1[tmp] = 0;
        ++nr;
        --tmp;
    }
}

int main()
{
    freopen(in, "r", stdin);
    freopen(out, "w", stdout);
    scanf("%lld%d", &n, &m);
    for(int i = 1; i<= m; ++i) scanf("%d", &v[i]);
    init();
    solve();
    printf("%lld\n", ans);
    return 0;
}