莫比乌斯反演总结


此总结写于18年10月左右, 忘记放在CSDN上共享了, 现在补一下.
同时提供markdown的源文件供下载修改成自己的模板, 也可在其中查看博客里面渲染失败的公式
链接: https://pan.baidu.com/s/1QjdjJa2ek-7NRBRIB4uDjw 提取码: 8t4c


莫比乌斯反演

目录

  • 约定
  • 基本概念
  • 杜教筛
  • 常用性质
  • 小技巧
  • 线性筛各种积性函数
  • gcd ⁡ \gcd gcd 相关题目
  • l c m lcm lcm 相关题目
  • 约数个数函数相关题目
  • 约数和函数相关题目
  • 欧拉函数相关题目
  • 莫比乌斯函数相关题目
  • 反演的巧妙应用
  • 综合应用
  • min_25筛及其应用

约定

1.任何一个数可以被表示成: P = p 1 q 1 ⋅ p 2 q 2 ⋅ p 3 q 3 ⋯ p t q t P=p_1^{q_{1}}\cdot{p_2^{q_{2}}}\cdot{p_3^{q_{3}}}\cdots{p_t^{q_{t}}} P=p1q1p2q2p3q3ptqt

并约定: q 1 + q 2 + q 3 + . . . + q t = t s u m q_1+q_2+q_3+...+q_t=tsum q1+q2+q3+...+qt=tsum.

2. d ( x ) d(x) d(x)表示 x x x的约数个数,即约数个数函数.

3. σ ( x ) \sigma(x) σ(x)表示 x x x的约数和,即约数和函数.

4. w ( x ) w(x) w(x)表示 x x x的不同质因子个数.

5.未特别注明且 n n n 不一定能整除 k k k n k \frac{n}{k} kn 均表示 ⌊ n k ⌋ \lfloor {\frac{n}{k}} \rfloor kn .

6.未注明范围的题均是 1 0 5 10^5 105 ~ 1 0 12 10^{12} 1012 都可以做的题.

7.题目中未特别注明的题,都根据范围考虑取模或不取模.


基本概念

莫比乌斯函数

莫比乌斯函数是一个由容斥系数所构成的函数. μ ( n ) \mu(n) μ(n) 的定义如下:

  1. n = 1 n=1 n=1 时, μ ( n ) = 1 \mu(n)=1 μ(n)=1
  2. n = ∏ i = 1 t p i n=\prod_{i=1}^tp_i n=i=1tpi p i p_i pi 为不同的素数时, μ ( n ) = ( − 1 ) t \mu(n)=(-1)^t μ(n)=(1)t . 即 n n n 分解质因子后没有幂次大于等于平方的质因子,此时函数值根据分解的个数决定.
  3. n n n 含有任何质因子的幂次大于等于 2 2 2 时, μ ( n ) = 0 \mu(n)=0 μ(n)=0

莫比乌斯反演

  • 定理 : F ( n ) F(n) F(n) f ( n ) f(n) f(n) 是定义在非负整数集合上的两个函数,并且满足条件 :

    F ( n ) = ∑ d ∣ n f ( d ) F(n)=\sum_{d|n}f(d) F(n)=dnf(d)

    那么存在一个结论 :

    f ( n ) = ∑ d ∣ n μ ( d ) ⋅ F ( n d ) f(n)=\sum_{d|n}\mu(d)\cdot{F(\frac{n}{d})} f(n)=dnμ(d)F(dn)

  • 更常用的形式: 如果 F ( n ) F(n) F(n) f ( n ) f(n) f(n) 满足条件:

    F ( n ) = ∑ n ∣ d f ( d ) F(n)=\sum_{n|d}f(d) F(n)=ndf(d)

​ 那么存在一个结论:

f ( n ) = ∑ n ∣ d μ ( d n ) ⋅ F ( d ) f(n)=\sum_{n|d}\mu(\frac{d}{n})\cdot{F(d)} f(n)=ndμ(nd)F(d)


杜教筛

作用: 以低于线性的时间复杂度来计算积性函数的前缀和(常为 O ( n 2 3 ) O(n^{\frac{2}{3}}) O(n32)).

前提 : 对于一个积性函数 f ( i ) f(i) f(i) ,如果知道 ∑ d ∣ n f ( d ) = g ( n ) \sum_{d|n}f(d) = g(n) dnf(d)=g(n) ,且 g ( n ) g(n) g(n) 非常容易求出来,那么就可以用杜教筛快速求出 ∑ i = 1 n f ( i ) \sum_{i=1}^nf(i) i=1nf(i) .

预处理 : 通常预处理出 ∑ i = 1 n f ( i ) \sum_{i=1}^nf(i) i=1nf(i) 的前 n 2 3 n^{\frac{2}{3}} n32 项 .

原理 : 假设现在要求 ∑ i = 1 n φ ( i ) \sum_{i=1}^n \varphi(i) i=1nφ(i) , 我们知道 ∑ d ∣ n φ ( d ) = n \sum_{d|n}\varphi(d) = n dnφ(d)=n .

∑ d ∣ n φ ( d ) = n \sum_{d|n}\varphi(d) = n dnφ(d)=n 拆分移项可以得到 : φ ( n ) = n − ∑ d ∣ n , d < n φ ( d ) \varphi(n) = n - \sum_{d|n,d\lt{n}}\varphi(d) φ(n)=ndn,d<nφ(d)

将式子左右两端同时取和得 : ∑ i = 1 n φ ( i ) = ∑ i = 1 n i − ∑ i = 1 n ∑ d ∣ i , d < i φ ( d ) \sum_{i=1}^n\varphi(i) = {\sum_{i=1}^n i} - \sum_{i=1}^n\sum_{d|i,d\lt{i}}\varphi(d) i=1nφ(i)=i=1nii=1ndi,d<iφ(d) .

考虑将右边的式子更换枚举项由 i i i 变成 i d \frac{i}{d} di 得: ∑ i = 1 n φ ( i ) = ∑ i = 1 n i − ∑ i = 2 n ∑ d = 1 n i φ ( d ) \sum_{i=1}^n\varphi(i) = {\sum_{i=1}^ni} - \sum_{i=2}^n\sum_{d=1}^{\frac{n}{i}}\varphi(d) i=1nφ(i)=i=1nii=2nd=1inφ(d)

此时容易发现可以可以分块+记忆化处理.

常用式子总结:

∑ i = 1 n μ ( i ) = 1 − ∑ i = 2 n ∑ d = 1 n i μ ( d ) \sum_{i=1}^n\mu(i) = 1 - \sum_{i=2}^n\sum_{d=1}^{\frac{n}{i}}\mu(d) i=1nμ(i)=1i=2nd=1inμ(d)

∑ i = 1 n i ⋅ μ ( i ) = 1 − ∑ i = 2 n i ⋅ ∑ d = 1 n i d ⋅ μ ( d ) \sum_{i=1}^ni\cdot\mu(i) = 1 - \sum_{i=2}^ni\cdot\sum_{d=1}^{\frac{n}{i}}d\cdot\mu(d) i=1niμ(i)=1i=2nid=1indμ(d)

∑ i = 1 n φ ( i ) = ∑ i = 1 n i − ∑ i = 2 n ∑ d = 1 n i φ ( d ) \sum_{i=1}^n\varphi(i) = {\sum_{i=1}^ni} - \sum_{i=2}^n\sum_{d=1}^{\frac{n}{i}}\varphi(d) i=1nφ(i)=i=1nii=2nd=1inφ(d)

∑ i = 1 n i ⋅ φ ( i ) = ∑ i = 1 n i 2 − ∑ i = 2 n i ⋅ ∑ d = 1 n i d ⋅ φ ( d ) \sum_{i=1}^ni\cdot\varphi(i) = {\sum_{i=1}^ni^2} - \sum_{i=2}^ni\cdot\sum_{d=1}^{\frac{n}{i}}d\cdot\varphi(d) i=1niφ(i)=i=1ni2i=2nid=1indφ(d)

∑ i = 1 n i 2 ⋅ φ ( i ) = ∑ i = 1 n i 3 − ∑ i = 2 n i 2 ⋅ ∑ d = 1 n i d 2 ⋅ φ ( d ) \sum_{i=1}^ni^2\cdot\varphi(i) = {\sum_{i=1}^ni^3} - \sum_{i=2}^ni^2\cdot\sum_{d=1}^{\frac{n}{i}}d^2\cdot\varphi(d) i=1ni2φ(i)=i=1ni3i=2ni2d=1ind2φ(d)


常用性质

1.设 g ( n ) = ∑ i = 1 n ⌈ n i ⌉ g(n)=\sum_{i=1}^n\lceil{\frac{n}{i}}\rceil g(n)=i=1nin , 那么 g ( n ) = g ( n − 1 ) + d ( n − 1 ) + 1 g(n)=g(n-1)+d(n-1)+1 g(n)=g(n1)+d(n1)+1 , d ( i ) d(i) d(i)为约数个数函数.

2. d ( i ⋅ j ) = ∑ x ∣ i ∑ y ∣ j [ gcd ⁡ ( x , y ) = = 1 ] d(i\cdot{j}) = \sum_{x|i}\sum_{y|j}[\gcd(x,y)==1] d(ij)=xiyj[gcd(x,y)==1].

3. σ ( i ⋅ j ) = ∑ x ∣ i ∑ y ∣ j ( x ⋅ j y ) [ gcd ⁡ ( x , y ) = = 1 ] \sigma(i\cdot{j})=\sum_{x|i}\sum_{y|j} (x\cdot\frac{j}{y}) [\gcd(x,y)==1] σ(ij)=xiyj(xyj)[gcd(x,y)==1] .

