How many integers can you find
Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5605 Accepted Submission(s): 1602
Problem Description
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
Author
wangye
容斥,是上一题的简化版.注意M个数 可以有0,有0 的时候要把0去掉
/* *********************************************** Author :pk29 Created Time :2015/8/20 10:01:50 File Name :4.cpp ************************************************ */ #include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <iomanip> #include <list> #include <deque> #include <stack> #define ull unsigned long long #define ll long long #define mod 90001 #define INF 0x3f3f3f3f #define maxn 10000+10 #define cle(a) memset(a,0,sizeof(a)) const ull inf = 1LL << 61; const double eps=1e-5; using namespace std; bool cmp(int a,int b){ return a>b; } ll n,m; ll arr[20]; ll gcd(ll a,ll b){ return a==0?b:gcd(b%a,a); } ll lcm(ll a,ll b){ ll c=gcd(a,b); return a*b/c; } ll solve(){ ll sum=0; n--; for(int i=1;i<(1<<m);i++){ int bits=0; ll mul=1; for(int j=0;j<m;j++){ if(i&(1<<j)){ bits++; mul=lcm(arr[j],mul); } } if(bits&1)sum+=n/mul; else sum-=n/mul; } return sum; } int main() { #ifndef ONLINE_JUDGE //freopen("in.txt","r",stdin); #endif //freopen("out.txt","w",stdout); while(cin>>n>>m){ int cnt=0,x; for(int i=0;i<m;i++){ cin>>x; if(x!=0)arr[cnt++]=x; } m=cnt--; cout<<solve()<<endl; } return 0; }