uva10325(容斥原理)

题目连接:UVA 10325

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<iostream>
 6 using namespace std;
 7 #define LL long long
 8 LL n,m;
 9 LL a[20];
10 LL gcd(LL a,LL b)
11 {
12     return b==0?a:gcd(b,a%b);
13 }
14 int main()
15 {
16     while(scanf("%lld%lld",&n,&m)!=EOF)
17     {
18         for(int i=0;i<m;i++)
19             scanf("%lld",&a[i]);
20         LL ans=0;
21         for(int i=0;i<(1<<m);i++)
22         {
23             int ct=0;
24             LL temp=1;
25             for(int j=0;j<m;j++)
26             {
27                 if(i&(1<<j)) ct++,temp=temp/gcd(temp,a[j])*a[j];
28             }
29             if(ct&1) ans-=n/temp;
30             else ans+=n/temp;
31         }
32         printf("%lld
",ans);
33     }
34 }
原文地址:https://www.cnblogs.com/yijiull/p/6796730.html