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;
}
目录
相关文章
uva 10340 all in all
输入两个字符串s和t,判断是否可以从t中删除0个或多个字符(其他字符顺序不变),得到字符串是。
52 0
UVa11076 - Add Again
UVa11076 - Add Again
63 0
uva127 "Accordian" Patience
uva127 "Accordian" Patience
69 0
UVa343 What Base Is This
UVa343 What Base Is This
80 0
|
人工智能
POJ3104---烘干机
POJ3104---烘干机
UVa 10082 WERTYU
Problem Description A common typing error is to place the hands on the keyboard one row to the right of the correct position.
899 0