How many integers can you find

Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7456    Accepted Submission(s): 2205

Problem Description
Now you get a number N, and a M-integers set, you should find out how many integers which are small than N, that they can divided exactly by any integers in the set. For example, N=12, and M-integer set is {2,3}, so there is another set {2,3,4,6,8,9,10}, all the integers of the set can be divided exactly by 2 or 3. As a result, you just output the number 7.

Input
There are a lot of cases. For each case, the first line contains two integers N and M. The follow line contains the M integers, and all of them are different from each other. 0<N<2^31,0<M<=10, and the M integer are non-negative and won’t exceed 20.

Output
For each case, output the number.

Sample Input
12 2
2 3

Sample Output
7

AC代码：

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
typedef __int64 LL;
using namespace std;
LL a[105];

LL gcd(LL a,LL b)
{
return b==0?a:gcd(b,a%b);
}
LL solve(LL n,LL m)
{
LL ans=0;
for(LL msk=1; msk<(1<<m); msk++)    //共2^m种组合情况
{
LL mult=1,cnt=0;
for(LL i=0; i<m; i++)
{
if(msk&(1<<i))
{
mult=mult/gcd(mult,a[i])*a[i];  //计算当前组合的最小公倍数
++cnt;
}
}
LL ks=(n-1)/mult;
if(cnt&1)ans+=ks;
else ans-=ks;
}
return ans;
}

int main()
{
LL n,m;
while(~scanf("%I64d%I64d",&n,&m))
{
for(int i=0; i<m; i++)
{
scanf("%I64d",a+i);
if(a[i]==0)i--,m--;
}
printf("%I64d\n",solve(n,m));
}
return 0;
}