4.设 f ( n ) f(n) f(n) 1 1 1 ~ n n n 中无平方因子的数的数量.则 f ( n ) = ∑ i = 1 n μ ( i 2 ) = ∑ i = 1 n μ ( i ) ⋅ n i 2 f(n) =\sum_{i=1}^n\mu(i^2)= \sum_{i=1}^{\sqrt{n}} \mu(i)\cdot\frac{n}{i^2} f(n)=i=1nμ(i2)=i=1n μ(i)i2n

5. ∑ i = 1 n i ⋅ ⌊ n i ⌋ = ∑ i = 1 n ∑ j = 1 n i j = ∑ i = 1 n σ ( i ) \sum_{i=1}^ni\cdot{\lfloor\frac{n}{i}\rfloor}=\sum_{i=1}^n\sum_{j=1}^{\frac{n}{i}}j=\sum_{i=1}^n\sigma(i) i=1niin=i=1nj=1inj=i=1nσ(i) .

6. ∑ d ∣ n μ ( d ) = [ n = = 1 ] \sum_{d|n}\mu(d)=[n==1] dnμ(d)=[n==1]

7. ∑ d ∣ n μ ( d ) d = φ ( n ) n \sum_{d|n}\frac{\mu(d)}{d}=\frac{\varphi(n)}{n} dndμ(d)=nφ(n)

8. ∑ d ∣ n φ ( d ) = n \sum_{d|n}\varphi(d)=n dnφ(d)=n

9. ∑ i = 1 n i ⋅ [ gcd ⁡ ( i , n ) = = 1 ] = n ⋅ φ ( n ) + [ n = = 1 ] 2 \sum_{i=1}^ni\cdot[\gcd(i,n)==1] =\frac{n\cdot\varphi(n)+[n==1]}{2} i=1ni[gcd(i,n)==1]=2nφ(n)+[n==1]

10. 2 w ( n ) = ∑ d ∣ n μ 2 ( d ) 2^{w(n)}=\sum_{d|n}\mu^2(d) 2w(n)=dnμ2(d)

11. φ ( i k ) = i k − 1 ⋅ φ ( i ) \varphi(i^k)=i^{k-1}\cdot\varphi(i) φ(ik)=ik1φ(i)

12. ∑ i = 1 n ∑ j = 1 n [ gcd ⁡ ( i , j ) = = 1 ] = 2 ⋅ ∑ i = 1 n φ ( i ) − 1 \sum_{i=1}^n\sum_{j=1}^n[\gcd(i,j)==1]=2\cdot\sum_{i=1}^n\varphi(i)-1 i=1nj=1n[gcd(i,j)==1]=2i=1nφ(i)1


小技巧

1.对于要求 ∑ i = 1 n f [ i ] x \sum_{i=1}^n\frac{f[i]}{x} i=1nxf[i] , ∏ i = 1 n f [ i ] x \prod_{i=1}^n\frac{f[i]}{x} i=1nxf[i]的情况(f为给定的数组,x为给定的值),如果 f [ i ] f[i] f[i]的范围较小, 可以把 f f f数组里面的值装入桶里,然后做一次前缀和,再根据x值来分块计算.从而将式子转化成: ∑ i = 1 m a x i ⋅ ( s u m [ x ⋅ i + x − 1 ] − s u m [ x ⋅ i − 1 ] ) \sum_{i=1}^{max}i\cdot(sum[x\cdot{i}+x-1]-sum[x\cdot{i}-1]) i=1maxi(sum[xi+x1]sum[xi1]) , ∏ i = 1 m a x i ( s u m [ x ⋅ i + x − 1 ] − s u m [ x ⋅ i − 1 ] ) \prod_{i=1}^{max}i^{(sum[x\cdot{i}+x-1]-sum[x\cdot{i}-1])} i=1maxi(sum[xi+x1]sum[xi1]) .

2.对于一个数组 a 1 , a 2 , a 3 ⋯ a n a_1,a_2,a_3\cdots{a_n} a1,a2,a3an 求每一个元素的逆元, 可以先做一次前缀积得到数组 S S S.然后我们可以快速求出 S n S_n Sn ~ S 1 S_1 S1 的逆元,那么 a n a_n an 的逆元就等于 S n S_n Sn 的逆元乘以 S n − 1 S_{n-1} Sn1 .

下面以斐波那契数列为例求逆元:

void init()
{
    fib[0]=0;fib[1]=1;
    multi_fib[0]=multi_fib[1]=1;
    invfib[0]=invfib[1]=1;
    for(int i=2;i<=maxn;i++){
        fib[i]=(fib[i-1]+fib[i-2])%mod;
        multi_fib[i]=multi_fib[i-1]*fib[i]%mod;//预处理fib的前缀积
    }
    invfib[maxn]=qpow(multi_fib[maxn],mod-2);
    for(int i=maxn;i>=2;i--){
        invfib[i-1]=invfib[i]*fib[i]%mod;//先给invfib[i-1]赋值为前i-1项前缀积的逆元
        invfib[i]=invfib[i]*multi_fib[i-1]%mod;//更新invfib为fib[i]的逆元.
    }
}

3.有些函数本身就可以分块的处理出来的,在范围比较大的时候,可以用杜教筛的思想预处理出前 n 2 3 n^{\frac{2}{3}} n32 项,然后分块的处理后 n 1 3 n^{\frac{1}{3}} n31 项.


线性筛各种积性函数

关键: 线性筛积性函数 f ( n ) f(n) f(n),只需要知道 f ( p ) f(p) f(p) 的值和 f ( p k ) f(p^k) f(pk) 即可做到线性筛.

1.莫比乌斯函数 μ ( i ) \mu(i) μ(i):

f ( p ) = − 1 f(p)=-1 f(p)=1 , f ( p k ) = 0 ( k > 1 ) f(p^k)=0(k>1) f(pk)=0(k>1).

void make()
{
    mu[1]=1;
    for(int i=2;i<=maxn;i++){
        if(!prime[i]) prime[++prime[0]]=i,mu[i]=-1;
        for(int j=1;j<=prime[0]&&prime[j]<=maxn/i;j++){
            prime[i*prime[j]]=1;
            if(i%prime[j]==0) break;
            else mu[i*prime[j]]=-mu[i];
        }
    }
}

2.欧拉函数 φ ( i ) \varphi(i) φ(i) :

f ( p ) = p − 1 , f ( p k ) = f ( p k − 1 ) ⋅ p f(p) = p-1 , f(p^k) = f(p^{k-1}) \cdot {p} f(p)=p1,f(pk)=f(pk1)p

void make()
{
    phi[1]=1;
    for(int i=2;i<=maxn;i++){
        if(!prime[i]) prime[++prime[0]]=i,phi[i]=i-1;
        for(int j=1;j<=prime[0]&&prime[j]<=maxn/i;j++){
            prime[i*prime[j]]=1;
            if(i%prime[j]==0) { phi[i*prime[j]]=phi[i]*prime[j]; break;}
            else phi[i*prime[j]]=phi[i]*phi[prime[j]];
        }
    }
}

3.约数个数函数 d ( i ) : d(i): d(i):

f ( p ) = 2 , f ( p k ) = k + 1 f(p)=2,f(p^k)=k+1 f(p)=2,f(pk)=k+1

void make()
{
    d[1]=1;
    for(int i=2;i<=maxn;i++){
        if(!prime[i]){
            prime[++prime[0]]=i;
            d[i]=2; pow_cnt[i]=1;//pow_cnt记录最小质因子的幂次.
        }
        for(int j=1;j<=prime[0]&&prime[j]<=maxn/i;j++){
            int to=i*prime[j]; prime[to]=1;
            if(i%prime[j]==0){
                pow_cnt[to]=pow_cnt[i]+1;
                d[to]=d[i]/(pow_cnt[i]+1)*(pow_cnt[to]+1);
                break;
            }
            else{
                pow_cnt[to]=1; d[to]=d[i]*d[prime[j]];
            }
        }
    }
}

4.约数和函数 σ ( i ) : \sigma(i): σ(i):

f ( p ) = p + 1 , f ( p k ) = p 0 + p 1 + ⋯ + p k f(p)=p+1,f(p^k)=p^0+p^1+\cdots+p^k f(p)=p+1,f(pk)=p0+p1++pk

void make()
{
    d_sum[1]=1;//表示约数和函数
    for(int i=2;i<=maxn;i++){
        if(!prime[i]){
            prime[++prime[0]]=i;  d_sum[i]=i+1;//素数的话约数和就是i+1.
            prime_pow_count[i]=i+1;//素数对应的最小质因子幂和就是i+1.
        }
        for(int j=1;j<=prime[0]&&prime[j]<=maxn/i;j++){
            int to=i*prime[j]; prime[to]=1;
            if(i%prime[j]==0){//如果不是互素的话,说明当前的i中至少含有一个prime[j].
                prime_pow_count[to]=prime_pow_count[i]*prime[j]+1;
                //先除去之前的,再重新乘上去即可.
                d_sum[to]=d_sum[i]/prime_pow_count[i]*prime_pow_count[to];
                break; 
            }
            else {
                prime_pow_count[to]=prime[j]+1;
                d_sum[to]=d_sum[i]*d_sum[prime[j]];
                //由于约数和函数是积性函数,所以互质的情况下等于直接相乘.
            }
        }
    }
}

5. σ ( i 2 ) \sigma(i^2) σ(i2) :

