UVA10325--- The Lottery (容斥)

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long  LL;
LL get[18];
LL gcd(LL m, LL n)
{
    if(n == 0)
    return m;
    return gcd(n, m%n);
}

LL lcm(LL a, LL b)
{
    LL t=gcd(a, b);
    return a/t*b;
}
int main()
{
    int n,m;
    while(cin>>m>>n)
    {
        for(int i=0; i<n; i++)
        cin>>get[i];
        int sum=0;
        for(int i=1; i<(1<<n); i++)
        {
            LL ans=1;
            int cnt=0;
            for(int j=0; j<n; j++)
            {
                if(i&(1<<j))
                {
                    ans=lcm(ans,get[j]);
                    if(ans>m)
                    break;
                    cnt++;
                }
            }
            if(ans>m)
            continue;
            if(cnt&1)
            sum+=m/ans;
            else
            sum-=m/ans;
        }
        cout<<m-sum<<endl;
    }
    return 0;
}

你可能感兴趣的:(UVA10325--- The Lottery (容斥))