uva 10325 The Lottery(组合数学-容斥原理)
2014-08-25 20:29
507 查看
The Lottery |
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 notdivisible 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; }
相关文章推荐
- UVA 10325 The Lottery (组合数学,容斥原理,二进制枚举)
- UVA.10325 The Lottery (组合数学 容斥原理 二进制枚举)
- uva 10325 The Lottery(组合数学-容斥原理)
- UVA10325 The Lottery(容斥原理)
- UVa 10325 The Lottery 容斥原理
- UVa 11806 - Cheerleaders (组合数学 容斥原理)
- UVA10325 The Lottery(容斥原理)
- UVA 10325 The Lottery( 容斥原理)
- UVALive 7040 Color (容斥原理 + 组合数学递推公式 + 求逆元 + 基础数论)
- UVA 10325 The Lottery(容斥原理)
- uva 10325 The Lottery (容斥原理)dfs枚举 或二进制枚举
- UVA 10325 The Lottery( 容斥原理)
- uva 10325 The Lottery 枚举子集+容斥原理
- UVALive 6469 Deranged Exams 组合数学+容斥原理
- UVA.11806 Cheerleaders (组合数学 容斥原理 二进制枚举)
- UVALive 7040 Color (容斥原理 + 组合数学递推公式 + 求逆元 + 基础数论)
- UVA 10325 The Lottery(容斥原理)
- UVa10325 - The Lottery(容斥原理)
- UVA - 10325 The Lottery(容斥原理)
- 51NOD 1806 wangyurzee的树(容斥原理 + 组合数学)