Home Article Practice 容斥原理

容斥原理

2024-11-25 19:40  views:44  source:mmyyyy    

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 20;
int n, m, prim[N];
int calc(){ //容斥原理
int res = 0;
for(int i = 1; i < 1 << m; i++){//枚举状态
int t = 1, sign = -1;
for(int j = 0; j < m; j++) //过滤状态
if(i & 1 << j){
if((LL)t * prim[j] > n){
t = 0; break;
}
t *= prim[j]; //质数的积
sign = -sign;
}
if(t) res += n / t * sign; //交集的和
}
return res;
}
int main(){
cin >> n >> m;
for(int i = 0; i < m; i++) cin >> prim[i];
cout << calc();
return 0;
}



Disclaimer: The above articles are added by users themselves and are only for typing and communication purposes. They do not represent the views of this website, and this website does not assume any legal responsibility. This statement is hereby made! If there is any infringement of your rights, please contact us promptly to delete it.

字符:    改为:
去打字就可以设置个性皮肤啦!(O ^ ~ ^ O)