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;
}
题意:求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;
}
相关文章推荐
- UVA10325 The Lottery(容斥原理)
- UVA 10325 The Lottery(容斥原理)
- UVA 10325 The Lottery(容斥原理)
- UVA10325 The Lottery(容斥原理)
- UVA - 10325 The Lottery(容斥原理)
- uva 10325 The Lottery(组合数学-容斥原理)
- uva 10325 The Lottery(组合数学-容斥原理)
- UVA 10325 The Lottery (组合数学,容斥原理,二进制枚举)
- UVA 10325 The Lottery( 容斥原理)
- uva 10325 The Lottery 枚举子集+容斥原理
- UVA.10325 The Lottery (组合数学 容斥原理 二进制枚举)
- uva 10325 The Lottery (容斥原理)dfs枚举 或二进制枚举
- UVa10325 - The Lottery(容斥原理)
- UVA 10325 The Lottery( 容斥原理)
- 【UVA10325】The Lottery——简单容斥定理
- UVA10325--- The Lottery (容斥)
- UVA 10325 The Lottery
- UVA10325--- The Lottery (容斥)
- UVA 10325 lottery 容斥原理
- UVA10325:The Lottery (容斥)