Acm:how many integers can you find-the topic of number theory-the simple application of the repulsion principle +GCD

Source: Internet
Author: User

How many integers can you findTime limit:5000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64 U 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.

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

/* /test Instructions: gives the number of N and M input m, finds a multiple of all m numbers and, in multiples of MI, is less than n, and outputs the total number of all numbers. If a number is at the same time a multiple of three numbers, a number of times the number of multiples of C (3,1) =3 two times the number of times C (3,2) =3 three number of times the number is C (3,3) =13-3+1=1, only one time, and so on one count is 5 number of multiples C (5,1) =5c (5,2) =10C (5,3) =10c (5,4) =5c (5,5) =15-10+10-5+1=1 six numbers C (6,1) =6c (6,2) =15c (6,3) =20c (6,4) =15c (6,5) =6c (6,6) =16-15+20-15+6-1= 1 then because the number is not more than 10, you can use the idea of enumerating subsets to do this topic. So use DFS. Finally, there is a place to pay attention is in the DFS inside to determine the product here, to use GCD, at first did not think of a sample. AC Code:/*

#include "map" #include "Cmath" #include "string" #include "Cstdio" #include "vector" #include "CString" #include "iostream "#include" algorithm "using namespace std;typedef long LL; LL A[15];int n,m,cnt; LL ans,x; ll GCD (ll A,ll b) {return B?GCD (b,a%b): A;} void DFS (int x,ll axb,int num) {AXB=A[X]/GCD (A[X],AXB) *axb;if (num&1) ans+= (n-1)/axb;else ans-= (n-1)/axb;//cout << "Now ans is:" <<ans<<endl;  Check for (int i=x+1; i<cnt; i++) DFS (i,axb,num+1);} int main () {while (~scanf ("%d%d", &n,&m)) {ans=0;cnt=0;for (int i=0; i<m; i++) {scanf ("%i64d", &x); if (x!=0 ) A[cnt++]=x;} for (int i=0; i<cnt; i++) {DFS (i,a[i],1);       Use DFS to enumerate the circumstances of each selection. }printf ("%d\n", ans); return 0;}

  

Acm:how many integers can you find-the topic of number theory-the simple application of the repulsion principle +GCD

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.