Cod sursa(job #857462)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 17 ianuarie 2013 20:45:23
Problema Light2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <cstdio>
#include <cassert>

using namespace std;

typedef long long int64;

const int MaxN = 22;

int M, Value[MaxN];
int64 N, Solution;

int64 GCD(int64 x, int64 y) {
    int64 r;
    while (y != 0) {
        r = x % y; x = y; y = r;
    }
    return x;
}

void Back(int K, int Index, int64 LCM) {
    if (Index >= M)
        return;
    Back(K, Index + 1, LCM);
    LCM = LCM * Value[Index] / GCD(LCM, Value[Index]);
    Solution += ((K & 1) == 0 ? 1 : -1) * (N / LCM)  * (1 << K);
    Back(K + 1, Index + 1, LCM);
}

void Read() {
    assert(freopen("light2.in", "r", stdin));
    assert(scanf("%lld\n%d", &N, &M) == 2);
    for (int i = 0; i < M; ++i)
        assert(scanf("%d", &Value[i]) == 1);
}

void Print() {
    assert(freopen("light2.out", "w", stdout));
    printf("%lld\n", Solution);
}

int main() {
    Read();
    Back(0, 0, 1);
    Print();
    return 0;
}