Cod sursa(job #549755)

Utilizator hadesgamesTache Alexandru hadesgames Data 8 martie 2011 22:03:55
Problema Light2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <cstdio>
FILE *in,*out;
int d[30],k;
long long rez,n;
long long  cmmdc(long long x,long long y)
{
  long long aux;
  while (y)
  {
    aux=x;
    x=y;
    y=aux%y;
  }
  return x;
}
void back(int x,int stare,long long cmmmc)
{
  int semn=1,q;
  if (x==k+1)
  {
    if (stare)
    {
      q=__builtin_popcount(stare);
      if (!(q & 1))
        semn=-1;
      if (q>1)
        rez+=n/cmmmc *(1<<(__builtin_popcount(stare)-1))*semn;
      else
        rez+=n/cmmmc;
    }
    return ;
  }
  long long cmmmc_nou;
  cmmmc_nou=cmmmc/cmmdc(cmmmc,d[x]) * d[x];
  if (cmmmc_nou<=n)
    back(x+1,stare+(1<<(x-1)),cmmmc_nou);
  back(x+1,stare,cmmmc);
}
int main()
{
  int i;
  in=fopen("light2.in","r");
  out=fopen("light2.out","w");
  fscanf(in,"%lld%d",&n,&k);
  for (i=1;i<=k;i++)
    fscanf(in,"%d",&d[i]);
  back(1,0,1);
  fprintf(out,"%lld\n",rez);
}