题目大意:给定一个集合,求 1∼n−1 有多少个能被集合中至少一个数整除。n<231,∣S∣≤10
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
ll n, m;
ll a[15];
ll gcd(ll x, ll y) {
if (y == 0)
return x;
return gcd(y, x % y);
}
ll lcm(ll x, ll y) {
return x / gcd(x, y) * y;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++)
cin >> a[i];
ll ans = 0;
for (int S = 0; S < (1 << m); S++) {
ll L = 1, cnt = 0;
for (int i = 0; i < m; i++)
if ((S >> i) & 1) {
cnt++;
L = lcm(L, a[i + 1]);
if (L > n)
break;
}
if (cnt % 2 == 1)
ans -= n / L;
else
ans += n / L;
}
cout << n - 1 - ans << endl;
return 0;
}