zoj 2836 容斥原理
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2836
#include <cstdio> #include <cmath> #include <algorithm> #include <iostream> #include <cstring> #include <queue> #include <vector> #define maxn 15 #define INF 0x3f3f3f using namespace std; int a[maxn]; int N; long long ans,M; int gcd(int a,int b){ //printf("%d %d\n",a,b); if(a%b == 0) return b; else return gcd(b,a%b); } int lcm(int a,int b){ if(a<b){ int temp = a; a = b; b = temp; } return a*b/gcd(a,b); } void dfs(int u,int deep,int sum){ for(int i=u+1;i<=N;i++){ int temp = lcm(sum,a[i]); if(temp > M) return; if(deep%2) ans += M/temp; else ans -= M/temp; dfs(i,deep+1,temp); } } int main() { //freopen("input.txt","r",stdin); while(cin>>N>>M){ for(int i=1;i<=N;i++) cin>>a[i]; sort(a+1,a+N+1); ans = 0; dfs(0,1,1); printf("%lld\n",ans); } }