【莫比乌斯反演+杜教筛】51Nod-1227 平均最小公倍数

【题意】
原题地址
题目大意:见分析。

【解题思路】
显然是反演,然而我的做法和网上题解完全不一样- -,使得我心态爆炸了很久。

题目相当于求 A n s = i = 1 n 1 i j = 1 i l c m ( i , j ) ,开始推柿子。
lcm转化为gcd(跳了一步) i = 1 n j = 1 i j g c d ( i , j )
疯狂改变枚举顺序 d = 1 n i = 1 n d j = 1 i [ g c d ( i , j ) == 1 ] j
d = 1 n d = 1 n d i = 1 n d d j = 1 i ( j d μ ( d ) )
我们设 s ( x ) = x μ ( x ) , f ( x ) = x ( x + 1 ) ( 2 x + 1 ) 2 6 + x ( x + 1 ) 2 2
那么上面的柿子可以化成 d = 1 n d = 1 n d ( s ( d ) f ( n d d ) )
我们发现现在还是求不了答案,于是设 T = d d
使劲化: T = 1 n f ( n T ) d | T s ( d )
g ( x ) = d | x s ( d ) ,观察柿子我们可以发现:
i = 1 n g ( i ) = i = 1 n d | i s ( d ) = d = 1 n i = 1 n d s ( d ) = d = 1 n s ( d ) n d
s u m ( x ) = i = 1 x s ( i ) ,我们可以发现: i = 1 n g ( i ) = i = 1 n s u m ( n d )
接下来的问题是求 s ( x ) g ( x ) ,两个都是积性函数,因为前缀和比较大,考虑用杜教筛。
对于 s ( x ) 那么卷上一个 i d ,得到的实际上就是一个 e
s [ i ] = i = 1 n j | i j μ ( j ) i j = j = 1 n i = 1 n j j μ ( j ) = [ n == 1 ]
i == 1 的情况移过去,得到:
[ n == 1 ] j = 1 n j μ ( j ) = i = 2 n i j = 1 n i j μ ( j )
[ n == 1 ] s ( n ) = i = 2 n i s ( n i )
g ( x ) 类似,然后就没了。

【参考代码】

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
const int N=5e6+10;
const int mod=1e9+7;
const LL inv2=500000004,inv6=166666668;
map<int,LL>mps,mpg;
int pnum,l,r;
int bo[N],pri[N];
LL s[N],g[N];


void init()
{
    s[1]=g[1]=1;
    for(int i=2;i<N;++i)
    {
        if(!bo[i]) pri[++pnum]=i,s[i]=-i,g[i]=(s[i]+s[1])%mod;
        for(int j=1;j<=pnum && i*pri[j]<N;++j)
        {
            bo[i*pri[j]]=1;
            if(!(i%pri[j]))
            {
                g[i*pri[j]]=g[i];
                break;
            }
            s[i*pri[j]]=s[i]*s[pri[j]]%mod;;
            g[i*pri[j]]=g[i]*g[pri[j]]%mod;
        }
    }
    for(int i=2;i<N;++i) (((s[i]+=s[i-1])%mod+mod)%mod);
    for(int i=2;i<N;++i) (((g[i]+=g[i-1])%mod+mod)%mod);
}

LL calcs(int x)
{
    if(x<N) return s[x];
    if(mps[x]) return mps[x];
    LL res=1;
    for(int i=2,las;i<=x;i=las+1)
    {
        las=x/(x/i);
        (res-=(LL)(1ll*las*(las+1)/2%mod-1ll*i*(i-1)/2%mod)*calcs(x/i)%mod)%=mod;
    }
    (res+=mod)%=mod;
    return mps[x]=res;
}

LL calcg(int x)
{
    if(x<N) return g[x];
    if(mpg[x]) return mpg[x];
    LL res=0;
    for(int i=1,las;i<=x;i=las+1)
    {
        las=x/(x/i);
        (res+=1ll*(las-i+1)*calcs(x/i)%mod)%=mod;
    }
    return mpg[x]=res;
}

LL calcf(int x)
{
    LL res=1ll*x*(x+1)%mod*(x*2+1)%mod*inv6%mod+1ll*x*(x+1)/2%mod;
    return res%mod;
}

LL calc(int x)
{
    LL res=0;
    for(int i=1,las;i<=x;i=las+1)
    {
        las=x/(x/i);
        (res+=(calcg(las)-calcg(i-1))*calcf(x/i)%mod+mod)%=mod;
    }
    return res*inv2%mod;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("51Nod1227.in","r",stdin);
    freopen("51Nod1227.out","w",stdout);
#endif 
    init();
    scanf("%d%d",&l,&r);
    printf("%d\n",(calc(r)-calc(l-1)+mod)%mod);

    return 0;
}

【总结】
嗯,我们要多记住一些函数的性质,就不用像我一样推这么久了,直接用 ϕ 的性质就很好做,我这里最后一步还是侥幸化出一个积性函数的东西才搞掉的- -。
而且这个做法杜教筛套杜教筛,十分不优美- -。

猜你喜欢

转载自blog.csdn.net/Dream_Lolita/article/details/81348952
今日推荐