---
id: 1931
lang: en
translation_of: index.md
source_sha256: 9bb0243573144a75761f5f69f39e16263567f67510be982319bad321953f0a94
title: "Yali Mock Contest #1 - Problem-Solving Report"
description: "Problem A combines distinct-value and range XOR sums kept offline in a Fenwick tree; problem C is Möbius inversion plus a sieve."
tags: ["OI","Math","Bitwise Operations","Möbius Inversion","Offline Algorithms"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "The ' - ' in the title and both problem headings is kept as the hyphen stored in the Chinese source."
  - "大力反演 chipped with the existing 大力 gloss from post 1541, rendered “muscling through it with inversion”."
---

## A - Magical Bitwise Operations

I thought about it for ages, and when I finally looked, it turned out to be maintained offline. I straight up <span class="slang" data-zh="吐血" data-gloss="tùxuè, “to spit blood”. Internet slang for being so frustrated or exasperated one could cough up blood.">coughed up blood</span>.

First, you can probably figure out that XORing the XOR sum of the distinct values with the range XOR sum gives the answer. Because this filters out the ones that occur an odd number of times and gives the final answer. Consider how to get the XOR sum of the distinct values in a range: one way is to maintain it with a persistent segment tree, the other is to maintain it offline. We can maintain the prefix XOR of distinct values in a Fenwick tree: we stuff each number into the nearest spot possible, and at that point we need a map to decide whether it has to be moved.

<!--more-->

```cpp
// xor.cpp
#include <bits/stdc++.h>
#define ull unsigned int

using namespace std;

const int MAX_N = 3e5 + 200;

int n, m, prefix[MAX_N][32];
ull ai[MAX_N], pre[MAX_N], nodes[MAX_N], anses[MAX_N];
unordered_map<ull, int> pos;
vector<pair<int, int> /**/> vec[MAX_N];

int lowbit(int x) { return x & (-x); }

void update(int x, ull val)
{
    for (; x <= n; x += lowbit(x))
        nodes[x] ^= val;
}

int query(int x, ull ret = 0)
{
    for (; x; x -= lowbit(x))
        ret ^= nodes[x];
    return ret;
}

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%u", &ai[i]), pre[i] = pre[i - 1] ^ ai[i];
    scanf("%d", &m);
    for (int i = 1, l, r; i <= m; i++)
        scanf("%d%d", &l, &r), vec[r].push_back(make_pair(l, i)), anses[i] = pre[r] ^ pre[l - 1];
    ull tot = 0;
    for (int i = 1; i <= n; i++)
    {
        if (pos[ai[i]])
            update(pos[ai[i]], ai[i]);
        else
            tot ^= ai[i];
        update(pos[ai[i]] = i, ai[i]);
        for (int idx = 0, siz = vec[i].size(); idx < siz; idx++)
            anses[vec[i][idx].second] ^= tot ^ query(vec[i][idx].first - 1);
    }
    for (int i = 1; i <= m; i++)
        printf("%u\n", anses[i]);
    return 0;
}
```

## C - A Wondrous Counting Problem

On seeing the problem, start by <span class="slang" data-zh="大力" data-gloss="dàlì, “great force”. OI slang for bashing a problem head-on with a heavy, straightforward method instead of anything clever.">muscling through it with</span> inversion:

$$
\begin{aligned} \text{ans} &= \sum_{i = 1}^n \sum_{j = 1}^n (a_i, a_j) \cdot (i, j) \\ &= \sum_{d = 1}^n d \sum_{i = 1}^n \sum_{j = 1}^n (a_i, a_j) [(i, j) = d] \\ &= \sum_{d = 1}^n d \sum_{i = 1}^{\lfloor \frac{n}{d} \rfloor} \sum_{j = 1}^{\lfloor \frac{n}{d} \rfloor} (a_{id}, a_{jd}) [(i, j) = 1] \\ &= \sum_{d = 1}^n d \sum_{i = 1}^{\lfloor \frac{n}{d} \rfloor} \sum_{j = 1}^{\lfloor \frac{n}{d} \rfloor} (a_{id}, a_{jd}) \sum_{x|i, x|j} \mu(x) \\ &= \sum_{d = 1}^n d \sum_{x = 1}^{\lfloor \frac{n}{d} \rfloor} \mu(x) \sum_{i = 1}^{\lfloor \frac{n}{d} \rfloor} \sum_{j = 1}^{\lfloor \frac{n}{d} \rfloor} (a_{id}, a_{jd}) [x|i] [x|j] \\ &= \sum_{d = 1}^n d \sum_{x = 1}^{\lfloor \frac{n}{d} \rfloor} \mu(x) \sum_{i = 1}^{\lfloor \frac{n}{dx} \rfloor} \sum_{j = 1}^{\lfloor \frac{n}{dx} \rfloor} (a_{idx}, a_{jdx}) \end{aligned}
$$

Define the last part, and then let’s sieve it:

$$
\begin{aligned} f(dx) &= \sum_{i = 1}^{\lfloor \frac{n}{dx} \rfloor} \sum_{j = 1}^{\lfloor \frac{n}{dx} \rfloor} (a_{idx}, a_{jdx}) \\ f(x) &= \sum_{i = 1}^{\lfloor \frac{n}{x} \rfloor} \sum_{j = 1}^{\lfloor \frac{n}{x} \rfloor} (a_{ix}, a_{jx}) \\ &= \sum_{d = 1}^{\lfloor \frac{n}{x} \rfloor} d \sum_{i = 1}^{\lfloor \frac{n}{x} \rfloor} \sum_{j = 1}^{\lfloor \frac{n}{x} \rfloor} [(a_{ix}, a_{jx}) = d] \end{aligned}
$$

How do we get rid of that last bit? We can’t directly use convolution with the identity element, because the condition is different. But with a little transformation:

$$
\sum_{d|T, T|x} \mu(\frac{T}{d}) = [x = d]
$$

It’s equivalent to what you get by multiplying the unit $1$ by $d$. With this in hand, we just substitute it in:

$$
\begin{aligned} f(x) &= \sum_{d = 1}^{\lfloor \frac{n}{x} \rfloor} d \sum_{i = 1}^{\lfloor \frac{n}{x} \rfloor} \sum_{j = 1}^{\lfloor \frac{n}{x} \rfloor} [(a_{ix}, a_{jx}) = d] \\ &= \sum_{d = 1}^{\lfloor \frac{n}{x} \rfloor} d \sum_{i = 1}^{\lfloor \frac{n}{x} \rfloor} \sum_{j = 1}^{\lfloor \frac{n}{x} \rfloor} \sum_{d|T, T|(a_{ix}, a_{jx})} \mu(\frac{T}{d}) \\ &= \sum_{d = 1}^{\lfloor \frac{n}{x} \rfloor} \sum_{i = 1}^{\lfloor \frac{n}{x} \rfloor} \sum_{j = 1}^{\lfloor \frac{n}{x} \rfloor} \sum_{d|T, T|(a_{ix}, a_{jx})} d \mu(\frac{T}{d}) \end{aligned}
$$

Looks like we’re about to get a $\varphi$ convolution! Swap the order:

$$
\begin{aligned} f(x) &= \sum_{d = 1}^{\lfloor \frac{n}{x} \rfloor} \sum_{d|T} d \mu(\frac{T}{d}) \sum_{i = 1}^{\lfloor \frac{n}{x} \rfloor} \sum_{j = 1}^{\lfloor \frac{n}{x} \rfloor} [T|(a_{ix}, a_{jx})] \\ &= \sum_{d = 1}^{\lfloor \frac{n}{x} \rfloor} \sum_{d|T} d \mu(\frac{T}{d}) \sum_{i = 1}^{\lfloor \frac{n}{x} \rfloor} \sum_{j = 1}^{\lfloor \frac{n}{x} \rfloor} [T|a_{ix}] [T|a_{jx}] \\ &= \sum_{T = 1}^{n} \varphi(T) (\sum_{i = 1}^{\lfloor \frac{n}{x} \rfloor} [T|a_{ix}])^2 \end{aligned}
$$

That’s about ready to sieve, except it seems only the sieve of Eratosthenes works, because every factor has to be accounted for. Plugging it back into the answer should be simple.

```cpp
// gcd.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e5 + 2000, mod = 1e9 + 7;

int phi[MAX_N], mu[MAX_N], primes[MAX_N], tot, f[MAX_N], n, ai[MAX_N], bucket[MAX_N];
bool vis[MAX_N];
vector<int> fac[MAX_N], tmp;

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &ai[i]);
    mu[1] = 1, phi[1] = 1;
    for (int i = 2; i <= n; i++)
    {
        if (!vis[i])
            primes[++tot] = i, mu[i] = -1, phi[i] = i - 1;
        for (int j = 1; j <= tot && 1LL * i * primes[j] <= n; j++)
        {
            vis[i * primes[j]] = true;
            if (i % primes[j] == 0)
            {
                phi[i * primes[j]] = phi[i] * primes[j];
                mu[i * primes[j]] = 0;
                break;
            }
            phi[i * primes[j]] = phi[i] * phi[primes[j]];
            mu[i * primes[j]] = -mu[i];
        }
    }
    // \Theta(n \log n);
    for (int i = 1; i <= n; i++)
        for (int j = i; j <= n; j += i)
            fac[j].push_back(i);
    // process the f(x), by enumerating size d;
    for (int d = 1; d <= n; d++)
    {
        for (int i = d; i <= n; i += d)
            for (vector<int>::iterator it = fac[ai[i]].begin(); it != fac[ai[i]].end(); it++)
                bucket[*it]++, tmp.push_back(*it);
        for (vector<int>::iterator it = tmp.begin(); it != tmp.end(); it++)
            if (bucket[*it])
                f[d] = (1LL * f[d] + 1LL * bucket[*it] * bucket[*it] % mod * phi[*it] % mod) % mod, bucket[*it] = 0;
        tmp.clear();
    }
    int ans = 0;
    for (int d = 1; d <= n; d++)
        for (int x = 1; x <= (n / d); x++)
            ans = (1LL * ans + 1LL * d * mu[x] % mod * f[d * x] % mod + mod) % mod;
    printf("%d\n", ans);
    return 0;
}
```
