Test instructions: Given a number n and then the number of M (m<15) assuming that the M number is a[0],a[1].....a[m-1];
In order to 1~n the number of the number of the non-array A, it is to sift out the multiples of the number of arrays a in 1~n, and the number of the remainder is the result.
The violent run will time out, using the repulsion principle, such as n=10,m=2,a[0]=2,a[1]=3, to sift out all 2 multiples of 1 to 20,
Shilling ans=n=20,ans=ans-n/2=10. Sift out the multiples of 1 to 20 of all 3, ans=ans-n/3=4.
So now the problem, so the screening will lead to 2 and 3 common multiple two screening, that is, 6,12,18 these three numbers,
So to add these three numbers back, ans=ans+n/(2*3) = 7, the final result. And so on, the number of numbers is odd, minus,
The number of numbers is even plus, which is the principle of repulsion.
Note: The number of the subject is not necessarily prime, it may also be composite, the above-mentioned principle of repulsion applies to prime numbers,
Use their least common multiple LCM for the principle of repulsion in the subject.
For example n=10,m=2,a[0]=2,a[1]=4, this set of data, can be verified to use LCM, rather than a[0]*a[1].
#include <iostream>#include<stdio.h>#include<algorithm>using namespaceStd;typedefLong Longll;ll n,m,a[ -],ans;ll gcd (ll a,ll b)//Beg Greatest common divisor{ returnb==0? A:GCD (b,a%b);} ll LCM (ll A,ll b)//Beg least common multiple{ returnA/GCD (A, b) *b;}voidDFS (ll hav,ll cur,ll num)//principle of tolerance and repulsion{ if(hav>n| | CUR>=M)//m=2 m!=15 return ; for(inti=cur;i<m;i++)//Note the I in this and the main function, if it's mixed, it's wrong.{ll temp=LCM (Hav,a[i]); if(num&1) ans-=n/temp;//Odd minus Elseans+=n/temp;//even plusDFS (temp,i+1, num+1); }}intMain () {ll i,j; while(SCANF ("%lld%lld", &n,&m)! =EOF) { for(i=0; i<m;i++) Cin>>A[i]; Ans=N; for(i=0; i<m;i++) {ans-=n/a[i];//Odd minusDFS (a[i],i+1,2); } cout<<ans<<Endl; } return 0;}
UVA10325 The Lottery (tolerant principle)