Cod sursa(job #551169)

Utilizator andrei-alphaAndrei-Bogdan Antonescu andrei-alpha Data 10 martie 2011 14:29:01
Problema Light2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.73 kb
using namespace std;

#include <set>
#include <map>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <cctype>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <utility>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define oo (1<<30)
#define f first
#define s second
#define II inline
#define db double
#define ll long long
#define pb push_back
#define mp make_pair
#define Size(V) ((int)(V.size()))
#define all(V) (V).begin() , (V).end()
#define CC(V) memset((V),0,sizeof((V)))
#define CP(A,B) memcpy((A),(B),sizeof((B)))
#define FOR(i,a,b) for(int (i)=(a);(i)<=(b);++(i))
#define REP(i, N) for (int (i)=0;(i)<(int)(N);++(i))
#define FORit(it, x) for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); ++it)

#define IN "light2.in"
#define OUT "light2.out"
#define K_MAX (1<<5)

typedef vector<int> VI;
typedef pair<int,int> pi;
typedef vector<string> VS;
template<class T> string toString(T n) {ostringstream ost;ost<<n;ost.flush();return ost.str();}

ll rez,N;
int K,D[K_MAX];

II void scan()
{
	freopen(IN,"r",stdin);
	freopen(OUT,"w",stdout);
	
	scanf("%lld%d",&N,&K);
	FOR(i,1,K)
		scanf("%d",D+i);
}

II void compute(ll gcd,int nr,int poz)
{
	for(;;++poz)
	{
		if(poz > K)
		{
			rez += 1LL * (nr!=0) * N / gcd * (1<<(nr-1)) * (nr & 1 ? 1 : -1);
			break;
		}
	
		ll newgcd = 1LL * gcd * D[poz] / __gcd(gcd,(ll)D[poz]);
		if(newgcd <= N)
			compute(newgcd,nr + 1,poz + 1);
	}
}

II void solve()
{
	compute(1,0,1);
	printf("%lld\n",rez);
}

int main()
{
	scan();
	solve();
	return 0;
}