UVA 10325 The Lottery( 容斥原理)

来源:互联网 发布:看high类似软件 编辑:程序博客网 时间:2024/05/16 03:25
The Sports Association of Bangladesh is in great problem with their latest lottery `Jodi laiga Jai'. There
are so many participants this time that they cannot manage all the numbers. In an urgent meeting they
have decided that they will ignore some numbers. But how they will choose those unlucky numbers!!!
Mr. NondoDulal who is very interested about historic problems proposed a scheme to get free from
this problem.
You may be interested to know how he has got this scheme. Recently he has read the Joseph's
problem.
There are N tickets which are numbered from 1 to N. Mr. Nondo will choose M random numbers
and then he will select those numbers which is divisible by at least one of those M numbers. The
numbers which are not divisible by any of those M numbers will be considered for the lottery.
As you know each number is divisible by 1. So Mr. Nondo will never select 1 as one of those M
numbers. Now given N, M and M random numbers, you have to nd out the number of tickets which
will be considered for the lottery.
Input
Each input set starts with two Integers N (10  N < 231) and M (1  M  15). The next line will
contain M positive integers each of which is not greater than N.
Input is terminated by EOF.
Output
Just print in a line out of N tickets how many will be considered for the lottery.
Sample Input
10 2
2 3
20 2
2 4
Sample Output
3
10

题意: 给定一个数n 再给m个数(m<15) 假设这m个数为 a[0],a[1].....a[m-1];

    求1~n中非数组a的数的倍数的数,就是把1~n中数组a的数的倍数筛掉,剩下的数的个数就是结果。

    暴力跑会超时,利用容斥原理,比如n=10,m=2,a[0]=2,a[1]=3,把1到20中所有2的倍数筛掉,

    先令ans=n=20,ans=ans-n/2=10。再把1到20中所有3的倍数筛掉,ans=ans-n/3=4。

    那么现在问题来了,这么筛选的话会导致2和3的公倍数进行了二次筛选,也就是6,12,18这三个数,

    所以要把这三个数加回来,ans=ans+n/(2*3)=7,得出最终结果。以此类推,数的个数为奇数就减,

    数的个数为偶数就加,这就是容斥原理。

注意 :本题给的数不一定为质数,也可能为合数,上述容斥原理适用于质数,

     在本题中进行容斥原理时要用他们的最小公倍数lcm。

    比如n=10,m=2,a[0]=2,a[1]=4,这组数据,可验证本题要用lcm,而不是a[0]*a[1]。

 第一种方法:搜索遍历

#include <iostream>#include <stdio.h>#include <algorithm>using namespace std;typedef long long ll;ll n,m,a[20],ans;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;}void dfs(ll hav,ll cur,ll num) //容斥原理{    if(hav>n||cur>=m)  //m=2 m!=15    return ;    for(int i=cur;i<m;i++) //注意区分这里的i和主函数里的i,如果混了就错了    {        ll temp=lcm(hav,a[i]);        if(num&1)        ans-=n/temp; //奇数减        else        ans+=n/temp; //偶数加        dfs(temp,i+1,num+1);    }}int main(){    ll i,j;    while(scanf("%lld%lld",&n,&m)!=EOF)    {        for(i=0;i<m;i++)        cin>>a[i];        ans=n;        for(i=0;i<m;i++)        {            ans-=n/a[i]; //奇数减            dfs(a[i],i+1,2);        }        cout<<ans<<endl;    }    return 0;}

 第二种方法:二进制枚举

#include <iostream>#include <stdio.h>#include <algorithm>using namespace std;typedef long long ll;ll n,m,a[20],ans;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;}int main(){    ll i,j;    while(scanf("%lld%lld",&n,&m)!=EOF)    {        for(i=0;i<m;i++)        cin>>a[i];        ans=n;        for(int i=1;i<(1<<m);i++)        {            ll tmp=1,flag=0;            for(int j=0;j<m;j++)            {                if(i&(1<<j))                {                    tmp=lcm(tmp,a[j]);                    if(tmp>n)                    break;                    flag++;                }            }            if(flag&1)            ans-=n/tmp;            else            ans+=n/tmp;        }        cout<<ans<<endl;    }    return 0;}

 

0 0
原创粉丝点击