How many integers can you findTime
limit:12000/5000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 4674 Accepted Submission (s): 1340
Problem Description Now I get a number N, and a m-integers set, you should find out how many integers which is small t Han 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
Title translation: Give N and M, and then give a set of M elements, for N-1, how many numbers are the multiples of any element in the set! Write only the last number! Problem-solving ideas: or use the principle of tolerance to seek, but this time to your collection seems to be the previously requested p array elements, but he and p array elements of the requirements are different, because the previously requested p array elements only primes, and arbitrary elements are not the same, but this time is very mixed, only said to be a non-negative number, So we are asking for the first time to ask for some irrelevant elements, such as: 0 and greater than the number of N, this step of the time to deal with the input! Second, the tolerant principle minus overlapping elements, according to the previous, if the element is a and B, then we want to subtract n/(a*b), but for the subject, because a, a, a, a can exist in the Convention number, we have to calculate is n divided by the LCM (A, B), LCM is least common multiple, the specific algorithm is as follows:
#include <stdio.h> #include <algorithm>using namespace std;int p[12],top,ans,n;int gcd (int a,int b) { Return B?GCD (b,a%b): A;} int LCM (int a,int b) { return a/gcd (b) *b;} int NOP (int m) { int i,j,sum=0,flag,num; for (i=1;i<1<<top;i++) { flag=0; Num=1; for (j=0;j<top;j++) if (i& (1<<j)) FLAG++,NUM=LCM (Num,p[j]); if (flag&1) sum+=m/num; else sum-=m/num; } return sum;} int main () { while (~scanf ("%d%d", &n,&top)} {for (int i=0;i<top;i++) { scanf ("%d", &p[i ]); if (p[i]<1| | p[i]>=n) i--, top--; } printf ("%d\n", NOP (n-1)); } return 0;}
HDU 1796 How many integers can you find "repulsion principle"