How many integers can you findTime
limit:12000/5000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 5556 Accepted Submission (s): 1593
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
Main topic:
The number of numbers within n that can be divisible by the number in the given set.
Problem Solving Ideas:
Here we use what we call the principle of repulsion. The so-called tolerant principle, use to remember "odd plus even minus."
For example, 100 can be divisible by the number of 2,3,11,13,41, we Are you (i) 100 can be divisible by i number of numbers.
So the answer is:
U (2) +u (3) +u (one) +u (+u) (41)
-U (2*3)-U (3*11)-U (11*13)-U (13*41)
+u (2*3*11) +u (3*11*13) +u (11*13*41)
-U (2*3*11*13)-U (3*11*13*41)
+u (2*3*11*13*41)
This is called "odd plus even minus".
The number of numbers that can be divisible by I within N is (n-1)/I.
To sum up, we can enumerate the numbers in the collection and then get the answers.
The enumeration has 2 methods: Brute force enumeration and DFS. Since M Max is only 10, when brute force enumeration we can use binary to represent a state, each one represents to go and not fetch. DFS is easy.
Reference code:
/* binary memory:1568 kbtime:639 mslanguage:g++result:accepted*/#include <map> #include <stack> #include < queue> #include <cmath> #include <vector> #include <cctype> #include <cstdio> #include < cstring> #include <iostream> #include <algorithm>using namespace std;const double Eps=1e-10;const int INF =0x3f3f3f3f;const int maxn=25;typedef Long Long ll;int n,m,num[maxn],divi[maxn];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 main () {#ifndef Online_judge freopen ("In.txt", "R", stdin), #endif//Online_judge while (scanf ("%d%d", &n,&m) !=eof) {int cnt=0; for (int i=0;i<m;i++) {scanf ("%d", &num[i]); if (Num[i]) divi[cnt++]=num[i]; } m=cnt; int ans=0; for (int k=1;k< (1<<m); k++) {int select=0,tlcm=1; for (int i=0;i<m;i++) {if (k& (1<<i)) {select++; TLCM=LCM (Tlcm,divi[i]); }} if (select&1) ans+= (n-1)/TLCM; else ans-= (n-1)/TLCM; } printf ("%d\n", ans); } return 0;}
/*dfsmemory:1572 kbtime:109 mslanguage:g++result:accepted*/#include <map> #include <stack> #include < queue> #include <cmath> #include <vector> #include <cctype> #include <cstdio> #include < cstring> #include <iostream> #include <algorithm>using namespace std;const double Eps=1e-10;const int INF =0x3f3f3f3f;const int maxn=25;typedef Long Long ll;int n,m,num[maxn],divi[maxn],ans;int gcd (int a,int b) {return B?GCD ( B,A%B): A;} int LCM (int a,int b) {return a/gcd (b) *b;} void Dfs (int pos,int tlcm,int Select) {//if (pos>m)//return; TLCM=LCM (Tlcm,divi[pos]); select++; if (select&1) ans+= (n-1)/TLCM; else ans-= (n-1)/TLCM; for (int i=pos+1;i<m;i++) DFS (i,tlcm,select);} int main () {#ifndef Online_judge freopen ("In.txt", "R", stdin), #endif//Online_judge while (scanf ("%d%d", &n,&m) !=eof) {int cnt=0; for (int i=0; i<m; i++) {scanf ("%d", &num[i]); if (Num[i]) divi[cnt++]=num[i]; } m=cnt; ans=0; for (int i=0;i<m;i++) DFS (i,1,0); printf ("%d\n", ans); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1796 How many integers can you find (Repulsion principle + binary/dfs)