G30 容斥原理 集合的并

视频链接:https://www.bilibili.com/video/BV1S8411h77u/

#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;
}

 

posted @ 2022-10-13 22:28  董晓  阅读(632)  评论(0)    收藏  举报