Cod sursa(job #1343833)

Utilizator MaarcellKurt Godel Maarcell Data 15 februarie 2015 23:00:20
Problema Light2 Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <iostream>
#include <fstream>
#define LL long long int
using namespace std;

LL N,d[30],sum; int K,P;

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

void comb(int cnt, int ind, LL aux){
    if (cnt==0){
        sum+=N/aux;
        return;
    }

    if (aux>N) return;
    if (K-ind+1>cnt)
        comb(cnt,ind+1,aux);

    comb(cnt-1,ind+1,aux*d[ind]/gcd(aux,d[ind]));
}

int main(){
    ifstream fin("light2.in");
    ofstream fout("light2.out");
    fin >> N >> K;

    LL i,res=0;
    for (i=1; i<=K; i++) fin >> d[i];

    for (i=1; i<=K; i++){
        sum=0;
        comb(i,1,1);
        if (i%2) res+=(1<<(i-1))*sum;
        else res-=(1<<(i-1))*sum;
    }

    fout << res;
    return 0;
}