码迷,mamicode.com
首页 > 其他好文 > 详细

UVA10325--- The Lottery (容斥)

时间:2015-08-01 19:00:55      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:

#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 (容斥)

标签:

原文地址:http://blog.csdn.net/qingshui23/article/details/47188615

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!