题目描述
位枚举
容斥原理
C++ 代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 20;
int p[N];
int main()
{
int n,m;
cin >> n >> m;
for (int i = 0; i < m; i ++ )cin >> p[i];
int res = 0;
for (int i = 1; i < 1 << m; i ++ ){//枚举m位
int t = 1,cnt = 0;
for (int j = 0; j < m; j ++ ){
if(i>>j&1){//1代表选
cnt ++;
if((long long)t * p[j] > n){
t = -1;
break;
}
t *= p[j];
}
}
if(t != -1){
if(cnt % 2)res += n/t;
else res -= n/t;
}
}
cout << res;
return 0;
}