Topic Link: POINT here!!!
Test instructions: give you an n (n<=2^31), M (m<=10), and give you M number A[i] (0<=a[i]<=20), ask you how many numbers in the [1,n-1] range can be divisible by one or more of the M numbers.
The puzzle: a[i]=0 naked, pay attention to the situation.
sum = the number that is divisible by 1 numbers-the number that is divisible by 2 numbers + the number that is divisible by 3 numbers-.... (Note that when the number is divisible by multiple numbers, we use the LCM)
Code:
#include <cstdio> #include <cstring> #include <iostream> #include <sstream> #include < algorithm> #include <vector> #include <bitset> #include <set> #include <queue> #include < stack> #include <map> #include <cstdlib> #include <cmath> #define PI 2*asin (1.0) #define LL Long long# Define PB push_back#define pa pair<int,int> #define CLR (A, B) memset (A,b,sizeof (a)) #define Lson lr<<1,l,mid# Define Rson Lr<<1|1,mid+1,r#define Bug (x) printf ("%d++++++++++++++++++++%d\n", x,x) #define Key_value Ch[ch[root ][1]][0]c:\program files\git\binconst ll MOD = 1e9+7;const ll N = 2e5+15;const int MAXN = 5e5+15;const int letter = 130;c onst int INF = 1e17;const double Pi=acos ( -1.0); const double eps=1e-10;using namespace Std;inline int read () {int x=0,f= 1;char Ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} int n,m,a[15]; ll GCD (ll A,LL b) {if (b==0) return A; return gcd (b,a%b);} ll LCM (ll A,ll b) {return a*b/gcd (b);} int main () {while (scanf ("%d%d", &n,&m)!=eof) {for (int i=0;i<m;i++) {scanf ("%d", a+i); if (!a[i]) i--, m--; } n--; LL Sum=0,ans; int num; for (int i=1;i< (1<<m); i++) {Ans=1; num=0; for (int j=0;j<m;j++) {if ((1<<j) &i) {num++; ANS=LCM (Ans,1ll*a[j]); }} if (num%2) Sum+=1ll*n/ans; else Sum-=1ll*n/ans; } printf ("%i64d\n", sum); } return 0;}
hdu1796 How many integers can you find (allowance)