Cod sursa(job #541585)

Utilizator ssergiussSergiu-Ioan Ungur ssergiuss Data 25 februarie 2011 12:21:27
Problema Light2 Scor 10
Compilator cpp Status done
Runda Romanian Master in Mathematics and Sciences 2011, Ziua 1 Marime 1.13 kb
#include <algorithm>
#include <fstream>

using namespace std;

const char Input[] = "light2.in";
const char Output[] = "light2.out";

const int MaxK = 23;

int K;
int d[MaxK];
long long int N, XXX;
long long int pow2[MaxK];

inline int CMMDC( int a, int b ) {

    int r;

    do {

        r = a % b;
        a = b;
        b = r;
    }
    while( r );

    return a;
}

int main() {

    ifstream fin( Input );
    ofstream fout( Output );

    int i, j, cnt;
    long long int aux;

    fin >> N >> K;
    for( i = 1; i <= K; ++i )
        fin >> d[i];

    pow2[0] = 1;
    for( i = 1; i <= K; ++i )
        pow2[i] = (pow2[i - 1] << 1);

    for( i = 1; i < pow2[K]; ++i ) {

        aux = 1;
        cnt = 0;

        for( j = 1; j <= K; ++j )
            if( pow2[j - 1] & i ) {

                aux = aux * d[j] / CMMDC( d[j], aux % d[j] );
                ++cnt;
            }

        if( cnt & 1 )
            XXX += pow2[cnt - 1] * (N / aux);
        else
            XXX -= pow2[cnt - 1] * (N / aux);
    }

    fout << XXX;

    fin.close();
    fout.close();

    return 0;
}