f ( p ) = p 2 + p + 1 , f ( p k ) = p 0 + p 1 + p 2 + ⋯ + p k + ⋯ + p 2 ⋅ k f(p)=p^2+p+1,f(p^k)=p^0+p^1+p^2+\cdots+p^k+\cdots+p^{2\cdot{k}} f(p)=p2+p+1,f(pk)=p0+p1+p2++pk++p2k

void make()
{
    d_sum_pow_2[1]=1;
    for(int i=2;i<=maxn;i++){
        if(!prime[i]){
            prime[++prime[0]]=i; d_sum_pow_2[i]=i*i+i+1;//素数的话约数和是i^2+i+1.
            prime_pow_2_count[i]=d_sum_pow_2[i];//素数对应的最小质因子幂和就是i^2+i+1.
        }
        for(int j=1;j<=prime[0]&&prime[j]<=maxn/i;j++){
            int to=i*prime[j]; prime[to]=1;
            if(i%prime[j]==0){//如果不是互素的话,说明当前的i中至少含有一个prime[j].
            prime_pow_2_count[to]=prime_pow_2_count[i]*prime[j]*prime[j]+prime[j]+1;
             //先除去之前的,在重新乘上去即可.
          d_sum_pow_2[to]=d_sum_pow_2[i]/prime_pow_2_count[i]*prime_pow_2_count[to];
             break;
            }
            else {
                prime_pow_2_count[to]=prime[j]*prime[j]+prime[j]+1;
                d_sum_pow_2[to]=d_sum_pow_2[i]*d_sum_pow_2[prime[j]];
                //由于约数和函数是积性函数,所以互质的情况下等于直接相乘.
            }
        }
    }
}

6. f ( k ) = ∑ d ∣ k d ∗ μ ( d ) f(k)=\sum_{d|k}d*\mu(d) f(k)=dkdμ(d) :

f ( p ) = 1 − p f(p)=1-p f(p)=1p , f ( p k ) = f ( p ) f(p^k)=f(p) f(pk)=f(p) .

void make()
{
    f[1]=1;
    for(int i=2;i<=maxn;i++){
        if(!prime[i]) prime[++prime[0]]=i,f[i]=1-i;
        for(int j=1;j<=prime[0]&&prime[j]<=maxn/i;j++){
            prime[i*prime[j]]=1;
            if(i%prime[j]==0){ f[i*prime[j]]=f[i]; break; } 
            else f[i*prime[j]]=f[i]*f[prime[j]];
        }
    }
}

7. f ( k ) = ∑ d ∣ k d x ⋅ μ ( k d ) f(k) = \sum_{d|k}d^{x}\cdot\mu(\frac{k}{d}) f(k)=dkdxμ(dk) :

f ( p ) = p x − 1 f(p)=p^{x}-1 f(p)=px1 , f ( p k ) = f ( p k − 1 ) ⋅ p k f(p^k) = f(p^{k-1})\cdot{p^k} f(pk)=f(pk1)pk
​ 具体推的过程可以先取 f ( p ) f(p) f(p) 观察出结果,再取 f ( p 2 ) f(p^2) f(p2) f ( p ) f(p) f(p) 比较,从而得出结论

void make()
{
    f[1]=1;
    for(int i=2;i<=maxn;i++){
        if(!prime[i]){
            prime[++prime[0]]=i;
            g[i]=qpow(i,x);//记录p^x
            f[i]=g[i]-1;
        }
        for(int j=1;j<=prime[0]&&prime[j]<=maxn/i;j++){
            prime[i*prime[j]]=1;
            if(i%prime[j]==0){ f[i*prime[j]]=(f[i]*g[prime[j]])%mod; break; }
            else f[i*prime[j]]=(f[i]*f[prime[j]])%mod;
        }
    }
}

8. f ( k ) = ∑ d ∣ k φ ( d ) ⋅ μ ( k d ) f(k)=\sum_{d|k}\varphi(d)\cdot{\mu(\frac{k}{d})} f(k)=dkφ(d)μ(dk) :

f ( p ) = p − 2 f(p) = p-2 f(p)=p2 , f ( p k ) = p k + p k − 2 − 2 ⋅ p k − 1 f(p^k) = p^k+p^{k-2}-2\cdot{p^{k-1}} f(pk)=pk+pk22pk1

void make()
{
    phimu[1]=1;
    for(int i=2;i<=maxn;i++){
        if(!prime[i]){
            prime[++prime[0]]=i;
            low[i]=i; phimu[i]=i-2;//low数组代表i的最小质因子乘积.
        }
        for(int j=1;j<=prime[0]&&prime[j]<=maxn/i;j++){
            int to=i*prime[j]; prime[to]=1;
            if(i%prime[j]==0){
                low[to]=low[i]*prime[j];
                if(low[i]==i){
                    //如果当前这个合数是一个质因子的幂的形式,单独讨论.(也可以直接在下面讨论)
                    if(i==prime[j]) phimu[to]=prime[j]*prime[j]+1-2*prime[j];
                    //单独讨论为2的情况.
                    else phimu[to]=phimu[i]*prime[j];
                }
                else phimu[to]=phimu[i/low[i]]*phimu[low[i]*prime[j]];
                //将最小质因子部分分割出去.
                break;
            }
            else{
                low[to]=prime[j];
                phimu[to]=phimu[i]*phimu[prime[j]];//互质就直接相乘.
            }
        }
    }
}

9. x ∣ i k , x = ∏ p i q i x|i^k,x=\prod{p_i}^{q_i} xik,x=piqi ,令 f k ( x ) = ∏ p i ⌈ q i k ⌉ f_k(x)=\prod{p_i^{\lceil\frac{qi}{k}\rceil}} fk(x)=pikqi .

​ 比如求 ∑ i = 1 A [ d ∣ i k ] \sum_{i=1}^A[d|i^k] i=1A[dik] ,那么答案就等于 ⌊ A f k ( d ) ⌋ \lfloor\frac{A}{f_k(d)}\rfloor fk(d)A .

​ 可以发现 k = 1 k=1 k=1 的时候, f 1 ( x ) = x f_1(x) = x f1(x)=x ,下面给出 f 2 ( x ) , f 3 ( x ) f_2(x),f_3(x) f2(x),f3(x) 的线性筛代码:

void make()
{
    f2[1]=f3[1]=1;
    for(int i=2;i<=maxn;i++){
        if(!prime[i]){
            prime[++prime[0]]=i;
            low[i]=i;//low数组代表i的最小质因子乘积p^k.
            pow_cnt[i]=1;//pow_cnt记录i的最小质因子的幂.
            f2[i]=i; f3[i]=i;
        }
        for(int j=1;j<=prime[0]&&prime[j]<=maxn/i;j++){
            int to=i*prime[j];  prime[to]=1;
            if(i%prime[j]==0){
                low[to]=low[i]*prime[j];  pow_cnt[to]=pow_cnt[i]+1;
                //如果当前这个合数是一个质因子的幂的形式,单独讨论.(也可以直接在下面讨论)
                if(low[i]==i){
                    f2[to]=f2[i];  f3[to]=f3[i];
                    if(pow_cnt[to]%2==1) f2[to]*=prime[j];//如果恰好多1说明要加1
                    if(pow_cnt[to]%3==1) f3[to]*=prime[j];
                }
                else{
                    f2[to]=f2[i/low[i]]*f2[low[i]*prime[j]];//将最小质因子部分分割出去.
                    f3[to]=f3[i/low[i]]*f3[low[i]*prime[j]];
                }
                break;
            }
            else{
                low[to]=prime[j];  pow_cnt[to]=1;
                f2[to]=f2[i]*f2[prime[j]];//互质就直接相乘.
                f3[to]=f3[i]*f3[prime[j]];
            }
        }
    }
}


gcd ⁡ \gcd gcd 相关题目


题意:

给定 n n n , m m m , d d d ∑ i = 1 n ∑ j = 1 m [ gcd ⁡ ( i , j ) = = d ] \sum_{i=1}^n\sum_{j=1}^m[\gcd(i,j)==d] i=1nj=1m[gcd(i,j)==d] .

题解:

根据反演常用套路,设 :

f ( d ) f(d) f(d) ∑ i = 1 n ∑ j = 1 m [ gcd ⁡ ( i , j ) = = d ] \sum_{i=1}^n\sum_{j=1}^m[\gcd(i,j)==d] i=1nj=1m[gcd(i,j)==d] . F ( n ) F(n) F(n) ∑ i = 1 n ∑ j = 1 m d ∣ gcd ⁡ ( i , j ) \sum_{i=1}^n\sum_{j=1}^m{d}|\gcd(i,j) i=1nj=1mdgcd(i,j) .

则: ans = f ( d ) f(d) f(d)

​ = ∑ d ∣ k μ ( k d ) ⋅ F ( k ) \sum_{d|k}\mu(\frac{k}{d})\cdot{F(k)} dkμ(dk)F(k)

可以观察出: 对答案有贡献的 k k k 均是 d d d 的倍数,而 d d d 的倍数是有上限的,因此考虑更换枚举项为 k d \frac{k}{d} dk .

​ = ∑ k = 1 m i n ( n d , m d ) μ ( k ) ⋅ F ( k ⋅ d ) \sum_{k=1}^{min(\frac{n}{d},\frac{m}{d})}\mu(k)\cdot{F(k\cdot{d})} k=1min(dn,dm)μ(k)F(kd)

由于 F ( k ) = n k ⋅ m k F(k)=\frac{n}{k}\cdot\frac{m}{k} F(k)=knkm. 考虑后面分块处理即可解决问题.

​ — P 3455 P3455 P3455


题意:

