您的位置:首页 > 其它

UVa 10325 The Lottery 容斥原理

2017-08-08 16:06 399 查看
传送门

题意:求1-n中不能被给出的m个数整除的个数

思路:典型容斥原理,但值得注意的是这里要除的是选出的数的组合的最小公倍数

完整代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef long long LL;
LL divv[20];
LL gcd(LL a,LL b)
{
if(b==0) return a;
else gcd(b,a%b);
}
LL lcm(LL a,LL b)
{
LL ans=(a*b)/gcd(a,b);
return ans;
}
int main()
{
LL n,m;
while(scanf("%lld%lld",&n,&m)!=-1)
{
LL ans=0;
for(int i=0;i<m;i++)
scanf("%lld",&divv[i]);
LL S=0;
for(int i=1;i<(1<<m);i++)
{
LL mul=1;
LL cnt=0;
for(int j=0;j<m;j++)
{
if(i&(1<<j))
{
mul=lcm(mul,divv[j]);///除的数是选出来数的最小公倍数!不然就会减/加多了!
cnt++;
}
}
if(cnt&1)
{
S=S+n/mul;
}
else S=S-n/mul;
}
ans=n-S;
printf("%lld\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: