---
id: 1261
lang: en
translation_of: index.md
source_sha256: a9ca31c84c9b440c8eb1f32ca425699dd89ad7dd4a28d04584fc97866e547780
title: "P3172: [CQOI2015] Choosing Numbers — Solution"
description: "At first I had no clue at all; after reading the solution it all suddenly clicked (duh, doesn't everything click once you've read the solution?)."
tags: ["OI","Möbius Inversion","Du's Sieve"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "Problem name 选数 rendered as “Choosing Numbers” (literally “choose numbers”)."
  - "\\text{ 个} under the braces (a measure word, “n of them”) rendered as \\text{ of them}."
---

## Approach

At first I had no clue at all; after reading the solution it all suddenly clicked (duh, doesn't everything click once you've read the solution?). The problem asks us to compute:

$$
ans = \underbrace{ \sum_{a_1 = L}^R \dots \sum_{a_n = L}^R }_{n \text{ of them}} [\gcd_{i = 1}^n (a_i) = k]
$$

<!--more-->

Consider first dividing out a $k$:

$$
ans = \underbrace{ \sum_{a_1 = \frac{L-1}{k}}^\frac{R}{k} \dots \sum_{a_n = \frac{L-1}{k}}^\frac{R}{k} }_{n \text{ of them}} [\gcd_{i = 1}^n (a_i) = 1]
$$

Consider just inverting that last thingy away directly:

$$
ans = \underbrace{ \sum_{a_1 = \frac{L-1}{k}}^\frac{R}{k} \dots \sum_{a_n = \frac{L-1}{k}}^\frac{R}{k} }_{n \text{ of them}} \sum_{d|\gcd_{i = 1}^n (a_i) = 1} \mu(d)
$$

Pull it to the front:

$$
\sum_{d = 1}^\frac{R}{k} \mu(d) \underbrace{ \sum_{a_1 = \frac{L-1}{k}}^\frac{R}{k} \dots \sum_{a_n = \frac{L-1}{k}}^\frac{R}{k} }_{n \text{ of them}} 1
$$

That <span class="slang" data-zh="一坨" data-gloss="yī tuó, “a lump, a blob”. Casual, slightly crude word for a big messy chunk of something; here, the formula.">whole lump</span> at the back can all be massaged into the form of a power:

$$
\sum_{d = 1}^\frac{R}{k} \mu(d) (\frac{R}{k} - \frac{L-1}{k})^n
$$

Done. Just use Du's sieve to sieve the prefix sums, plus divisor-block decomposition.

## Code

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

const int MAX_N = 1e6 + 2000, mod = 1000000007, INF = 0x3f3f3f3f;
ll mu[MAX_N], n, k, l, r, prime[MAX_N], tot, inv2;
bool vis[MAX_N];
unordered_map<ll, ll> ump;

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

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

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

int main()
{
    scanf("%lld%lld%lld%lld", &n, &k, &l, &r);
    l = (l - 1) / k, r /= k, inv2 = quick_pow(2, mod - 2);
    preprocess();
    ll ans = 0;
    for (ll i = 1, gx; i <= r; i = gx + 1)
    {
        gx = min(l / i ? l / (l / i) : INF, r / (r / i));
        ans = (ans + (sieve(gx) - sieve(i - 1) + mod) % mod * quick_pow(r / i - l / i, n) % mod) % mod;
    }
    printf("%lld", (ans + mod) % mod);
    return 0;
}
```