给定 T T T , n n n , m m m , T T T 次询问, 输出 ∑ i = 1 n ∑ j = 1 m [ gcd ⁡ ( i , j ) = = P r i m e ] \sum_{i=1}^n\sum_{j=1}^m[\gcd(i,j)==Prime] i=1nj=1m[gcd(i,j)==Prime]

T ≤ 1 0 4 T\le{10^4} T104 , N , M ≤ 1 0 6 N,M\le 10^6 N,M106 .

题解:

根据反演常用套路,设 :

f ( d ) f(d) f(d) ∑ i = 1 n ∑ j = 1 m [ gcd ⁡ ( i , j ) = = d ] \sum_{i=1}^n\sum_{j=1}^m[\gcd(i,j)==d] i=1nj=1m[gcd(i,j)==d] . F ( n ) F(n) F(n) ∑ i = 1 n ∑ j = 1 m d ∣ gcd ⁡ ( i , j ) \sum_{i=1}^n\sum_{j=1}^m{d}|\gcd(i,j) i=1nj=1mdgcd(i,j) .

设: S S S 为质数集.

​ ans = ∑ p ∈ S ∑ i = 1 n ∑ j = 1 m [ gcd ⁡ ( i , j ) = = p ] \sum_{p \in S} \sum_{i=1}^n\sum_{j=1}^m[\gcd(i,j)==p] pSi=1nj=1m[gcd(i,j)==p]

​ = ∑ p ∈ S f ( p ) \sum_{p \in S} f(p) pSf(p)

​ = ∑ p ∈ S ∑ p ∣ d μ ( d p ) ⋅ F ( d ) \sum_{p \in S} \sum_{p|d} \mu(\frac{d}{p})\cdot{F(d)} pSpdμ(pd)F(d) (一般把分块的部分和 μ \mu μ 函数分开,所以需要更换枚举项)

​ = ∑ p ∈ S ∑ d = 1 m i n ( n p , m p ) μ ( d ) ⋅ F ( d ⋅ p ) \sum_{p \in S} \sum_{d=1}^{min(\frac{n}{p},\frac{m}{p})}\mu(d)\cdot{F(d\cdot{p})} pSd=1min(pn,pm)μ(d)F(dp) (由枚举 d d d 变成 d p \frac{d}{p} pd )

由于此时枚举 p p p 的部分在最外面无法预处理,所以更换枚举项,改成枚举 d ∗ p d*p dp.

​ = ∑ k = 1 m i n ( n , m ) F ( k ) ⋅ ∑ p ∣ k , p ∈ S μ ( k p ) \sum_{k=1}^{min(n,m)}F(k)\cdot\sum_{p|k,p \in S} \mu(\frac{k}{p}) k=1min(n,m)F(k)pk,pSμ(pk)

此时容易发现后面一部分可以预处理处理.前一部分可以分块.

​ — P 2257 P2257 P2257


题意:

求: ∑ i = 1 n ∑ j = 1 n gcd ⁡ ( i , j ) \sum_{i=1}^n\sum_{j=1}^n\gcd(i,j) i=1nj=1ngcd(i,j) , n ≤ 1 0 10 n\le 10^{10} n1010

题解:

对于两个 ∑ \sum 前后都是 n n n 的情况,通常将后面的 ∑ \sum 上限换成 i i i ,然后减去多余的部分.

​ ans = ( 2 ⋅ ∑ i = 1 n ∑ j = 1 i gcd ⁡ ( i , j ) ) − ∑ i = 1 n i (2\cdot\sum_{i=1}^n\sum_{j=1}^i\gcd(i,j) )-\sum_{i=1}^ni (2i=1nj=1igcd(i,j))i=1ni

