题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32762
大意:求1~n中不能被给定m个数中任意一个数整除的数的个数
避免超时使用容斥原理来间接求解。其中需要注意的是2,4; 6,8这样的数字
#include <iostream> #include<cstdio> using namespace std; const int maxn=1e6+10; typedef long long LL; LL num[16]; LL gcd(LL a,LL b){ return b?gcd(b,a%b):a; } int main() { //freopen("cin.txt","r",stdin); LL n,m; while(cin>>n>>m){ LL ans=0; for(LL i=0;i<m;i++){ scanf("%lld",&num[i]); } for(LL i=1;i<(1<<m);i++){ LL temp=1,count=0; for(LL j=0;j<m;j++){ if(1<<j&i){ temp=temp*num[j]/gcd(temp,num[j]); //除去公共部分 count++; } if(temp>n)break; } if(count&1)ans+=n/temp; else ans-=n/temp; } printf("%lld\n",n-ans); } return 0; }