【NOI2010】能量采集 题解

【NOI2010】能量采集 题解

谨纪念我的第一道手推出来的莫反题。

题目大意:已知 \(n\)\(m\),求 \(\sum\limits_{i=1}^n\sum\limits_{j=1}^m(2\cdot \gcd(i,j)-1)\)

首先变形一手:

\[\sum\limits_{i=1}^n\sum\limits_{j=1}^m(2\cdot\gcd(i,j)-1)=2\sum\limits_{i=1}^n\sum\limits_{j=1}^m\gcd(i,j)-n\times m \]

然后我们只用求出中间那两个 \(\sum\) 就好了。

\[\begin{aligned} \sum\limits_{i=1}^n\sum\limits_{j=1}^m\gcd(i,j)&=\sum\limits_{i=1}^n\sum\limits_{j=1}^m\sum\limits_{d=1}^nd[\gcd(i,j)=d]\\ &=\sum\limits_{d=1}^nd\sum\limits_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum\limits_{j=1}^{\lfloor\frac{m}{d}\rfloor}[\gcd(i,j)=1]\\ &=\sum\limits_{d=1}^nd\sum\limits_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum\limits_{j=1}^{\lfloor\frac{m}{d}\rfloor}\sum\limits_{x|\gcd(i,j)}\mu(x)\\ &=\sum\limits_{d=1}^nd\sum\limits_{x=1}^{\lfloor\frac{n}{d}\rfloor} \mu(x)\lfloor\frac{n}{dx}\rfloor\lfloor\frac{m}{dx}\rfloor \end{aligned} \]

\(T=dx\)

\[\begin{aligned} \sum\limits_{d=1}^nd\sum\limits_{x=1}^{\lfloor\frac{n}{d}\rfloor} \mu(x)\lfloor\frac{n}{dx}\rfloor\lfloor\frac{m}{dx}\rfloor &=\sum\limits_{d=1}^nd\sum\limits_{T=1}^n\mu(\frac{T}{d})\lfloor\frac{n}{T}\rfloor\lfloor\frac{m}{T}\rfloor[d|T]\\ &=\sum\limits_{T=1}^n\lfloor\frac{n}{T}\rfloor\lfloor\frac{m}{T}\rfloor\sum\limits_{d=1}^nd\cdot\mu(\frac{T}{d})[d|T]\\ &=\sum\limits_{T=1}^n\lfloor\frac{n}{T}\rfloor\lfloor\frac{m}{T}\rfloor\sum\limits_{d|T}d\cdot\mu(\frac{T}{d}) \end{aligned} \]

如何处理后面那个 \(\sum\),考虑狄利克雷卷积。不会的可以看我博客。

因为 \(\varphi*I=Id_1\),又因 \(I*\mu=\epsilon\),所以

\[\varphi=Id_1*\mu \]

注意到右边那个 \(\sum\) 其实就是 \(Id_1*\mu\),即 \(\varphi\)

所以可化为:

\[\sum\limits_{T=1}^n\lfloor\frac{n}{T}\rfloor\lfloor\frac{m}{T}\rfloor\sum\limits_{d|T}d\cdot\mu(\frac{T}{d})=\sum\limits_{T=1}^n\lfloor\frac{n}{T}\rfloor\lfloor\frac{m}{T}\rfloor\cdot\varphi(T) \]

很明显的整除分块,预处理 \(\varphi\) 的前缀和就好了。

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

#define int long long

const int N=1e5+5;

int n,m;
int cnt,pri[N],phi[N],mu[N],sum[N];
bool flg[N];

void init()
{
    mu[1]=1,phi[1]=1;
    for(int i=2;i<=N-5;i++)
    {
        if(!flg[i])
            pri[++cnt]=i,phi[i]=i-1,mu[i]=-1;
        for(int j=1;j<=cnt&&pri[j]*i<=N-5;j++)
        {
            flg[i*pri[j]]=1;
            if(i%pri[j]==0)
            {
                phi[i*pri[j]]=phi[i]*pri[j];
                break;
            }
            mu[i*pri[j]]=-mu[i];
            phi[i*pri[j]]=phi[i]*phi[pri[j]];
        }
    }
    for(int i=1;i<=N-5;i++)
        sum[i]=sum[i-1]+phi[i];
}

int work(int n,int m)
{
    if(n>m) swap(n,m);
    int res=0;
    for(int l=1,r;l<=n;l=r+1)
    {
        r=min(n/(n/l),m/(m/l));
        res+=(sum[r]-sum[l-1])*(n/l)*(m/l);
    }
    return res;
}

signed main()
{
    init();
    scanf("%lld%lld",&n,&m);
    printf("%lld\n",2*work(n,m)-n*m);
    return 0;
}
posted @ 2024-05-25 16:40  WerChange  阅读(17)  评论(0编辑  收藏  举报