Cod sursa(job #543738)

Utilizator ChallengeMurtaza Alexandru Challenge Data 28 februarie 2011 15:51:58
Problema Light2 Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <fstream>
#include <algorithm>

using namespace std;

const char InFile[]="light2.in";
const char OutFile[]="light2.out";
const int MaxK=32;
const int INF=-1;

ifstream fin(InFile);
ofstream fout(OutFile);

inline long long cmmdc(long long a, long long b)
{
	long long r=a%b;
	while(r)
	{
		a=b;
		b=r;
		r=a%b;
	}
	return b;
}

inline long long cmmmc(long long a, long long b)
{
	return (a*b)/cmmdc(a,b);
}

int T[MaxK],D[MaxK],K,nrone;
long long N,sol;

void back(int k, long long CMMMC)
{
	if(k>D[0])
	{
		if(nrone)
		{
			if((nrone&1)==1)
			{
				sol+=(N/CMMMC)*(1<<(nrone-1));
			}
			else
			{
				sol-=(N/CMMMC)*(1<<(nrone-1));
			}
		}
	}
	else
	{
		back(k+1,CMMMC);
		++nrone;
		back(k+1,cmmmc(CMMMC,D[k]));
		--nrone;
	}
}

int main()
{
	fin>>N>>K;
	for(register int i=1;i<=K;++i)
	{
		fin>>T[i];
	}
	fin.close();

	sort(T+1,T+1+K);
	for(register int i=1;i<=K;++i)
	{
		if(D[D[0]]!=T[i])
		{
			D[++D[0]]=T[i];
		}
		else if(D[0]>0)
		{
			--D[0];
		}
	}
	
	back(1,1);
	
	fout<<sol;
	fout.close();
	return 0;
}