HDU 1796 How many integers can you find exclusion principles
How many integers can you findTime Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 4848 Accepted Submission (s): 1388
Problem Description Now you get a number N, and a M-integers set, you shocould find out how many integers which are small than N, that they can divided exactly by any integers in the set. for example, N = 12, and M-integer set is {2, 3}, so there is another set {2, 3, 4, 6, 8, 9, 10 }, all the integers of the set can be divided exactly by 2 or 3. as a result, you just output the number 7.
Input There are a lot of cases. for each case, the first line contains two integers N and M. the follow line contains the M integers, and all of them are different from each other. 0 Output For each case, output the number.
Sample Input
12 22 3
Sample Output
7
Author wangye
Source 2008 "Insigma International Cup" Zhejiang Collegiate Programming Contest-Warm Up (4)
Given that n and m have m elements, evaluate the number of elements smaller than n that are multiples of any element in m.
Basic rejection: ans = number of elements divisible by 1-Number of elements divisible by 2 + number of elements divisible by 3-number of elements divisible by 4 + ....
// 904MS1596K # include
# Include
Int s [27], vis [27], sum, n, m, k; int gcd (int a, int B) // maximum approximate number {return B? Gcd (B, a % B): a;} int lcm (int a, int B) // minimum public multiple {return a/gcd (a, B) * B ;} void dfs (int x, int ans, int now) // x indicates the number of the current number, ans indicates the total number of ans, now indicates the current number of now {if (now = ans) {int a = 1; for (int I = 1; I <= k; I ++) if (vis [I]) a = lcm (a, s [I]); if (ans & 1) sum + = (n-1)/; else sum-= (n-1)/a; return;} for (; x <= k; x ++) if (! Vis [x]) {vis [x] = 1; dfs (x + 1, ans, now + 1); vis [x] = 0 ;}} int main () {while (scanf (% d, & n, & m )! = EOF) {int a; k = 0, sum = 0; for (int I = 1; I <= m; I ++) {scanf (% d, & a); if (a) s [++ k] = a ;}for (int I = 1; I <= k; I ++) {memset (vis, 0, sizeof (vis); dfs (1, I, 0);} printf (% d, sum);} return 0 ;}