Olympiad in Informatics

P3327: [SDOI2015] Sum of Divisor Counts — Solution

Main Idea and Derivation

This problem is still fairly interesting (ugh, I had to read the solution for ages before I could write it, I’m so weak). The main thing is turning \(d\) into:

\[ d(xy) = \sum_{i|x}^x \sum_{j|y}^y [\gcd(i,j) = 1] \]

You can get a hand-wavy intuition for it: every time a divisor is enumerated, it is counted only when the two are coprime, which avoids counting anything twice.

So we can try simplifying it:

\[ \begin{gathered} \sum^N_{i=1} \sum^M_{j=1} d(i,j) = \sum^N_{i=1} \sum^M_{j=1} \sum_{x|i}^i \sum_{y|j}^j [\gcd(x,y) = 1] \\ \sum^N_{i=1} \sum^M_{j=1} \lfloor \frac{N}{i} \rfloor \lfloor \frac{M}{j} \rfloor [\gcd(x,y) = 1] \end{gathered} \]

Now we can apply Möbius inversion:

\[\begin{gathered}f(h) = \sum^N_{i=1} \sum^M_{j=1} \lfloor \frac{N}{i} \rfloor \lfloor \frac{M}{j} \rfloor [\gcd(x,y) = h] \\ F(h) = \sum_{h|d} f(h) \\ F(h) = \sum_{h|d} \sum^N_{i=1} \sum^M_{j=1} \lfloor \frac{N}{i} \rfloor \lfloor \frac{M}{j} \rfloor [\gcd(x,y) = d] \\ F(h) = \sum^N_{i=1} \sum^M_{j=1} \lfloor \frac{N}{i} \rfloor \lfloor \frac{M}{j} \rfloor [h|\gcd(x,y)] \\ F(h) = \sum^{\frac{N}{h}}_{i=1} \sum^{\frac{M}{h}}_{j=1} \lfloor \frac{N}{hi} \rfloor \lfloor \frac{M}{hj} \rfloor \\ f(h)=\sum_{h|d} \mu(\frac{d}{h})F(h) \\ f(h)=\sum_{h|d} \mu(\frac{d}{h}) \sum^{\frac{N}{h}}_{i=1} \sum^{\frac{M}{h}}_{j=1} \lfloor \frac{N}{hi} \rfloor \lfloor \frac{M}{hj} \rfloor \\ f(h)=\sum_{h|d} \mu(\frac{d}{h}) (\sum^{\frac{N}{h}}_{i=1} \lfloor \frac{N}{hi} \rfloor) (\sum^{\frac{M}{h}}_{j=1} \lfloor \frac{M}{hj} \rfloor) \end{gathered}\]

For the fraction part at the end, we just define a function and precompute it:

\[ \sum^{\frac{N}{h}}_{i=1} \lfloor \frac{N}{hi} \rfloor = \sum^{\frac{N}{h}}_{i=1} \lfloor \frac{N}{h} * \frac{1}{i} \rfloor \]

So we take \( \frac{N}{h} \) as \(x\) and precompute \(s(x) = \sum_{i=1}^x \frac{x}{i}\); then with divisor-block decomposition the complexity drops really low.

Code

// P3327.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX_N = 50100;
int n, prime[MAX_N], tot, mu[MAX_N], muprefix[MAX_N];
ll g[MAX_N];
bool vis[MAX_N];
int main()
{
    scanf("%d", &n);
    mu[1] = 1;
    for (int i = 2; i < MAX_N; i++)
    {
        if (!vis[i])
            prime[++tot] = i, mu[i] = -1;
        for (int j = 1; j <= tot && i * prime[j] < MAX_N; j++)
        {
            vis[i * prime[j]] = true;
            if (i % prime[j] == 0)
                break;
            else
                mu[i * prime[j]] = -mu[i];
        }
    }
    for (int i = 1; i < MAX_N; i++)
        muprefix[i] = muprefix[i - 1] + mu[i];
    for (int i = 1; i < MAX_N; i++)
    {
        ll ans = 0;
        for (int l = 1, r = 0; l <= i; l = r + 1)
        {
            r = (i / (i / l));
            ans += 1LL * (r - l + 1) * 1LL * (i / l);
        }
        g[i] = ans;
    }
    while (n--)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        ll ans = 0;
        for (int l = 1, r; l <= min(a, b); l = r + 1)
        {
            r = min(a / (a / l), b / (b / l));
            ans += (muprefix[r] - muprefix[l - 1]) * 1LL * g[a / l] * 1LL * g[b / l];
        }
        printf("%lld\n", ans);
    }
    return 0;
}