设 ans ′ ^{'} = ∑ i = 1 n ∑ j = 1 i gcd ⁡ ( i , j ) \sum_{i=1}^n\sum_{j=1}^i\gcd(i,j) i=1nj=1igcd(i,j)

​ ans ′ ^{'} = ∑ d = 1 n d ⋅ ∑ i = 1 n d ∑ j = 1 i [ gcd ⁡ ( i , j ) = = 1 ] \sum_{d=1}^nd\cdot\sum_{i=1}^{\frac{n}{d}}\sum_{j=1}^i [\gcd(i,j)==1] d=1ndi=1dnj=1i[gcd(i,j)==1]

此时容易发现 ∑ j = 1 i [ gcd ⁡ ( i , j ) = = 1 ] \sum_{j=1}^i[\gcd(i,j)==1] j=1i[gcd(i,j)==1] i i i 以内与 i i i 互质的数的个数 ,因此原式再次化简为:

​ = ∑ d = 1 n d ⋅ ∑ i = 1 n d φ ( i ) \sum_{d=1}^nd\cdot\sum_{i=1}^{\frac{n}{d}}\varphi(i) d=1ndi=1dnφ(i)

​ = ∑ i = 1 n φ ( i ) ⋅ ∑ d = 1 n i d \sum_{i=1}^n\varphi(i)\cdot\sum_{d=1}^{\frac{n}{i}}d i=1nφ(i)d=1ind

此时可用杜教筛处理出 ∑ i = 1 n φ ( i ) \sum_{i=1}^n\varphi(i) i=1nφ(i) ,即可解决该题.

​ — 51 N o d 1237 51Nod 1237 51Nod1237


题意:

∑ i = 1 n ∑ j = 1 m [ gcd ⁡ ( i , j ) = = k ] [ t s u m ≤ P ] \sum_{i=1}^n\sum_{j=1}^m[\gcd(i,j)==k][tsum\leq{P}] i=1nj=1m[gcd(i,j)==k][tsumP] (tsum为k的质数幂和,P为题目给定)

1 ≤ n , m ≤ 1 0 5 1\le{n,m}\le10^5 1n,m105, 0 ≤ P ≤ 1 0 5 0\le{P}\le10^5 0P105. Q次询问, Q ≤ 1 0 5 Q\le10^5 Q105.( 1的tsum为0 ).

题解:

用f(d)表示满足 d = gcd ⁡ ( i , j ) , 且 1 ≤ i ≤ n , 1 ≤ j ≤ m d=\gcd(i,j),且 1\le{i}\le{n},1\le{j}\le{m} d=gcd(i,j),1in,1jm的对数.

用F(d)表示满足 d ∣ gcd ⁡ ( i , j ) , 且 1 ≤ i ≤ n , 1 ≤ j ≤ m ​ d|\gcd(i,j),且 1\le{i}\le{n},1\le{j}\le{m}​ dgcd(i,j),1in,1jm的对数.

可知: F ( d ) = ⌊ n d ⌋ ​ F(d)=\lfloor\frac{n}{d}\rfloor​ F(d)=dn ⋅ ⌊ m d ⌋ ​ \cdot\lfloor\frac{m}{d}\rfloor​ dm , f ( x ) = ∑ x ∣ d μ ( d x ) ⋅ F ( d ) ​ f(x) = \sum_{x|d}\mu(\frac{d}{x})\cdot{F(d)}​ f(x)=xdμ(xd)F(d).

设k为满足 t s u m ≤ P ​ tsum\le{P}​ tsumP的数. 则枚举每一个k既是答案:

​ ans = ∑ k f ( k ) ​ \sum_kf(k)​ kf(k)

​ = ∑ k ∑ k ∣ d μ ( d k ) ⋅ F ( d ) \sum_k\sum_{k|d}\mu(\frac{d}{k})\cdot{F(d)} kkdμ(kd)F(d)

​ = ∑ d = 1 m i n ( n , m ) F ( d ) ⋅ ∑ k ∣ d μ ( d k ) \sum_{d=1}^{min(n,m)}F(d)\cdot\sum_{k|d}\mu(\frac{d}{k}) d=1min(n,m)F(d)kdμ(kd) (更换枚举项)

此时发现后面的可以线性筛出来后做前缀和即可.由于tsum最多不超过20.所以线性筛的时候记录

一下即可.

const int maxn = 5e5+7;
int prime[maxn+5],mu[maxn+5],pow_cnt[maxn+5];//pow_cnt记录i的质因子幂和.(出现+1即可).
long long sum[21][maxn+5];
void init()
{
    make_mu();
    for(int i=1;i<=maxn;i++)
        for(int j=i;j<=maxn;j+=i)
            sum[pow_cnt[i]][j]+=mu[j/i];//质因子个数为pow_cnt[i],能贡献的所有j.
    for(int i=1;i<=20;i++)
        for(int j=1;j<=maxn;j++)
            sum[i][j]+=sum[i-1][j];
    for(int i=0;i<=20;i++)
        for(int j=1;j<=maxn;j++)
            sum[i][j]+=sum[i][j-1];
}
int main()
{
    scanf("%d",&t);
    while(t--){
        scanf("%d%d%d",&n,&m,&p);
        if(p>20){printf("%lld\n",1LL*n*m);continue;}
        long long ans=0;
        for(int l=1,r;l<=min(n,m);l=r+1){
            r=min(n/(n/l),m/(m/l));
            ans+=1LL*(n/l)*(m/l)*(sum[p][r]-sum[p][l-1]);
        }
        printf("%lld\n",ans);
    }
}

​ — h d u 4746 hdu4746 hdu4746


l c m lcm lcm 相关题目


题意:

∑ i = 1 n ∑ j = 1 m L C M ( i , j ) \sum_{i=1}^n\sum_{j=1}^mLCM(i,j) i=1nj=1mLCM(i,j) , n , m ≤ 1 0 7 n,m\le 10^7 n,m107 .

题解:

由于 L C M ( i , j ) = i ⋅ j gcd ⁡ ( i , j ) LCM(i,j) = \frac{i\cdot{j}}{\gcd(i,j)} LCM(i,j)=gcd(i,j)ij .

​ ans = ∑ i = 1 n ∑ j = 1 m i ⋅ j gcd ⁡ ( i , j ) ​ \sum_{i=1}^n\sum_{j=1}^m\frac{i\cdot{j}}{\gcd(i,j)}​ i=1nj=1mgcd(i,j)ij

​ = ∑ d = 1 m i n ( n , m ) ∑ i = 1 n d ∑ j = 1 m d i ⋅ j ⋅ d ⋅ [ gcd ⁡ ( i , j ) = = 1 ] ​ \sum_{d=1}^{min(n,m)}\sum_{i=1}^{\frac{n}{d}}\sum_{j=1}^{\frac{m}{d}}i\cdot{j}\cdot{d}\cdot[\gcd(i,j)==1]​ d=1min(n,m)i=1dnj=1dmijd[gcd(i,j)==1] (考虑枚举 gcd ⁡ ( i , j ) ​ \gcd(i,j)​ gcd(i,j))

​ = ∑ d = 1 m i n ( n , m ) ∑ i = 1 n d ∑ j = 1 m d i ⋅ j ⋅ d ⋅ ∑ k ∣ gcd ⁡ ( i , j ) μ ( k ) \sum_{d=1}^{min(n,m)}\sum_{i=1}^{\frac{n}{d}}\sum_{j=1}^{\frac{m}{d}}i\cdot{j}\cdot{d}\cdot \sum_{k|\gcd(i,j)}\mu(k) d=1min(n,m)i=1dnj=1dmijdkgcd(i,j)μ(k) (考虑将 [ gcd ⁡ ( i , j ) = = 1 ] [\gcd(i,j)==1] [gcd(i,j)==1] 更换为 μ \mu μ)

​ = ∑ d = 1 min ⁡ ( n , m ) d ⋅ ∑ k = 1 min ⁡ ( n d , m d ) k 2 ⋅ μ ( k ) ⋅ ∑ i = 1 n k ⋅ d i ⋅ ∑ j = 1 m k ⋅ d j \sum_{d=1}^{\min(n,m)}{d}\cdot\sum_{k=1}^{\min(\frac{n}{d},\frac{m}{d})}{k^2}\cdot\mu(k)\cdot\sum_{i=1}^{\frac{n}{k\cdot{d}}}{i}\cdot\sum_{j=1}^{\frac{m}{k\cdot{d}}}{j} d=1min(n,m)dk=1min(dn,dm)k2μ(k)i=1kdnij=1kdmj (考虑枚举 k)

此时枚举 d ​ d​ d 即可过题. 但还可以通过更换枚举项将复杂度再次降低.

由于 ∑ i = 1 x i ​ \sum_{i=1}^{x}i​ i=1xi 可以 O ( 1 ) ​ O(1)​ O(1) 的计算出,所以我们用 f ( x ) ​ f(x) ​ f(x) 来代表 ∑ i = 1 x i ​ \sum_{i=1}^xi​ i=1xi .

​ = ∑ d = 1 m i n ( n , m ) f ( n d ) ⋅ f ( m d ) ⋅ d ⋅ ∑ k ∣ d k ⋅ μ ( k ) ​ \sum_{d=1}^{min(n,m)}f(\frac{n}{d})\cdot{f(\frac{m}{d})}\cdot{d}\cdot\sum_{k|d}k\cdot\mu(k)​ d=1min(n,m)f(dn)f(dm)dkdkμ(k)

考虑预处理出 d ⋅ ∑ k ∣ d k ⋅ μ ( k ) d\cdot\sum_{k|d}k\cdot\mu(k) dkdkμ(k) .设 g ( d ) = ∑ k ∣ d k ⋅ μ ( k ) g(d)=\sum_{k|d}k\cdot\mu(k) g(d)=kdkμ(k) .由于 g ( d ) g(d) g(d) 是积性函数,所以可以线性筛出.

最后做次前缀和即可解决这道题.

P 1829 P1829 P1829


题意:

求: ∑ i = 1 n ∑ j = 1 n L C M ( i , j ) \sum_{i=1}^n\sum_{j=1}^nLCM(i,j) i=1nj=1nLCM(i,j) , n ≤ 1 0 10 n\le 10^{10} n1010

题解:

对于两个 ∑ \sum 前后都是 n n n 的情况,通常将后面的 ∑ \sum 上限换成 i i i ,然后减去多余的部分.

​ ans = ( 2 ⋅ ∑ i = 1 n ∑ j = 1 i i ⋅ j gcd ⁡ ( i , j ) ) − ∑ i = 1 n i (2\cdot\sum_{i=1}^n\sum_{j=1}^i\frac{i\cdot{j}}{\gcd(i,j)} )-\sum_{i=1}^ni (2i=1nj=1igcd(i,j)ij)i=1ni

设 ans ′ ^{'} = ∑ i = 1 n ∑ j = 1 i i ⋅ j gcd ⁡ ( i , j ) \sum_{i=1}^n\sum_{j=1}^i\frac{i\cdot{j}}{\gcd(i,j)} i=1nj=1igcd(i,j)ij

​ ans ′ ^{'} = ∑ d = 1 n d ⋅ ∑ i = 1 n d ∑ j = 1 i i ⋅ j ⋅ [ gcd ⁡ ( i , j ) = = 1 ] \sum_{d=1}^nd\cdot\sum_{i=1}^{\frac{n}{d}}\sum_{j=1}^ii\cdot{j}\cdot [\gcd(i,j)==1] d=1ndi=1dnj=1iij[gcd(i,j)==1]

此时容易发现 ∑ j = 1 i j ⋅ [ gcd ⁡ ( i , j ) = = 1 ] \sum_{j=1}^ij\cdot[\gcd(i,j)==1] j=1ij[gcd(i,j)==1] i i i 以内与 i i i 互质的数之和 ,因此原式再次化简为:

​ = ∑ d = 1 n d ⋅ ∑ i = 1 n d i 2 ⋅ φ ( i ) 2 \sum_{d=1}^nd\cdot\sum_{i=1}^{\frac{n}{d}}\frac{i^2\cdot\varphi(i)}{2} d=1ndi=1dn2i2φ(i)

​ = ∑ i = 1 n i 2 ⋅ φ ( i ) 2 ∑ d = 1 n i d \sum_{i=1}^n\frac{i^2\cdot\varphi(i)}{2}\sum_{d=1}^{\frac{n}{i}}d i=1n2i2φ(i)d=1ind

此时可用杜教筛处理出 ∑ i = 1 n i 2 ⋅ φ ( i ) 2 \sum_{i=1}^n\frac{i^2\cdot\varphi(i)}{2} i=1n2i2φ(i) ,即可解决该题.

​ — 51 N o d 1238 51Nod1238 51Nod1238


题意:

求满足 a ≤ l c m ( x , y ) ≤ b {a}\le { lcm(x,y) } \le {b} alcm(x,y)b x ≤ y x\le{y} xy 的二元组 ( x , y ) (x,y) (x,y) 的数量. 1 ≤ a , b ≤ 1 0 11 1\le{a,b}\le{10^{11}} 1a,b1011

题解:

容易发现可以答案可以转化为求满足 1 ≤ l c m ( x , y ) ≤ b 1\le{lcm(x,y)}\le{b} 1lcm(x,y)b 的数量减去 1 ≤ l c m ( x , y ) ≤ ( a − 1 ) 1\le{lcm(x,y)}\le{(a-1)} 1lcm(x,y)(a1) 的数量.

问题转化为求满足 1 ≤ l c m ( x , y ) ≤ n 1\le{lcm(x,y)}\le{n} 1lcm(x,y)n 的数量,我们先不考虑 x ≤ y x\le{y} xy ,算出这种情况下的答案再去掉重复的即可.

因此要求的式子变成了: ∑ i = 1 n ∑ j = 1 n [ l c m ( i , j ) ≤ n ] \sum_{i=1}^n\sum_{j=1}^n[lcm(i,j)\le{n}] i=1nj=1n[lcm(i,j)n]

​ = ∑ d = 1 n ∑ i = 1 n d ∑ j = 1 n d [ i ⋅ j ⋅ d ≤ n ] ⋅ [ gcd ⁡ ( i , j ) = = 1 ] \sum_{d=1}^n\sum_{i=1}^{\frac{n}{d}}\sum_{j=1}^{\frac{n}{d}}[i\cdot{j}\cdot{d}\le{n}]\cdot[\gcd(i,j)==1] d=1ni=1dnj=1dn[ijdn][gcd(i,j)==1]

​ = ∑ d = 1 n ∑ k = 1 n d μ ( k ) ⋅ ∑ i = 1 n k ⋅ d ∑ j = 1 n k ⋅ d [ i ⋅ j ⋅ d ⋅ k 2 ≤ n ] \sum_{d=1}^n\sum_{k=1}^{\frac{n}{d}}\mu(k)\cdot\sum_{i=1}^{\frac{n}{k\cdot{d}}}\sum_{j=1}^{\frac{n}{k\cdot{d}}}[i\cdot{j}\cdot{d}\cdot{k^2}\le{n}] d=1nk=1dnμ(k)i=1kdnj=1kdn[ijdk2n]

此时更换 k k k d d d 的位置,同时将 k 2 k^2 k2 移到右边可得:

​ = ∑ k = 1 n ∑ d = 1 n k 2 ∑ i = 1 n k 2 ⋅ d ∑ j = 1 n k 2 ⋅ d [ i ⋅ j ⋅ d ≤ n k 2 ] \sum_{k=1}^{\sqrt{n}}\sum_{d=1}^{\frac{n}{k^2}}\sum_{i=1}^{\frac{n}{k^2\cdot{d}}}\sum_{j=1}^{\frac{n}{k^2\cdot{d}}}[i\cdot{j}\cdot{d}\le\frac{n}{k^2}] k=1n d=1k2ni=1k2dnj=1k2dn[ijdk2n]

观察发现后面一部分求的是形如 [ a ⋅ b ⋅ c ≤ n ] [a\cdot{b}\cdot{c}\le{n}] [abcn] 的东西.

因此令 a ≤ b ≤ c a\le{b}\le{c} abc ,那么 a a a 只需要枚举到 n 1 3 n^{\frac{1}{3}} n31 ,然后 b b b 枚举到 ⌊ n a ⌋ \sqrt{\lfloor\frac{n}{a}\rfloor} an , c c c 的范围可以直接算出.

然后统计三个数相同,两个数相同,三个数都不同的情况即可解决该题.

long long cal(long long n)//计算1~n中LCM(i,j)<=n的对数.
{
    if(!n) return 0;
    long long res=0;
    for(int k=1;1LL*k*k<=n;k++){
        if(mu[k]==0) continue;
        long long x=n/(1LL*k*k);
        long long now=0;//计算a*b*c<=x的对数.
        for(int i=1;1LL*i*i*i<=x;i++){//枚举第一位.
            for(int j=i+1;1LL*j*j*i<=x;j++)//枚举第二位
                now+=1LL*(x/(1LL*i*j)-j)*6+3;//a>1;//程序计算的是无顺序的,答案要求有顺序,所以(ans+n)/2;
}

​ — 51 N o d 1222 51Nod1222 51Nod1222


约数个数函数相关题目


题意:

∑ i = 1 n ∑ j = 1 m d ( i ⋅ j ) \sum_{i=1}^n\sum_{j=1}^md(i\cdot{j}) i=1nj=1md(ij) n , m ≤ 1 0 5 n,m\le{10^5} n,m105

题解:

a n s = ∑ i = 1 n ∑ j = 1 m ∑ x ∣ i ∑ y ∣ j [ gcd ⁡ ( x , y ) = = 1 ] ans = \sum_{i=1}^n\sum_{j=1}^m\sum_{x|i}\sum_{y|j}[\gcd(x,y)==1] ans=i=1nj=1mxiyj[gcd(x,y)==1]

= ∑ x = 1 n ∑ y = 1 m n x ⋅ m y ⋅ [ gcd ⁡ ( x , y ) = = 1 ] = \sum_{x=1}^n\sum_{y=1}^m\frac{n}{x}\cdot\frac{m}{y}\cdot[\gcd(x,y)==1] =x=1ny=1mxnym[gcd(x,y)==1] (由上一步更换枚举项得到)

= ∑ x = 1 n ∑ y = 1 m n x ⋅ m y ⋅ ∑ k ∣ gcd ⁡ ( x , y ) μ ( k ) = \sum_{x=1}^n\sum_{y=1}^m\frac{n}{x}\cdot\frac{m}{y}\cdot\sum_{k|\gcd(x,y)}\mu(k) =x=1ny=1mxnymkgcd(x,y)μ(k)

= ∑ k = 1 m i n ( n , m ) μ ( k ) ⋅ ∑ x = 1 n k n k ⋅ x ⋅ ∑ y = 1 m k m k ⋅ y = \sum_{k=1}^{min(n,m)}\mu(k)\cdot\sum_{x=1}^{\frac{n}{k}}\frac{n}{k\cdot{x}}\cdot\sum_{y=1}^{\frac{m}{k}}\frac{m}{k\cdot{y}} =k=1min(n,m)μ(k)x=1knkxny=1kmkym

可以发现后一部分可以分块处理,因此问题得到解决.

​ — B Z O J 3994 BZOJ3994 BZOJ3994


题意:

∑ i = 1 n σ ( i ) \sum_{i=1}^n\sigma(i) i=1nσ(i) , n < 2 63 n\lt2^{63} n<263 , 1 0 5 10^5 105 组数据 , 15 s 15s 15s . (神仙题)
题目给的是 σ \sigma σ , 但求的是约数个数.

题解:

#include 
using namespace std;
const int maxn = 1e6+7;
typedef long long ll;
typedef __int128 lll;
int t;
ll n;
struct Node{
    ll x,y;
    Node(ll _x=0,ll _y=0):x(_x),y(_y){}
    inline Node operator + (const Node &B){return Node(x+B.x,y+B.y);}
}S[maxn];
int top=0;
inline bool inner(ll x,ll y){return n

​ — S P O J SPOJ SPOJ D I V C N T 1 DIVCNT1 DIVCNT1


题意:

∑ i = 1 n d ( i 2 ) \sum_{i=1}^nd(i^2) i=1nd(i2) n ≤ 1 0 12 n\le{10^{12}} n1012

题解:

容易发现我们对于 d ( i 2 ) d(i^2) d(i2) 没办法直接处理,所以对于 d ( n 2 ) d(n^2) d(n2) ,我们考虑那些 n 2 n^2 n2 有的因子,而 n n n 没有的因子,可以知道,对于 n = p 1 q 1 ⋅ p 2 q 2 ⋯ p t q t n=p_1^{q_1}\cdot{p_2}^{q_2}\cdots{p_t}^{q_t} n=p1q1p2q2ptqt ,每一个 n n n 有的约数对应质因子指数上分别加上 0 0 0 q i q_i qi ,可以构成一个 n 2 n^2 n2 的因子.

即: d ( n 2 ) = ∑ d ∣ n 2 w ( d ) d(n^2)=\sum_{d|n}2^{w(d)} d(n2)=dn2w(d) ( w ( d ) w(d) w(d)表示 d d d 的不同质因子个数 ) .

a n s = ∑ i = 1 n ∑ d ∣ n 2 w ( d ) ans = \sum_{i=1}^n\sum_{d|n}2^{w(d)} ans=i=1ndn2w(d)

考虑 2 w ( d ) 2^{w(d)} 2w(d) , 本质上是 d d d 的所有约数中无平方因数的个数,他们有一个特点, μ \mu μ 要么是 − 1 -1 1要么是 1 1 1 .

= ∑ i = 1 n ∑ d ∣ i ∑ k ∣ d μ 2 ( k ) = \sum_{i=1}^n\sum_{d|i}\sum_{k|d}\mu^2(k) =i=1ndikdμ2(k)

= ∑ i = 1 n ∑ k ∣ i μ 2 ( k ) ⋅ d ( i k ) = \sum_{i=1}^n\sum_{k|i}\mu^2(k)\cdot{d(\frac{i}{k})} =i=1nkiμ2(k)d(ki)

= ∑ k = 1 n μ 2 ( k ) ⋅ ∑ i = 1 n k d ( i ) = \sum_{k=1}^n\mu^2(k)\cdot\sum_{i=1}^{\frac{n}{k}}d(i) =k=1nμ2(k)i=1knd(i)

发现 ∑ k = 1 n μ 2 ( k ) \sum_{k=1}^n\mu^2(k) k=1nμ2(k) 等于 1 1 1 ~ n n n 中无平方因子数的个数,等于 ∑ i = 1 n μ ( i ) ⋅ n i 2 \sum_{i=1}^{\sqrt{n}}\mu(i)\cdot{\frac{n}{i^2}} i=1n μ(i)i2n

因此分块即可解决此题.

S P O J SPOJ SPOJ D I V C N T 2 DIVCNT2 DIVCNT2


约数和函数相关题目


题意:

∑ i = 1 n ∑ j = 1 n σ ( i ⋅ j ) \sum_{i=1}^n\sum_{j=1}^n\sigma(i\cdot{j}) i=1nj=1nσ(ij) n ≤ 1 0 9 n\le{10^9} n109

题解:

a n s = ∑ i = 1 n ∑ j = 1 n ∑ x ∣ i ∑ y ∣ j x ⋅ j y ⋅ [ gcd ⁡ ( x , y ) = = 1 ] ans = \sum_{i=1}^n\sum_{j=1}^n\sum_{x|i}\sum_{y|j}x\cdot{\frac{j}{y}}\cdot[\gcd(x,y)==1] ans=i=1nj=1nxiyjxyj[gcd(x,y)==1]

= ∑ x = 1 n ∑ y = 1 n x ⋅ ∑ j = 1 n y j ⋅ [ gcd ⁡ ( x , y ) = = 1 ] = \sum_{x=1}^n\sum_{y=1}^nx\cdot\sum_{j=1}^{\frac{n}{y}}j\cdot[\gcd(x,y)==1] =x=1ny=1nxj=1ynj[gcd(x,y)==1]

= ∑ x = 1 n ∑ y = 1 n x ⋅ ∑ j = 1 n y j ⋅ ∑ k ∣ gcd ⁡ ( x , y ) μ ( k ) =\sum_{x=1}^n\sum_{y=1}^nx\cdot\sum_{j=1}^{\frac{n}{y}}j\cdot\sum_{k|\gcd(x,y)}\mu(k) =x=1ny=1nxj=1ynjkgcd(x,y)μ(k)

= ∑ k = 1 n k ⋅ μ ( k ) ⋅ ∑ x = 1 n k x ⋅ ∑ y = 1 n k ∑ j = 1 n k ⋅ y j = \sum_{k=1}^nk\cdot\mu(k)\cdot\sum_{x=1}^{\frac{n}{k}}x\cdot\sum_{y=1}^{\frac{n}{k}}\sum_{j=1}^{\frac{n}{k\cdot{y}}}j =k=1nkμ(k)x=1knxy=1knj=1kynj

由于 ∑ i = 1 n i ⋅ ⌊ n i ⌋ = ∑ i = 1 n ∑ j = 1 n i j = ∑ i = 1 n σ ( i ) \sum_{i=1}^ni\cdot{\lfloor\frac{n}{i}\rfloor}=\sum_{i=1}^n\sum_{j=1}^{\frac{n}{i}}j=\sum_{i=1}^n\sigma(i) i=1niin=i=1nj=1inj=i=1nσ(i)

所以: = ∑ k = 1 n k ⋅ μ ( k ) ⋅ ( ∑ i = 1 n k σ ( i ) ) 2 = \sum_{k=1}^nk\cdot\mu(k)\cdot(\sum_{i=1}^{\frac{n}{k}}\sigma(i))^2 =k=1nkμ(k)(i=1knσ(i))2

因此分块加杜教筛即可解决该题. (后面的部分可以用杜教筛的思想预处理前 n 2 3 n^{\frac{2}{3}} n32 项,然后分块出后 n 1 3 n^{\frac{1}{3}} n31 项)

​ — 51 N o d 1220 51Nod1220 51Nod1220


题意:

∑ i = 1 n ∑ j = 1 n m a x ( i , j ) ⋅ σ ( i ⋅ j ) \sum_{i=1}^n\sum_{j=1}^nmax(i,j)\cdot\sigma(i\cdot{j}) i=1nj=1nmax(i,j)σ(ij) n ≤ 1 0 6 n\le{10^6} n106 , 5 ⋅ 1 0 4 5\cdot10^4 5104 组数据.

题解:

易得: a n s = 2 ⋅ ∑ i = 1 n ∑ j = 1 i i ⋅ σ ( i ⋅ j ) − ∑ i = 1 n i ⋅ σ ( i 2 ) ans = 2\cdot\sum_{i=1}^n\sum_{j=1}^ii\cdot\sigma(i\cdot{j})-\sum_{i=1}^ni\cdot\sigma(i^2) ans=2i=1nj=1iiσ(ij)i=1niσ(i2)

可以发现后面一部分是可以线性筛出来的.所以式子变成:

a n s ′ = ∑ i = 1 n ∑ j = 1 i i ⋅ σ ( i ⋅ j ) ans^{'} = \sum_{i=1}^n\sum_{j=1}^ii\cdot\sigma(i\cdot{j}) ans=i=1nj=1iiσ(ij)

= ∑ i = 1 n ∑ j = 1 i i ⋅ ∑ x ∣ i ∑ y ∣ j x ⋅ j y ⋅ [ gcd ⁡ ( x , y ) = = 1 ] = \sum_{i=1}^n\sum_{j=1}^ii\cdot\sum_{x|i}\sum_{y|j}x\cdot\frac{j}{y}\cdot[\gcd(x,y)==1] =i=1nj=1iixiyjxyj[gcd(x,y)==1]

= ∑ x = 1 n x 2 ⋅ ∑ i = 1 x i i ⋅ ∑ y = 1 i ⋅ x ∑ j = 1 i ⋅ x y j ⋅ [ gcd ⁡ ( x , y ) = = 1 ] = \sum_{x=1}^nx^2\cdot\sum_{i=1}^{\frac{x}{i}}i\cdot\sum_{y=1}^{i\cdot{x}}\sum_{j=1}^{\frac{i\cdot{x}}{y}}j\cdot[\gcd(x,y)==1] =x=1nx2i=1ixiy=1ixj=1yixj[gcd(x,y)==1]

= ∑ x = 1 n x 2 ⋅ ∑ i = 1 x i i ⋅ ∑ y = 1 i ⋅ x σ ( y ) ⋅ [ gcd ⁡ ( x , y ) = = 1 ] =\sum_{x=1}^nx^2\cdot\sum_{i=1}^{\frac{x}{i}}i\cdot\sum_{y=1}^{i\cdot{x}}\sigma(y)\cdot[\gcd(x,y)==1] =x=1nx2i=1ixiy=1ixσ(y)[gcd(x,y)==1]

= ∑ k = 1 n k 2 ⋅ μ ( k ) ⋅ ∑ x = 1 n k x 2 ⋅ ∑ i = 1 n k ⋅ x i ⋅ ∑ y = 1 i ⋅ x σ ( y ) =\sum_{k=1}^nk^2\cdot\mu(k)\cdot\sum_{x=1}^{\frac{n}{k}}x^2\cdot\sum_{i=1}^{\frac{n}{k\cdot{x}}}i\cdot\sum_{y=1}^{i\cdot{x}}\sigma(y) =k=1nk2μ(k)x=1knx2i=1kxniy=1ixσ(y)

= ∑ k = 1 n k 2 ⋅ μ ( k ) ∑ i = 1 n k i ⋅ σ ( i ) ⋅ ∑ j = 1 i σ ( j ) =\sum_{k=1}^nk^2\cdot\mu(k)\sum_{i=1}^{\frac{n}{k}}i\cdot\sigma(i)\cdot\sum_{j=1}^i\sigma(j) =k=1nk2μ(k)i=1kniσ(i)j=1iσ(j) (由上一步更换枚举项可得)

此时可以分块做,但由于数据组数过多,令 T = i ⋅ k T=i\cdot{k} T=ik 可以再次变换:

= ∑ T = 1 n ∑ k ∣ T T ⋅ k ⋅ μ ( k ) ⋅ σ ( T k ) ⋅ ∑ i = 1 T k σ ( i ) =\sum_{T=1}^n\sum_{k|T}T\cdot{k}\cdot\mu(k)\cdot\sigma(\frac{T}{k})\cdot\sum_{i=1}^{\frac{T}{k}}\sigma(i) =T=1nkTTkμ(k)σ(kT)i=1kTσ(i)

此时可以 n ⋅ ln ⁡ n n\cdot{\ln{n}} nlnn 预处理出解然后 O ( 1 ) O(1) O(1) 的输出答案.

​ — 51 N o d 1584 51Nod1584 51Nod1584


欧拉函数相关题目


题意:

给定 n , m n,m n,m ,求 ∑ i = 1 n ∑ j = 1 m φ ( i ⋅ j ) \sum_{i=1}^n\sum_{j=1}^m\varphi(i\cdot{j}) i=1nj=1mφ(ij) , n ≤ 1 0 5 n\le{10^5} n105 , m ≤ 1 0 9 m\le{10^9} m109 .


题解:

首先观察到 n n n 的范围较小,因此考虑令 S ( n , m ) = ∑ i = 1 m φ ( n ⋅ i ) S(n,m) = \sum_{i=1}^m\varphi(n\cdot{i}) S(n,m)=i=1mφ(ni) , 则 $ans $ = ∑ i = 1 n S ( i , m ) \sum_{i=1}^nS(i,m) i=1nS(i,m)

现在考虑化简 ∑ i = 1 m φ ( n ⋅ i ) \sum_{i=1}^m\varphi(n\cdot{i}) i=1mφ(ni) . 我们发现没有直接的公式可以化简 φ ( n ⋅ i ) \varphi(n\cdot{i}) φ(ni) , 因此考虑从 φ ( x ) \varphi(x) φ(x) 的定义考虑.

由于 φ ( x ) \varphi(x) φ(x) = = = p 1 q 1 ⋅ p 2 q 2 ⋅ p 3 q 3 ⋯ p t q t ⋅ ( 1 − 1 p 1 ) ⋅ ( 1 − 1 p 2 ) ⋅ ( 1 − 1 p 3 ) ⋯ ( 1 − 1 p t ) p_1^{q_{1}}\cdot{p_2^{q_{2}}}\cdot{p_3^{q_{3}}}\cdots{p_t^{q_{t}}}\cdot{(1-\frac{1}{p_1})}\cdot{(1-\frac{1}{p_2})}\cdot{(1-\frac{1}{p_3})}\cdots(1-\frac{1}{p_t}) p1q1p2q2p3q3ptqt(1p11)(1p21)(1p31)(1pt1)

= = = p 1 q 1 − 1 ⋅ p 2 q 2 − 1 ⋅ p 3 q 3 − 1 ⋯ p t q t − 1 ⋅ ( p 1 − 1 ) ⋅ ( p 2 − 1 ) ⋅ ( p 3 − 1 ) ⋯ ( p t − 1 ) p_1^{q_1-1}\cdot{p_2^{q_2-1}}\cdot{p_3^{q_3-1}}\cdots{p_t^{q_t-1}}\cdot(p_1-1)\cdot(p_2-1)\cdot(p_3-1)\cdots(p_t-1) p1q11p2q21p3q31ptqt1(p11)(p21)(p31)(pt1)

所以我们可以把 x x x 表示成 y ⋅ w y\cdot{w} yw ,分别对应上面的前一部分和后一部分.因此,原式可以化简成:

= = = y ⋅ ∑ i = 1 m φ ( w ⋅ i ) y\cdot\sum_{i=1}^m\varphi(w\cdot{i}) yi=1mφ(wi)

此时再考虑将 w w w i i i 的公因子都放到 w w w 上:

= = = y ⋅ ∑ i = 1 m φ ( w gcd ⁡ ( w , i ) ⋅ i ⋅ gcd ⁡ ( w , i ) ) y\cdot\sum_{i=1}^m\varphi(\frac{w}{\gcd(w,i)}\cdot{i}\cdot{\gcd(w,i)}) yi=1mφ(gcd(w,i)wigcd(w,i))

= = = y ⋅ ∑ i = 1 m φ ( w gcd ⁡ ( w , i ) ) ⋅ φ ( i ⋅ gcd ⁡ ( w , i ) ) y\cdot\sum_{i=1}^m\varphi(\frac{w}{\gcd(w,i)})\cdot\varphi(i\cdot\gcd(w,i)) yi=1mφ(gcd(w,i)w)φ(igcd(w,i))

由于 gcd ⁡ ( w , i ) \gcd(w,i) gcd(w,i) 有的因子 i i i 也有,所以可以考虑将 g c d ( w , i ) gcd(w,i) gcd(w,i) 提取出来

= = = y ⋅ ∑ i = 1 m φ ( w gcd ⁡ ( w , i ) ) ⋅ φ ( i ) ⋅ gcd ⁡ ( w , i ) y\cdot\sum_{i=1}^m\varphi(\frac{w}{\gcd(w,i)})\cdot\varphi(i)\cdot\gcd(w,i) yi=1mφ(gcd(w,i)w)φ(i)gcd(w,i)

此时考虑利用欧拉函数的性质 ∑ d ∣ n φ ( d ) = n \sum_{d|n}\varphi(d)=n dnφ(d)=n g c d ( w , i ) gcd(w,i) gcd(w,i) 转化成 φ ( ) \varphi() φ() 的形式

= = = y ⋅ ∑ i = 1 m φ ( w gcd ⁡ ( w , i ) ) ⋅ φ ( i ) ⋅ ∑ k ∣ gcd ⁡ ( w , i ) φ ( k ) y\cdot\sum_{i=1}^m\varphi(\frac{w}{\gcd(w,i)})\cdot\varphi(i)\cdot\sum_{k|\gcd(w,i)}\varphi(k) yi=1mφ(gcd(w,i)w)φ(i)kgcd(w,i)φ(k)

此时观察发现 φ ( w gcd ⁡ ( w , i ) ) \varphi(\frac{w}{\gcd(w,i)}) φ(gcd(w,i)w) φ ( k ) \varphi(k) φ(k) 之间有一定关系,所以可以再次转化:

= = = y ⋅ ∑ i = 1 m φ ( i ) ⋅ ∑ k ∣ gcd ⁡ ( w , i ) φ ( w k ) y\cdot\sum_{i=1}^m\varphi(i)\cdot\sum_{k|\gcd(w,i)}\varphi(\frac{w}{k}) yi=1mφ(i)kgcd(w,i)φ(kw)

= = = y ⋅ ∑ i = 1 m φ ( i ) ⋅ ∑ k ∣ w , k ∣ i φ ( w k ) y\cdot\sum_{i=1}^m\varphi(i)\cdot\sum_{k|w,k|i}\varphi(\frac{w}{k}) yi=1mφ(i)kw,kiφ(kw)

此时可以考虑更换枚举项的位置,把 k k k 的位置提前,同时:

= = = y ⋅ ∑ k ∣ w φ ( w k ) ⋅ ∑ i = 1 m k φ ( k ⋅ i ) y\cdot\sum_{k|w}\varphi(\frac{w}{k})\cdot\sum_{i=1}^{\frac{m}{k}}\varphi(k\cdot{i}) ykwφ(kw)i=1kmφ(ki)

= = = y ⋅ ∑ k ∣ w φ ( w k ) ⋅ S ( k , m k ) y\cdot\sum_{k|w}\varphi(\frac{w}{k})\cdot{S(k,\frac{m}{k})} ykwφ(kw)S(k,km)

此时发现后面一部分是一个递归的形式,且当 n = 1 n=1 n=1 S ( n , m ) = ∑ i = 1 m φ ( i ) S(n,m) = \sum_{i=1}^m\varphi(i) S(n,m)=i=1mφ(i) 可以杜教筛算出来.

因此记忆化后即可解决该题.

​ — B Z O J 3512 BZOJ3512 BZOJ3512


莫比乌斯函数相关题目


题意:

∑ i = 1 m μ ( n ⋅ i ) \sum_{i=1}^m\mu(n\cdot{i}) i=1mμ(ni) , n ≤ 1 0 12 n\le{10^{12}} n1012 , m ≤ 2 ⋅ 1 0 9 m\le{2\cdot10^9} m2109

题解:

S ( n , m ) = ∑ i = 1 m μ ( n ⋅ i ) S(n,m)=\sum_{i=1}^m\mu(n\cdot{i}) S(n,m)=i=1mμ(ni) .

则 ans = μ ( n ) ⋅ ∑ i = 1 m μ ( i ) ⋅ [ gcd ⁡ ( n , i ) = = 1 ] \mu(n)\cdot\sum_{i=1}^m\mu(i)\cdot[\gcd(n,i)==1] μ(n)i=1mμ(i)[gcd(n,i)==1]

​ = μ ( n ) ⋅ ∑ i = 1 m μ ( i ) ⋅ ∑ k ∣ gcd ⁡ ( n , i ) μ ( k ) \mu(n)\cdot\sum_{i=1}^m\mu(i)\cdot\sum_{k|\gcd(n,i)}\mu(k) μ(n)i=1mμ(i)kgcd(n,i)μ(k)

​ = μ ( n ) ⋅ ∑ k ∣ n μ ( k ) ⋅ ∑ i = 1 m k μ ( k ⋅ i ) \mu(n)\cdot\sum_{k|n}\mu(k)\cdot\sum_{i=1}^{\frac{m}{k}}\mu(k\cdot{i}) μ(n)knμ(k)i=1kmμ(ki)

​ = μ ( n ) ⋅ ∑ k ∣ n μ ( k ) ⋅ S ( k , m k ) \mu(n)\cdot\sum_{k|n}\mu(k)\cdot{S(k,\frac{m}{k})} μ(n)knμ(k)S(k,km)

发现化简得到一个递归的形式,当 n = = 1 n==1 n==1 时为出口,用杜教筛求出 ∑ i = 1 m μ ( i ) \sum_{i=1}^m\mu(i) i=1mμ(i) 即可.

小技巧: 只在最外面分解一次 n n n 的质因子,然后在求 S ( n , m ) S(n,m) S(n,m) 时,对于一个 n n n ,暴力判断哪些原始 n n n 的质因子可以整除现在的 $n $ ,然后二进制枚举 k k k 即可.

​ — 18 18 18 年徐州网络赛 e a s y m a t h easy math easymath


反演的巧妙应用


题意:

给定长度为n的数组a, 求 ∑ i = 1 n ∑ j = 1 n gcd ⁡ ( a [ i ] , a [ j ] ) ⋅ ( gcd ⁡ ( a [ i ] , a [ j ] ) − 1 ) \sum_{i=1}^n\sum_{j=1}^n\gcd(a[i],a[j])\cdot(\gcd(a[i],a[j])-1) i=1nj=1ngcd(a[i],a[j])(gcd(a[i],a[j])1) .

1 ≤ n ≤ 1 0 4 1\le{n}\le10^4 1n104, 1 ≤ a [ i ] ≤ 1 0 4 1\le{a[i]}\le10^4 1a[i]104.

题解:

考虑直接枚举 gcd ⁡ ( a [ i ] , a [ j ] ) = = d \gcd(a[i],a[j])==d gcd(a[i],a[j])==d的对数统计. 用max表示a[i]的最大值.

用f(d) 表示 gcd ⁡ ( a [ i ] , a [ j ] ) = = d \gcd(a[i],a[j])==d gcd(a[i],a[j])==d 的对数, 用F(d)表示 d ∣ gcd ⁡ ( a [ i ] , a [ j ] ) d|\gcd(a[i],a[j]) dgcd(a[i],a[j]) 的对数.

F(d)可以较为轻松的预处理出来.

容易得到式子: ans = ∑ d = 1 m a x ( d 2 − d ) ⋅ f ( d ) \sum_{d=1}^{max}(d^2-d)\cdot{f(d)} d=1max(d2d)f(d)

​ = ∑ d = 1 m a x ( d 2 − d ) ⋅ ∑ d ∣ x μ ( x d ) ⋅ F ( x ) \sum_{d=1}^{max}(d^2-d)\cdot\sum_{d|x}\mu(\frac{x}{d})\cdot{F(x)} d=1max(d2d)dxμ(dx)F(x)

由于此题式子化简后系数是 d 2 − d d^2-d d2d ,且范围在 1 0 4 10^4 104,所以可以直接预处理后面一部分来计算.

当计算的是 g c d ( a [ i ] , a [ j ] ) gcd(a[i],a[j]) gcd(a[i],a[j])时,还可以继续化简.

h d u 5212 hdu 5212 hdu5212


题意:

求: ∏ i = 1 n ∏ j = 1 m f i b [ gcd ⁡ ( i , j ) ] \prod_{i=1}^n\prod_{j=1}^mfib[\gcd(i,j)] i=1nj=1mfib[gcd(i,j)] n , m ≤ 1 0 6 n,m\le{10^6} n,m106

题解:

根据反演基本套路,可得: ans = ∏ d = 1 m i n ( n , m ) f i b [ d ] f ( d ) \prod_{d=1}^{min(n,m)}fib[d]^{f(d)} d=1min(n,m)fib[d]f(d)

​ ans = ∏ d = 1 m i n ( n , m ) f i b [ d ] ∑ d ∣ k μ ( k d ) ⋅ F ( k ) \prod_{d=1}^{min(n,m)}fib[d]^{\sum_{d|k}}\mu(\frac{k}{d})\cdot{F(k)} d=1min(n,m)fib[d]dkμ(d

你可能感兴趣的:(ACM)