Cod sursa(job #2070607)

Utilizator alexpetrescuAlexandru Petrescu alexpetrescu Data 19 noiembrie 2017 18:51:56
Problema Light2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.73 kb
#include <fstream>

using namespace std;

ifstream fin("light2.in");
ofstream fout("light2.out");

#define ll long long

ll ans = 0, lim;
int n, a[22];

inline ll gcd(ll a, ll b) {
    ll r;
    while (b) {
        r = a % b;
        a = b;
        b = r;
    }
    return a;
}

void bkt(int p, ll s, int cnt, bool x) {
    if (s > lim) return ;
    else if (p == n) {
        if (x) ans += (cnt / 2) * (lim / s);
        else ans -= (cnt / 2) * (lim / s);
    } else {
        bkt(p + 1, s, cnt, x);
        bkt(p + 1, s * a[p] / gcd(s, a[p]), cnt * 2, 1 ^ x);
    }
}

int main() {
    fin >> lim >> n;
    for (int i = 0; i < n; i++) fin >> a[i];

    bkt(0, 1, 1, 0);

    fout << ans;

    return 0;
}