题意
给你k个数问你在1,n里面恰好是奇数个数的倍数的数有多少个
题解
理解了一会容斥原理啊
然后这个就是一个容斥原理的裸题了
但是我觉得一般这种容斥系数都不好看出来。。
但是你可以列一个方程,高斯消元找一波规律就可以了
这题的容斥系数是
(−1)i∗2(i−1)
(
−
1
)
i
∗
2
(
i
−
1
)
CODE:
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long LL;
const LL K=20;
LL a[K];
LL ans;
LL n,k;
LL gcd (LL x,LL y)
{
if (x==0) return y;
return gcd(y%x,x);
}
void dfs (LL x,LL y,LL z)//当前到第几个数了 乘积是多少了 选了多少个了
{
if (y>n) return ;
if (x>k)
{
if (z==0) return ;
LL t;
if (z&1) t=1;
else t=-1;
t=t*(1<<z-1);
//printf("%lld %lld %lld\n",y,z,t);
ans=ans+(n/y)*t;
return ;
}
dfs(x+1,y,z);
dfs(x+1,y*a[x]/gcd(y,a[x]),z+1);
}
int main()
{
LL T;
scanf("%lld",&T);
while (T--)
{
ans=0;
scanf("%lld%lld",&n,&k);
for (LL u=1;u<=k;u++) scanf("%lld",&a[u]);
dfs(1,1,0);
printf("%lld\n",ans);
}
return 0;
}