B - How many integers can you find HDU - 1796 -状压-容斥

  • B - How many integers can you find

  •  HDU - 1796 
  • (1)两个集合容斥关系
  • (2)三个集合容斥关系
  • 公式:
  •  这就是所谓的奇加偶减。
  • #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    int n,m,top,d;
    ll ans,temp,a[55];
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            ans=top=0;
            for(int i=1; i<=m; i++)
            {
                scanf("%d",&d);
                if(d!=0)a[top++]=d;
            }
            d=(1<<top);
            for(int i=1; i<d; i++)
            {
                int cnt=0;
                for(int j=0; j<top; j++)
                {
                    if((1<<j)&i)
                    {
                        cnt++;
                        if(cnt>1)
                            temp=temp*a[j]/__gcd(temp,a[j]);
                        else temp=a[j];
                    }
                }
                if(cnt%2)
                    ans+=(n-1)/temp;
                else ans-=(n-1)/temp;
            }
            printf("%lld\n",ans);
        }
        return 0;
    }
    

猜你喜欢

转载自blog.csdn.net/BePosit/article/details/85197513
今日推荐