UVA10325:The Lottery (容斥)

来源:互联网 发布:淘宝店注册公司 编辑:程序博客网 时间:2024/06/05 09:25


题意:给出N和M个数,要求1~N里面有多少个数,其不能被这M个数任意一个整除。

思路:简单容斥,使用队列数组优化。

# include <iostream># include <cstring># include <cstdlib># include <cstdio># define ll long longusing namespace std;ll n, m, a[100000];ll gcd(ll a, ll b){    return b==0?a:gcd(b, a%b);}ll lcm(ll a, ll b){    return a/gcd(a, b)*b;}ll fun(){    int t, k = 16;    ll sum=0;    a[k++] = -1;    for(int i=0; i<m; ++i)    {        t = k;        for(int j=16; j<t; ++j)            a[k++] = lcm(a[i], abs(a[j]))*-1*(a[j]<0?-1:1);    }    for(int i=17; i<k; ++i)        sum += n/a[i];    return sum;}int main(){    while(~scanf("%lld%lld",&n,&m))    {        for(int i=0; i<m; ++i)            scanf("%lld",&a[i]);        printf("%lld\n",n-fun());    }    return 0;}



0 0