您的位置:首页 > 其它

uva 10325 The Lottery(组合数学-容斥原理)

2014-08-25 20:29 507 查看



The Lottery

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.

The 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 find out the number of tickets which will be considered for
the lottery.

The Input

Each input set starts with two Integers N (10<=N<2^31) 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.

The 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

Md. Kamruzzaman



题目大意:

给定n,m个数,问你1~n中有多少个数不能被m个数任意之一整数?

解题思路:

利用容斥原理,这篇已写过:/article/1615801.html

解题代码:

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

typedef long long ll;
int n,m,a[20];

ll gcd(ll a,ll b){
    return b>0 ? gcd(b,a%b):a;
}

int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        int ans=0;
        vector <int> v;
        for(int i=0;i<m;i++){
            scanf("%d",&a[i]);
            if(a[i]>0) v.push_back(a[i]);
        }
        m=v.size();
        for(int i=1;i<(1<<m);i++){
            int cnt=0;
            ll x=1;
            for(int t=0;t<m;t++){
                if(i&(1<<t)){
                    cnt++;
                    x=x*v[t]/gcd(x,v[t]);
                }
            }
            if( cnt&1 ) ans+=(n)/x;
            else ans-=(n)/x;
        }
        cout<<n-ans<<endl;
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: