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<<N); i++) { intCnt=0; ll LCM=1; for(intj=0; j<n;j++) { if(i& (1<<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