Title Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1836
Given a list of integers (A1, A2, ..., an), and a positive integer M, please find the number of positive integers that is Not greater than M and dividable by any integer from the given list.
Input
The input contains several test cases.
For the test case, there is and lines. The first line contains n (1 <= n <=) and M (1 <= M <= 200000000), and the second line contains A1, A2,. ., an (1 <= Ai <=, for i = 1, 2, ..., N).
Output
The For each test is in the input, and the result in a is output.
Sample Input
3 2
2 3 7
3 6
2 3 7
Sample Output
1
4
Author:MAO, Yiqiang
Source:Zhejiang University Local Contest
Ps:http://www.cnblogs.com/bigballon/p/4072487.html
The code is as follows:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream>using namespace STD; #define LL long Longll N, m; LL a[17]; ll GCD (ll A, ll b) {if (b = = 0) return A; Return GCD (b,a%b);} ll LCM (ll A, ll b) {return a * (B/GCD (A, b));} int main () {while (~scanf ("%lld%lld", &n,&m)}) {for (int i = 0; i < n; i++) {scan F ("%lld", &a[i]); } LL ans = 0; for (int i = 1; i < (1<<n); i++)//Using Binary enumeration (traversal) to select the case {LL TT = 1; LL num = 0; for (int j = 0; J < N; j + +) {if ((1<<j) & i) {TT = LCM (Tt,a[j]); num++; }} if (num & 1) {ans + = M/TT; } else {ans-= m/tt; }} printf ("%lld\n", ans); } return 0;}
ZOJ 2836 number Puzzle (Tolerance principle AH)