[ZOJ 2836] Number Puzzle

Source: Internet
Author: User

Number Puzzle Time limit: 2 Seconds Memory Limit: 65536 KB

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

Introduction to the principle of repulsion, see Code

#include <iostream>#include<cstdio>using namespacestd;#definell Long Long#defineN 10intn,m;intA[n];intgcd (ll a,ll b) {returnB?GCD (b,a%b): A;}intLCM (ll a,ll b) {returnA/GCD (A, b) *b;}////notation 1/////intsolve () {intres=0;  for(intI=1;i< (1&LT;&LT;N); i++)    {        intCnt=0; ll LCM=1;  for(intj=0; j<n;j++)        {            if(i& (1&LT;&LT;J)) cnt++,lcm=LCM (Lcm,a[j]); }        if(cnt&1) res+=m/LCM; Elseres-=m/LCM; }    returnRes;}////notation 2/////intans;voidDfsintPosintLcmintCNT) {    if(CNT) {if(cnt&1) ans+=m/LCM; Elseans-=m/LCM; }     for(inti=pos+1; i<n;i++) Dfs (I,LCM (lcm,a[i]), CNT+1);}intMain () { while(SCANF ("%d%d", &n,&m)! =EOF) {         for(intI=0; i<n;i++) scanf ("%d",&A[i]); //printf ("%d\n", Solve ());ans=0; DFS (-1,1,0); printf ("%d\n", ans); }    return 0;}

[ZOJ 2836] Number Puzzle

Related Article

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.