【51NOD 1585】买邮票送邮票

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值