容斥原理

在组合数学中,容斥是常常被用到的,用容斥求解一些带有条件的组合数。

容斥原理:具有性质A和性质B的元素个数等同于具有性质A的个数和具有性质B的个数的和再减去同时具有性质A和性质B的元素的个数。 
数学公式表示为 |A∪B|=|A|+|B|-|A∩B|。 
同样以此类推对于三个性质来说其数学公式为|A∪B∪C|=|A|+|B|+|C|-|A∩B|-|A∩C|-|B∩C|+|A∩B∩C| 
为什么要加上最后那个呢?因为在减的过程中多减了一个。

对于容斥原理来说比较常用的方法为递归法和二进制枚举法,二进制枚举的方法最大的好处是枚举出所有元素的子集。假设一个集合的元素有m个,则对于m长的二进制数来说就有m个1或0的位置,对于每一个1 
-就对应一个元素,整个二进制枚举完就是所有子集,从0到2^m就行。

递归法则是利用dfs的思想进行搜索,检索每一种方案进行容斥。由于每一种题都有不同的搜索方法,没用统一的模板,就不弄代码了。

在这其中都是奇数个性质加偶数个性质减,而如果所求的性质是相反的性质,则用总数减去。(这条看了好久才明白,其实容斥就这么一个主要的)
容斥原理开着简单,实际非常复杂,每一道题都用不同的性质容斥,但最终的思想是不变的,多做一些题就会慢慢积累经验最终由一个好的思想。

 Now you get a number N, and a M-integers set, you should find out how many integers which are small than N, that they can divided exactly by any integers in the set. For example, N=12, and M-integer set is {2,3}, so there is another set {2,3,4,6,8,9,10}, all the integers of the set can be divided exactly by 2 or 3. As a result, you just output the number 7.

Input

  There are a lot of cases. For each case, the first line contains two integers N and M. The follow line contains the M integers, and all of them are different from each other. 0<N<2^31,0<M<=10, and the M integer are non-negative and won’t exceed 20.

Output

  For each case, output the number.

Sample Input

12 2
2 3

Sample Output

7

给定一个包括m个数的集合,数不超过20,问有多少个<n的数,能够被集合中的数整除。
关键问题是重复计算,比如6可以被2整除,也可以被3整除,计数了两次,所以要用到容斥原理。
容斥就是对于重叠次数只有奇数次的,我们加上,重叠次数为偶数次的,我们要减去。
对于每个数,都计算重叠一次的,重叠两次的,重叠三次的...最多重叠m次,有m个数
所以要用到DFS,然后保存中间态重叠x次的最小公倍数lcm,符合题意的数有(n-1)/lcm个
(**  1~n之间有多少个能被x整除的数,公式为n/x,题目中要求小于n,所以(n-1)/x  **),根据
step重叠的次数或者加上,或者减去。

比如n=7,m=2,集合中的数为2 3
首先对于2,重叠一次,lcm=2,7/2=3,有3个符合的数(其实是2,4,6),因为重叠次数是奇数,所以
ans+=3,这时中间态lcm=2,然后继续dfs剩下的数(因为要和其它数组和在一块进行重叠,这里只剩下3了),
重叠两次, lcm=LCM(lcm,3), lcm=LCM(2,3)=6,7/6=1,重叠次数为偶数,ans-=1,这样以2开始的所有情况就完了。
再对于3,重叠一次,lcm=3,7/3=2,有两个符合的数,ans+=2.最终ans=4

#include <iostream>
using namespace std;
typedef long long ll;
const int maxn=25;
ll num[maxn];
int cnt;
ll ans;
ll n,m;
ll gcd(ll a,ll b)
{
    if(b==0)
        return a;
    else
        return gcd(b,a%b);
}
ll LCM(ll a,ll b)
{
    return a*b/gcd(a,b);
}
void dfs(int th,ll now,int step)
{
    if(step>cnt)
        return;
    ll lcm=LCM(num[th],now);
    if(step&1)
        ans+=(n-1)/lcm;
    else
        ans-=(n-1)/lcm;
    for(int p=th+1;p<cnt;p++)
        dfs(p,lcm,step+1);
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m)
    {
        cnt=0;
        ans=0;
        ll val;
        while(m--)
        {
            cin>>val;
            if(val>0&&val<n)
            num[cnt++]=val;
        }
        for(int i=0;i<cnt;i++)
        {
            dfs(i,num[i],1);
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值