UVA 10325 - The Lottery(容斥)

时间:2023-03-09 01:59:29
UVA 10325 - The Lottery(容斥)

以前做过的一个题,忘记/gcd了,看来需要把以前的东西看一下啊。

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
#define LL long long
int p[],flag[];
LL gcd(LL a,LL b)
{
return b == ?a:gcd(b,a%b);
}
int main()
{
int n,i,j,ans,num,m;
LL temp;
while(scanf("%d%d",&n,&m)!=EOF)
{
ans = ;
memset(flag,,sizeof(flag));
for(i = ;i < m;i ++)
scanf("%d",&p[i]);
sort(p,p+m);
num = ;
for(i = ;i < m;i ++)
{
if(!flag[i])
p[num++] = p[i];
for(j = i+;j < m;j ++)
{
if(p[j]%p[i] == )
flag[j] = ;
}
}
m = num;
for(i = ;i < (<<m);i ++)
{
temp = ;
num = ;
for(j = ;j < m;j ++)
{
if(i&(<<j))
{
temp = temp*p[j]/gcd(temp,p[j]);
num ++;
}
}
if(num%)
ans += n/temp;
else
ans -= n/temp;
}
printf("%d\n",n-ans);
}
return ;
}