ZOJ 2836 number Puzzle (Tolerance principle AH)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.