Description
商店里面有n张邮票,现在去买一张,然后老板会送若干张(至少一张)邮票。如果老板送的邮票的面值的最大公约数不是1,并且老板送的邮票和我们购买的邮票的面值最大公约数是1,那么就是一组好的邮票组合。问有多少种好的邮票组合。
样例解释:
· 买第1张,送第2张;
· 买第3张,送第2张;
· 买第2张,送第1张;
· 买第2张,送第3张;
· 买第2张,送第1和第3张;
Solution
看到gcd=1,差不多就要用到反演的知识,这题用了
μ
函数,
考虑求出gcd为1的倍数的,再用mu来做,
也就是求出有多少种组合,使得送的邮票的gcd不为1,
先求出有多少种送的方案,使得送的的gcd不为1,再考虑买哪个,
求送的方案这个也用mu来做容斥,+2+3+5-6+7-10….
求完送的方案,那么就枚举一个邮票,看看送的方案有多少,
这个看起来要分解因数,但你打完后会发现,其实上面两个可以一起搞,只用枚举倍数即可,
最后再用mu做一遍即可
复杂度: O(107log(107))
Code
#include <cstdio>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define max(q,w) ((q)<(w)?(w):(q))
using namespace std;
typedef long long LL;
const int N=10000500,mo=1e9+7;
int read(int &n)
{
char ch=' ';int q=0,w=1;
for(;(ch!='-')&&((ch<'0')||(ch>'9'));ch=getchar());
if(ch=='-')w=-1,ch=getchar();
for(;ch>='0' && ch<='9';ch=getchar())q=q*10+ch-48;n=q*w;return n;
}
int m,n;
int a[N];
LL ans,er[N];
int pr[N/3],mu[N],co[N];
bool prz[N];
int main()
{
int q,mx=0;
read(n);
er[0]=1;fo(i,1,n)er[i]=er[i-1]*2%mo;
fo(i,1,n)a[read(q)]++,mx=max(mx,q);
LL T=0;
mu[1]=1;
fo(i,2,mx)
{
if(!prz[i])pr[++pr[0]]=i,mu[i]=-1;
int t=0;
for(int j=i;j<=mx;j+=i)t+=a[j];
co[i]=t;
if(t&&mu[i])
{
T=(T-mu[i]*(er[t]-1LL)%mo)%mo;
ans=(ans+mu[i]*er[t-1]*(LL)t)%mo;
}
fo(j,1,pr[0])
{
t=i*pr[j];
if(t>mx)break;
prz[t]=1;
if(i%pr[j]==0)break;
mu[t]=-mu[i];
}
}
ans=(ans+T*(LL)n)%mo;
fo(i,2,mx)if(co[i]&&mu[i])ans=(ans+mu[i]*(er[co[i]-1]-1LL)%mo*(LL)(co[i]))%mo;
printf("%lld\n",(ans%mo+mo)%mo);
return 0;
}


301

被折叠的 条评论
为什么被折叠?



