Cod sursa(job #2240933)

Utilizator PopoviciRobertPopovici Robert PopoviciRobert Data 14 septembrie 2018 15:22:00
Problema Light2 Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <bits/stdc++.h>
#define ll long long

using namespace std;

const ll INF = 1e18;
const int MAXK = 22;

int d[MAXK];

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

int main() {
    FILE *fi, *fout;
    int i, k;
    ll n;
    fi = fopen("light2.in" ,"r");
    fout = fopen("light2.out" ,"w");
    fscanf(fi,"%lld " ,&n);
    fscanf(fi,"%d " ,&k);
    for(i = 0; i < k; i++) {
        fscanf(fi,"%d " ,&d[i]);
    }
    ll ans = 0;
    for(int conf = 1; conf < (1 << k); conf++) {
        ll prd = 1;
        int cnt = 0;
        bool bad = 0;
        for(i = 0; i < k; i++) {
            if(conf & (1 << i)) {
                cnt++;
                prd /= gcd(d[i], prd);
                if(INF / prd < d[i]) {
                    bad = 1;
                    break;
                }
                prd *= d[i];
            }
        }
        if(bad == 0) {
            if(cnt & 1) {
                ans += 1LL * (n / prd) * cnt;
            }
            else {
                ans -= 1LL * (n / prd) * cnt;
            }
        }
    }
    fprintf(fout,"%lld" ,ans);
    fclose(fi);
    fclose(fout);
    return 0;
}