Cod sursa(job #544682)

Utilizator dushmiMihai-Alexandru Dusmanu dushmi Data 1 martie 2011 22:03:28
Problema Light2 Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.76 kb
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
ll n,rc,rez,v[25];
int nr1,k;
void read()
{
  freopen("light2.in","r",stdin);
  freopen("light2.out","w",stdout);
  scanf("%lld",&n);
  scanf("%d",&k);
  for(int i=1;i<=k;i++)
    scanf("%lld",&v[i]);
}
ll cmmdc(ll x,ll y)
{
  ll r=x%y;
  while(r)
  {
    x=y;
    y=r;
    r=x%y;
  }
  return y;
}
void back(int poz)
{
  if(rc>n)
    return;
  if(poz>k)
  {
    if(nr1)
    {
      if((nr1&1)==1)
        rez+=(n/rc)*(1<<(nr1-1));
      else
        rez-=(n/rc)*(1<<(nr1-1));
    }
    return;
  }
  back(poz+1);
  ll aux=cmmdc(rc,v[poz]);
  nr1++;
  rc=rc/aux*v[poz];
  back(poz+1);
  nr1--;
  rc=rc/v[poz]*aux;
}
int main()
{
  read();
  rc=1;
  nr1=0;
  back(1);
  printf("%lld",rez);
  return 0;
}