牛客练习赛76 F.phi and phi(莫比乌斯反演)

牛客练习赛76 F.phi and phi(莫比乌斯反演)

题目链接:https://ac.nowcoder.com/acm/contest/10845/F
题目大意:给定一个正整数 n n n m ∈ [ 1 , n ] , a n s ( m ) = ∑ i = 1 m ∑ j = 1 m φ ( i j ) φ ( gcd ⁡ ( i , j ) ) m \in[1,n], ans(m)=\sum\limits_{i=1}^{m}\sum\limits_{j=1}^{m}\varphi(ij)\varphi(\gcd(i,j)) m[1,n],ans(m)=i=1mj=1mφ(ij)φ(gcd(i,j)) ,求所有 a n s ( m ) ans(m) ans(m) 的值。
题解:既然要求所有式子在 1 1 1 n n n 中所有的取值,不妨假定这个式子存在状态转移方程。同时不难看出 a n s ( m + 1 ) ans(m+1) ans(m+1) a n s ( m ) ans(m) ans(m) 存在联系,我们这样做一下式子的转化:

a n s ( m + 1 ) = ∑ i = 1 m + 1 ( ∑ j = 1 m φ ( i j ) φ ( gcd ⁡ ( i , j ) ) + φ ( i ( m + 1 ) ) φ ( gcd ⁡ ( i , m + 1 ) ) ) = ∑ j = 1 m ∑ i = 1 m + 1 φ ( i j ) φ ( gcd ⁡ ( i , j ) ) + ∑ i = 1 m + 1 φ ( i ( m + 1 ) ) φ ( gcd ⁡ ( i , m + 1 ) ) = ∑ j = 1 m ( ∑ i = 1 m φ ( i j ) φ ( gcd ⁡ ( i , j ) ) + φ ( ( m + 1 ) j ) φ ( gcd ⁡ ( m + 1 , j ) ) ) + ∑ i = 1 m + 1 φ ( i ( m + 1 ) ) φ ( gcd ⁡ ( i , m + 1 ) ) = a n s ( m ) + 2 ( ∑ i = 1 m + 1 φ ( i ( m + 1 ) ) φ ( gcd ⁡ ( i , m + 1 ) ) ) − ( m + 1 ) φ 2 ( m + 1 ) \begin{aligned}ans(m+1)&=\sum\limits_{i=1}^{m+1}(\sum\limits_{j=1}^{m}\varphi(ij)\varphi(\gcd(i,j))+\varphi(i(m+1))\varphi(\gcd(i,m+1))) \\&=\sum\limits_{j=1}^{m}\sum\limits_{i=1}^{m+1}\varphi(ij)\varphi(\gcd(i,j))+\sum\limits_{i=1}^{m+1}\varphi(i(m+1))\varphi(\gcd(i,m+1)) \\&=\sum\limits_{j=1}^{m}(\sum\limits_{i=1}^{m}\varphi(ij)\varphi(\gcd(i,j))+\varphi((m+1)j)\varphi(\gcd(m+1,j)))+\sum\limits_{i=1}^{m+1}\varphi(i(m+1))\varphi(\gcd(i,m+1)) \\&=ans(m)+2(\sum\limits_{i=1}^{m+1}\varphi(i(m+1))\varphi(\gcd(i,m+1)))-(m+1)\varphi^2(m+1) \end{aligned} ans(m+1)=i=1m+1(j=1mφ(ij)φ(gcd(i,j))+φ(i(m+1))φ(gcd(i,m+1)))=j=1mi=1m+1φ(ij)φ(gcd(i,j))+i=1m+1φ(i(m+1))φ(gcd(i,m+1))=j=1m(i=1mφ(ij)φ(gcd(i,j))+φ((m+1)j)φ(gcd(m+1,j)))+i=1m+1φ(i(m+1))φ(gcd(i,m+1))=ans(m)+2(i=1m+1φ(i(m+1))φ(gcd(i,m+1)))(m+1)φ2(m+1)

f ( x ) = ∑ i = 1 x φ ( i x ) φ ( gcd ⁡ ( i , x ) ) f(x)=\sum\limits_{i=1}^x\varphi(ix)\varphi(\gcd(i,x)) f(x)=i=1xφ(ix)φ(gcd(i,x)) ,那么原式为:

a n s ( m + 1 ) = a n s ( m ) + 2 f ( m + 1 ) − ( m + 1 ) φ 2 ( m + 1 ) \begin{aligned}ans(m+1)=ans(m)+2f(m+1)-(m+1)\varphi^2(m+1) \end{aligned} ans(m+1)=ans(m)+2f(m+1)(m+1)φ2(m+1)

现在考虑如何求出函数 f ( x ) f(x) f(x) 的值。首先我们有公式:

φ ( i j ) = φ ( i ) φ ( j ) gcd ⁡ ( i , j ) φ ( gcd ⁡ ( i , j ) ) \begin{aligned}\varphi(ij)=\frac{\varphi(i)\varphi(j)\gcd(i,j)}{\varphi(\gcd(i,j))} \end{aligned} φ(ij)=φ(gcd(i,j))φ(i)φ(j)gcd(i,j)

d = gcd ⁡ ( i , j ) d=\gcd(i,j) d=gcd(i,j) ,证明过程如下:

φ ( i j ) = φ ( i d ) φ ( j d ) φ ( d 2 ) = φ ( i ) φ ( j ) φ ( d ) d φ 2 ( d ) = φ ( i ) φ ( j ) d φ ( d ) \begin{aligned} \varphi(ij)&=\varphi(\frac{i}{d})\varphi(\frac{j}{d})\varphi(d^2) \\&=\frac{\varphi(i)\varphi(j)\varphi(d)d}{\varphi^2(d)} \\&=\frac{\varphi(i)\varphi(j)d}{\varphi(d)} \end{aligned} φ(ij)=φ(di)φ(dj)φ(d2)=φ2(d)φ(i)φ(j)φ(d)d=φ(d)φ(i)φ(j)d

