https://www.acwing.com/problem/content/description/892/
给定一个整数 \(n\) 和 \(m\) 个不同的质数 \(p_1, p_2, ..., p_m\)。
请你求出 1 ∼ \(n\) 中能被 \(p_1, p_2, ..., p_m\) 中的至少一个数整除的整数有多少个。
二进制枚举所有可能。
#include <bits/stdc++.h>
using namespace std;
#define LL long long
int main(){
ios::sync_with_stdio(false);cin.tie(0);
LL n, m;
cin >> n >> m;
vector <LL> p(m);
for (int i = 0; i < m; i ++ )
cin >> p[i];
LL ans = 0;
for (int i = 1; i < (1 << m); i ++ ){
LL t = 1, cnt = 0;
for (int j = 0; j < m; j ++ ){
if (i >> j & 1){
cnt ++ ;
t *= p[j];
if (t > n){
t = -1;
break;
}
}
}
if (t != -1){
if (cnt & 1) ans += n / t;
else ans -= n / t;
}
}
cout << ans << "\n";
return 0;
}
\(dfs\)
#include <bits/stdc++.h>
using namespace std;
#define LL long long
int main(){
ios::sync_with_stdio(false);cin.tie(0);
LL n, m;
cin >> n >> m;
vector <LL> p(m);
for (int i = 0; i < m; i ++ )
cin >> p[i];
LL ans = 0;
function<void(LL, LL, LL)> dfs = [&](LL x, LL s, LL odd){
if (x == m){
if (s == 1) return;
ans += odd * (n / s);
return;
}
dfs(x + 1, s, odd);
if (s <= n / p[x]) dfs(x + 1, s * p[x], -odd);
};
dfs(0, 1, -1);
cout << ans << "\n";
return 0;
}
标签:int,LL,容斥,cin,long,dfs,ans,原理
From: https://www.cnblogs.com/Hamine/p/16610405.html