ZOJ 2836 Number Puzzle
Number Puzzle
Time Limit: 2000ms
Memory Limit: 65536KB
This problem will be judged on ZJU. Original ID: 283664-bit integer IO format: %lld Java class name: Main
Given a list of integers ($A_1, A_2, ..., A_n$), and a positive integer M, please find the number of positive integers that are not greater than M and dividable by any integer from the given list.
Input
The input contains several test cases.
For each test case, there are two lines. The first line contains N ($1 \leq N \leq 10$) and $M (1 \leq M \leq 200000000)$, and the second line contains $A_1, A_2, ..., A_n(1 \leq A_i \leq 10, for\ i = 1, 2, ..., N).$
Output
For each test case in the input, output the result in a single line.
Sample Input
3 2
2 3 7
3 6
2 3 7
Sample Output
1
4
Source
Author
MAO, Yiqiang
解题:容斥,求是集合中某些数的LCM的倍数且不超过m的数的数量
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 20; 5 int a[maxn],n,m; 6 LL gcd(LL a,LL b) { 7 return b?gcd(b,a%b):a; 8 } 9 LL LCM(LL a,LL b) { 10 return a/gcd(a,b)*b; 11 } 12 int main() { 13 while(~scanf("%d%d",&n,&m)) { 14 for(int i = 0; i < n; ++i) 15 scanf("%d",a+i); 16 LL ret = 0; 17 for(int i = 1; i < (1<<n); ++i) { 18 int cnt = 0; 19 LL x = 1; 20 for(int j = 0; j < n; ++j) { 21 if((i>>j)&1) { 22 x = LCM(x,a[j]); 23 cnt++; 24 } 25 } 26 if(cnt&1) ret += m/x; 27 else ret -= m/x; 28 } 29 printf("%lld\n",ret); 30 } 31 return 0; 32 }
夜空中最亮的星,照亮我前行