给定一个整数 nn 和 mm 个不同的质数 p1,p2,…,pmp1,p2,…,pm。
请你求出 1∼n1∼n 中能被 p1,p2,…,pmp1,p2,…,pm 中的至少一个数整除的整数有多少个。
输入格式
输出格式
数据范围
1≤m≤161≤m≤16,
1≤n,pi≤1091≤n,pi≤109
输入样例:
输出样例:
7
#include <iostream>#include <cstring>#include <algorithm>using namespace std;typedef long long LL;const int N = 20;int p[N];int n,m;int main() {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个数,有2^m - 1个选法(除了全不选)int t = 1, cnt = 0; //t表示质数乘积,cnt表示有多少个集合for (int j = 0; j < m; ++j) {if (i >> j & 1) { //说明选了这个集合cnt++;if ((LL)t * p[j] > n) {t = -1;break;}t *= p[j];}}if (t != -1) {//奇数个集合就加上 n / t 表示有多少个n 整除t 的个数if (cnt % 2) res += n / t;else res -= n / t;}}cout << res << endl;return 0;}