那么,

f ( x ) = ∑ i = 1 x φ ( i ) φ ( x ) gcd ⁡ ( i , x ) = φ ( x ) ∑ i = 1 x ∑ d = 1 x φ ( i ) d [ d = gcd ⁡ ( i , x ) ] = φ ( x ) ∑ d ∣ x d ∑ i = 1 x d φ ( i d ) [ 1 = gcd ⁡ ( i , x d ) ] = φ ( x ) ∑ d ∣ x d ∑ i = 1 x d φ ( i d ) ∑ t ∣ i , t ∣ x d μ ( t ) = φ ( x ) ∑ d ∣ x d ∑ t ∣ x d μ ( t ) ∑ i = 1 x d t φ ( i d t ) = φ ( x ) ∑ T ∣ x ∑ i = 1 x T φ ( i T ) ∑ d ∣ T d μ ( T d ) = φ ( x ) ∑ T ∣ x ∑ i = 1 x T φ ( i T ) φ ( T ) \begin{aligned} f(x)&=\sum\limits_{i=1}^x\varphi(i)\varphi(x)\gcd(i,x) \\&=\varphi(x)\sum\limits_{i=1}^x\sum\limits_{d=1}^x\varphi(i)d[d=\gcd(i,x)] \\&=\varphi(x)\sum\limits_{d\mid x}d\sum\limits_{i=1}^{\frac{x}{d}}\varphi(id)[1=\gcd(i,\frac{x}{d})] \\&=\varphi(x)\sum\limits_{d\mid x}d\sum\limits_{i=1}^{\frac{x}{d}}\varphi(id)\sum\limits_{t\mid i,t\mid \frac{x}{d}}\mu(t) \\&=\varphi(x)\sum\limits_{d\mid x}d\sum\limits_{t\mid\frac{x}{d}}\mu(t)\sum\limits_{i=1}^{\frac{x}{dt}}\varphi(idt) \\&=\varphi(x)\sum\limits_{T\mid x}\sum\limits_{i=1}^{\frac{x}{T}}\varphi(iT)\sum\limits_{d\mid T}d\mu(\frac{T}{d}) \\&=\varphi(x)\sum\limits_{T\mid x}\sum\limits_{i=1}^{\frac{x}{T}}\varphi(iT)\varphi(T) \end{aligned} f(x)=i=1xφ(i)φ(x)gcd(i,x)=φ(x)i=1xd=1xφ(i)d[d=gcd(i,x)]=φ(x)dxdi=1dxφ(id)[1=gcd(i,dx)]=φ(x)dxdi=1dxφ(id)ti,tdxμ(t)=φ(x)dxdtdxμ(t)i=1dtxφ(idt)=φ(x)Txi=1Txφ(iT)dTdμ(dT)=φ(x)Txi=1Txφ(iT)φ(T)

g ( x ) = ∑ T ∣ x ∑ i = 1 x T φ ( i T ) φ ( T ) g(x)=\sum\limits_{T\mid x}\sum\limits_{i=1}^{\frac{x}{T}}\varphi(iT)\varphi(T) g(x)=Txi=1Txφ(iT)φ(T) ,显然, g ( x ) g(x) g(x) 可以利用原始埃式筛 O ( n l o g n ) O(nlogn) O(nlogn) 预处理所有取值。那么对于原式 a n s ( m + 1 ) = a n s ( m ) + 2 φ ( m + 1 ) g ( m + 1 ) − ( m + 1 ) φ 2 ( m + 1 ) ans(m+1)=ans(m)+2\varphi(m+1)g(m+1)-(m+1)\varphi^2(m+1) ans(m+1)=ans(m)+2φ(m+1)g(m+1)(m+1)φ2(m+1) 便可以线性递推确定所有的值了,同时有 a n s ( 1 ) = 1 ans(1)=1 ans(1)=1

#include
using namespace std;
#define debug(x) cerr<<#x<<":"<
#define debug2(x,y) cerr<<#x<<":"<
#define debug3(x,y,z) cerr<<#x<<":"<
typedef long long ll;
const int N=1e6+5,M=1000000007;
int cnt=0,n;
int prime[N],part[N],phi[N],g[N];
void pre(){
    g[1]=sp[1]=phi[1]=1;
    for(int i=2;i<=n;++i){
        if(!part[i]){
            part[i]=prime[cnt++]=i;
            phi[i]=i-1;
        }
        g[i]=((ll)g[i-1]+(ll)phi[i])%M;
        for(int j=0;i*prime[j]<=n;++j){
            part[i*prime[j]]=prime[j];
            if(i%prime[j]) phi[i*prime[j]]=phi[i]*phi[prime[j]];
            else{
                phi[i*prime[j]]=phi[i]*prime[j];
                break;
            }
        }
    }
    for(int i=2;i<=n;++i){
        ll s=0;
        for(int j=i;j<=n;j+=i){
            s=(s+(ll)phi[j])%M;
            g[j]=((ll)g[j]+(ll)phi[i]*s%M)%M;
        }
    }
}
signed main(){
    int A=1;
    scanf("%d",&n);
    pre();
    printf("%d\n",A);
    for(ll i=2;i<=n;++i){
        ll t=(ll)phi[i]*(ll)g[i]%M;
        A=(((ll)A+t+t)%M-(ll)phi[i]*(ll)phi[i]%M*i%M)%M;
        printf("%d\n",(A+M)%M);
    }
}

你可能感兴趣的:(数论,莫比乌斯反演)