Olympiad in Informatics

P3768: A Simple Math Problem — Solution

Approach

The problem simply asks for:

\[ ( \sum_{i = 1}^n \sum_{j = 1}^n ij gcd(i, j) ) \mod p \]

The concise statement is very pleasant. With \(gcd\) showing up, we can think in the direction of inversion. First, the usual standard trick: enumerate the \(gcd\). For convenience of writing, I'll drop the modulo here.

\[ \sum_{d = 1}^n d \sum_{i = 1}^n \sum_{j = 1}^n ij [gcd(i, j) = d] \]

Then get rid of this \(d\) thing in the back; cancelling the two \(d\)'s at the same time produces \(d^2\), which we just multiply outside:

\[ \sum_{d = 1}^n d^3 \sum_{i = 1}^{\lfloor \frac{n}{d} \rfloor} \sum_{j = 1}^{\lfloor \frac{n}{d} \rfloor} ij [gcd(i, j) = 1] \]

We find that the latter part can be inverted directly. Consider substituting using the classic property:

\[ \sum_{d = 1}^n d^3 \sum_{i = 1}^{\lfloor \frac{n}{d} \rfloor} i \sum_{j = 1}^{\lfloor \frac{n}{d} \rfloor} j \sum_{h = 1}^n \mu(h) [h|i][h|j] \]

Rearrange the terms:

\[ \sum_{d = 1}^n d^3 \sum_{h = 1}^n \mu(h) \sum_{i = 1}^{\lfloor \frac{n}{d} \rfloor} i[h|i] \sum_{j = 1}^{\lfloor \frac{n}{d} \rfloor} j[h|j] \]

Divide out the two \(h\)'s:

\[ \sum_{d = 1}^n d^3 \sum_{h = 1}^n \mu(h) h^2 \sum_{i = 1}^{\lfloor \frac{n}{hd} \rfloor} i \sum_{j = 1}^{\lfloor \frac{n}{hd} \rfloor} j \]

The last two seem to be computable in \(O(1)\) time with high-school arithmetic series sums. Let \(calc(n) = \sum_{i = 1}^n i = \frac{n(n + 1)}{2}\):

\[ \sum_{d = 1}^n d^3 \sum_{h = 1}^n \mu(h) h^2 calc(\lfloor \frac{n}{hd} \rfloor)^2 \]

Let \(T = hd\) and simplify:

\[ \begin{gathered} \sum_{d = 1}^n d^3 \sum_{T = 1}^n \mu(\lfloor \frac{T}{d} \rfloor) (\lfloor \frac{T}{d} \rfloor)^2 calc(\lfloor \frac{n}{T} \rfloor)^2 \\ \sum_{T = 1}^n T^2 calc(\lfloor \frac{n}{T} \rfloor)^2 \sum_{d|T} d \mu(\lfloor \frac{T}{d} \rfloor) \end{gathered} \]

When swapping the positions of \(T\) and \(d\), mind the condition \(T = hd\), so \(d|T\) has to be satisfied. We notice this latter expression:

\[ \sum_{d|T} d \mu(\lfloor \frac{T}{d} \rfloor) \]

This expression has the form of a Dirichlet convolution, written for short as:

\[ id * \mu \]

Readers familiar with Dirichlet convolution can see at a glance that the convolution of these two functions is \(\varphi\). This result takes one line to prove. Readers who don't know where \( \epsilon = \mu * 1 \) comes from can read here. Here is the proof:

\[ \varphi * 1 = id = id * \epsilon = id * \mu * 1 = id * \mu = \varphi \]

So substitute it directly:

\[ \sum_{T = 1}^n calc(\lfloor \frac{n}{T} \rfloor)^2 \varphi(T) T^2 \]

That's already very good. Just knock out the prefix sums of \(\varphi(T) T^2\) with Du's sieve, then do divisor-block decomposition, and you're done.

Code

// P3768.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;

const ll MAX_N = 5e6 + 2000;
ll n, mod, phi[MAX_N], prime[MAX_N], tot, inv2, inv6;
bool vis[MAX_N];
unordered_map<ll, ll> ump;

ll getPreQuad(ll num)
{
    num %= mod;
    return num * (num + 1) % mod * inv2 % mod;
}

ll getPreThird(ll num)
{
    num %= mod;
    return num * (num + 1) % mod * (num + num + 1) % mod * inv6 % mod;
}

void phi_sieve()
{
    phi[1] = 1;
    for (ll i = 2; i < MAX_N; i++)
    {
        if (!vis[i])
            prime[++tot] = i, phi[i] = i - 1;
        for (ll j = 1; j <= tot && 1ll * prime[j] * i < MAX_N; j++)
        {
            vis[i * prime[j]] = true;
            if (i % prime[j] == 0)
            {
                phi[i * prime[j]] = phi[i] * prime[j] % mod;
                break;
            }
            phi[i * prime[j]] = phi[i] * phi[prime[j]] % mod;
        }
    }
    for (ll i = 2; i < MAX_N; i++)
        phi[i] = (phi[i] * i % mod * i % mod + phi[i - 1]) % mod;
}

ll quick_pow(ll bas, ll tim)
{
    ll ans = 1;
    while (tim)
    {
        if (tim & 1)
            ans = ans * bas % mod;
        bas = bas * bas % mod;
        tim >>= 1;
    }
    return ans;
}

ll sieve(ll num)
{
    if (num < MAX_N)
        return phi[num];
    if (ump[num])
        return ump[num];
    ll ans = getPreQuad(num);
    ans = ans * ans % mod;
    for (ll i = 2, gx; i <= num; i = gx + 1)
    {
        gx = num / (num / i);
        ll fact = (getPreThird(gx) - getPreThird(i - 1) + mod) % mod;
        ans = (ans - fact * sieve(num / i) % mod + mod) % mod;
    }
    return ump[num] = (ans + mod) % mod;
}

signed main()
{
    scanf("%lld%lld", &mod, &n);
    inv2 = quick_pow(2, mod - 2), inv6 = quick_pow(6, mod - 2);
    phi_sieve();
    ll ans = 0;
    for (ll i = 1, gx; i <= n; i = gx + 1)
    {
        gx = n / (n / i);
        ll fact = getPreQuad(n / i);
        fact = fact * fact % mod;
        ll bfact = (sieve(gx) - sieve(i - 1) + mod) % mod;
        ans = (ans + bfact * fact % mod) % mod;
    }
    printf("%lld", (ans + mod) % mod);
    return 0;
}