Cod sursa(job #1239751)

Utilizator paunmatei7FMI Paun Matei paunmatei7 Data 9 octombrie 2014 18:49:45
Problema Light2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.71 kb
#include <cstdio>

#define LL long long

using namespace std;

LL Ans,n;
int k, d[30];

inline LL gcd(LL a,LL b){
    if(!b)
        return a;
    return gcd(b, a % b);
}

void solve(int p, LL x, int s,int put){
    for(int i = p + 1; i <= k; ++i){
        LL cmmmc = x * d[i] / gcd(x, d[i]);
        Ans += n / cmmmc * s * put;
        solve(i, cmmmc, -s, 2 * put);
    }
}

int main(){
    freopen("light2.in", "r", stdin);
    freopen("light2.out", "w", stdout);
    scanf("%lld %d", &n, &k);
    for(int i = 1; i <= k; ++i)
        scanf("%d", &d[i]);
    for(int i = 1; i <= k; ++i){
        Ans += n / d[i];
        solve(i, d[i], -1, 2);
    }
    printf("%lld", Ans);
    return 0;
}