Cod sursa(job #595844)

Utilizator S7012MYPetru Trimbitas S7012MY Data 14 iunie 2011 16:36:04
Problema Light2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.61 kb
#include <iostream>
#include <fstream>
#define LL long long
using namespace std;

LL n,k,r,d[25];

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

inline LL cmmmc(LL a, LL b) {
    return (a*b)/(gcd(a,b));
}

void back(int p, int nr,LL pas) {
    if(p==k+1 || pas>n) return;
    LL cm=cmmmc(pas,d[p]);
    if(nr&1) r+=((n/cm)*(1<<(nr-1)));
    else r-=((n/cm)*(1<<(nr-1)));
    back(p+1,nr,pas);
    back(p+1,nr+1,cm);
}

int main()
{
    ifstream f("light2.in");
    ofstream g("light2.out");
    f>>n>>k;
    for(int i=1; i<=k; ++i) f>>d[i];
    back(1,1,1);
    g<<r;
    return 0;
}