HDU 1796 How many integers can you find

Source: Internet
Author: User
Tags greatest common divisor

Links: http://acm.hdu.edu.cn/showproblem.php?pid=1796


How many integers can you find

Time limit:12000/5000 MS (java/others) Memory limit:65536/32768 K (java/others)

Total submission (s): 5612 Accepted Submission (s): 1608


problem DescriptionNow 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.
InputThere 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.
Output

For each case, output the number.


Sample Input
12 22 3

Sample Output
7

AuthorWangye
Source"Insigma International Cup" Zhejiang Collegiate Programming Contest-warm up (4)
recommendWangye | We have carefully selected several similar problems for you:1793 1794 1797 1795 1798
to the effect--given a number n and a M-set with an M. element, you can find out how many positive integers are less than n, and these positive integers are divisible by at least one element in the M-set. Which 0<n<2^31,0<m<=10.

idea--it's easy to see that this is a bare simple repulsion principle. The tolerance principle formula is as follows:

Therefore, we need to find the count of each subset and then calculate it by formula. Because it is a step by step in-depth exploration, so you can use deep search to implement the principle of tolerance, but in the process of searching to add symbolic judgment, so as to get the correct results.

Complexity Analysis--time complexity: O (m+2^cnt), spatial complexity: O (M)

Attach the AC code:
#include <iostream> #include <cstdio> #include <string> #include <cmath> #include <iomanip > #include <ctime> #include <climits> #include <cstdlib> #include <cstring> #include < algorithm> #include <queue> #include <vector> #include <set> #include <map>//#pragma comment (Linker, "/stack:102400000, 102400000") using namespace std;typedef unsigned int li;typedef long long ll;typedef unsigned l Ong long Ull;typedef long double ld;const double pi = ACOs ( -1.0); const DOUBLE E = exp (1.0); const double EPS = 1e-8;const i NT MAXM = 15; Set max count int NUM[MAXM]; The set of m numbers int n, m, CNT; A given number of N, set count m, not zero m set count int ans; The final result is an int gcd (int a, int b); Greatest common divisor int LCM (int a, int b); Seek least common multiple void Dfs (int index, int common, int sign); Use deep search to show the principle of repulsion int main () {Ios::sync_with_stdio (false), while (~SCANF ("%d%d", &n, &m)) {int x;//collection element cnt = 0;while (M --) {scanf ("%d", &x); if (0 = = x)//collection element may contain 0, discard 0 continue;num[cnt++] = x;//Element not zeroDeposit Set}ans = 0; Initializes the final result for (int i=0; i<cnt; i++) Dfs (i, num[i], 1); Enumerates M-sets of non-0 elements for deep search printf ("%d\n", ans);} return 0;} int gcd (int a, int b) {//Euclidean algorithm/divide-and-go method return B? gcd (b, a%b): A;} the int lcm (int a, int b) {//This is written to help prevent intermediate process overflow return A/GCD (A, b) *b;} void Dfs (int index, int common, int sign) {//index of the collection element, least common multiple, symbol judgment (odd items in the formula and even minus) common = LCM (Num[index], common); & 1)//odd item plus ans + = (n-1)/common; Less than n, so use n-1else//even numbers minus ans-= (n-1)/common;for (int i=index+1; i<cnt; ++i) Dfs (i, common, sign+1); The first layer is to find a subset of the elements of the least common multiple//two layer is to find out two elements of a subset of least common multiple count//And so on, all subsets least common multiple counts are found}


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.