How many integers can you find
Time limit:12000/5000 MS (java/others) Memory limit:65536/32768 K (java/others) total submission (s): 6710 Accepted Submission (s): 1946
Problem Description
Now you get a number N, and a m-integers set, you should find out how many integers which is small than N, that they can Divided exactly by any integers in the set. For example, n=12, and M-integer set are {2,3}, so there are another set {2,3,4,6,8,9,10}, all the integers of the set can B e divided exactly by 2 or 3. As a result, you just output the number 7.
Input
There is a lot of cases. For each case, the first line contains integers N and M. The follow line contains the M integers, and all of the them is different from each other. 0<n<2^31,0<m<=10, and the M integer is non-negative and won ' t exceed 20.
Output
For each case, output the number.
Sample Input
12 22 3
Sample Output
7
Author
Wangye
Topic: Given N and a set of size m, the set element is a nonnegative integer. The number of digits divisible by any number in the set for 1...N. n<=2^31,m<=10
#include <cstdio>#include<iostream>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<algorithm>#include<Set>using namespaceStd;typedefLong Longll;typedef unsignedLong LongUll;#defineMM (A, B) memset (A,b,sizeof (a));Const DoubleEPS = 1e-Ten;Const intINF =0x7f7f7f7f;Const DoublePi=acos (-1);Const intmaxn=40000; ll gcd (ll A,ll b) {if(b==0)returnA; Else returnGCD (b,a%b);} ll LCM (ll A,ll b) {return(A/GCD (A, b)) *b;}intn,m,bit,mm[ -],tmp,cnt;ll mult;voidSolveintflag) {mult=1; bit=0; for(intI=0; i<cnt;i++) if(flag& (1<<i)) {mult=LCM (Mm[i],mult); bit++;}}intMain () { while(~SCANF ("%d%d",&n,&m)) {ll ans=0; n--;cnt=0; for(intI=0; i<m;i++) {scanf ("%d",&tmp); if(TMP) mm[cnt++]=tmp; } for(intI=1;i< (1<<CNT); i++) {solve (i); intNum= ((ll) n)/mult; if(bit%2==1) ans+=num; Elseans-=num; } printf ("%lld\n", ans); } return 0;}
Analysis: A very good problem, analysis: The number of points in a given interval can be divisible by any number in the set, the analysis of the problem
It can be found that the number of points in the interval that can be evenly divisible by a single number in the set, summing, will find that the point that can be divisible by two numbers at the same time ( is a multiple of the least common multiple of these two numbers ), so subtract the total number of points that can be divided by two numbers at a time, Then add the number of points that can be subtracted at the same time by three points .... However, this topic has a big pit point, which is the need to go to 0, otherwise it will not only lead to re, but also directly lead to errors, because in the following code, if CNT changed to M. It can be found that the presence of 0 directly leads to the change of ans, so it is necessary to reject the 0 directly when reading the set .
for (int i=1;i< (1<<cnt); i++) { solve (i); int num= (LL) n)/mult; if (bit%2==1) ans+=num; else ans-=num; }
HDU 1796 How many integers can you find the first question