HDU 1796 How many integers can you find

Source: Internet
Author: User
Tags cmath

How many integers can you findTime limit:12000/5000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 5517 Accepted Submission (s): 1580


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

Authorwangye
Source2008 "Insigma International Cup" Zhejiang Collegiate Programming Contest-warm up (4)
RecommendwangyeThe title means giving an array of N and a set containing the number of M, which requires less than n and is the number of multiples of any element in the set. (a bit around, read it two times to understand). The principle to be used in this topic is the principle of tolerance, what is the principle of repulsion? Probably learned about the set or probability theory! The principle of tolerance: first, regardless of overlap, the number of all objects contained in a certain content is calculated first, and then the number of repeated calculation is excluded, so that the results of the calculation are neither omitted nor duplicated.


the crux of the matter is how to implement the principle of tolerance, the simplest is the direct violence, data comparison of water over, there is a way to use DFS optimization. Two methods are available, just the size of the complexity of the time problem. there is a little bit of a hole in the problem, the less careful people may fall into (such as me ...). T_t ... ), the elements in the collection may contain 0, to filter over before they can be manipulated ... Not much to say, directly on the code:
/*author:zxpxxmemory:1616 kbtime:202 mslanguage:c++result:accepted*/#include <algorithm> #include < iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <string> #include <vector> #include <cmath> #include <ctime> #include <queue> #include <stack> #include < Set> #include <map>using namespace std;typedef long long ll;const int MAXN = 5e5 + 5;const int inf = 0x3f3f3f3f;in T a[130],n,m,ans,p,cur;int gcd (int a,int b) {return!b? A:GCD (b,a%b);} int LCM (int a,int b) {return a/gcd (b) *b;}    void Dfs (int i,int flag,int k) {K=LCM (a[i],k);    ans+=n/(K*flag); for (int j=i+1;j<cur;j++) DFS (J,-FLAG,K);}        int main () {while (~SCANF ("%d%d", &n,&m)) {cur=0;            for (int i=0; i<m; i++) {scanf ("%d", &p);        if (p) a[cur++]=p;        } ans=0;n--;        for (int i=0; i < cur; i++) {DFS (i,1,a[i]); } prinTF ("%d\n", ans); } return 0;}



the code of the search is also coming ...
/*author:zxpxxmemory:1648 kbtime:998 mslanguage:c++result:accepted*/#include <algorithm> #include < iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <string> #include <vector> #include <cmath> #include <ctime> #include <queue> #include <stack> #include < Set> #include <map>using namespace std;typedef long long ll;const int MAXN = 5e5 + 5;const int inf = 0x3f3f3f3f;in T a[130],m,n,p,cur,ans;int gcd (int a,int b) {return!b? A:GCD (b,a%b);} int LCM (int a,int b) {return a/gcd (b) *b;}    int main () {while (~SCANF ("%d%d", &n,&m)) {n--;     cur=0;        ans=0;            for (int i=0; i<m; i++) {scanf ("%d", &p);        if (p) a[cur++]=p;            } for (int i= 1;i < pow (2,cur); i++) {int cnt=0,flag=0,k=1;                    for (int j = 0; j < cur; j + +) {if (I>>j & 1) {K=LCM (k,a[j]); if (k&Gt;n) {flag=1;                    Break                } cnt++;            }} if (flag) continue;            if (cnt%2) ans+=n/k;        else ans-=n/k;    } 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.