Mobius And Multiplicative Counting¶
This lane starts when a gcd-counting problem stops being:
try every pair, compute gcd, and count the good ones
and becomes:
the right inclusion-exclusion lives on divisors, not on explicit pairs
The key shift is:
- stop iterating over pairs
- count how many values are divisible by each
d - use Mobius cancellation to isolate the exact gcd condition you want
For contest work, this is the clean exact route for:
- counting unordered pairs with
gcd = 1 - turning divisor-sum information into exact gcd information
- arithmetic inclusion-exclusion where "all divisors up to MAX" matter
- multiplicative-counting tasks that want one linear sieve plus one multiples loop
At A Glance¶
- Use when: the statement is really about gcd/divisibility counts over many values, and the clean answer is a sum over divisors
- Core worldview: Mobius gives the cancellation identity that turns "gcd is divisible by d" counts into "gcd is exactly 1" counts
- Main tools: Mobius function
mu, linear sieve, divisor-frequency arrays, sums over multiples - Typical complexity:
O(MAX)formuwith a linear sieve, plusO(MAX log MAX)style multiples loops - Main trap: deriving the Mobius formula correctly, then still counting ordered pairs or forgetting the
C(cnt[d], 2)combinatorial factor
Strong contest signals:
- you need the number of pairs whose gcd is
1 - the values are bounded, often by
10^6or similar - the statement is about divisibility structure across the whole array, not one query value
- ordinary prime-set inclusion-exclusion would have to range over too many divisors
Strong anti-cues:
- the problem gives one small explicit set of primes, so ordinary inclusion-exclusion is enough
- you only need the largest divisor with frequency at least
2 - the main work is modular arithmetic or congruence solving, not divisor counting
- the value bound is too large for a multiples loop over all
d <= MAX
Prerequisites¶
Helpful nearby anchors:
- Common Divisors
- Prime Multiples
- Number theory cheatsheet
- Dirichlet Convolution / Prefix Sums Of Number-Theoretic Functions
Problem Model And Notation¶
The Mobius function is:
The cancellation identity to remember is:
This is the exact arithmetic version of inclusion-exclusion over prime divisors.
For array problems, a standard helper is:
That is:
cnt[d]counts values divisible bydC(cnt[d], 2)counts unordered pairs whose gcd is divisible byd
Then Mobius cancellation can isolate the exact gcd event you want.
From Brute Force To The Right Idea¶
Brute Force¶
For a task like "count coprime pairs in an array", the most direct route is:
- iterate over all unordered pairs
- compute
gcd(a[i], a[j]) - count the ones equal to
1
That is O(n^2 log A), which dies immediately once n reaches 10^5.
First Structural Observation¶
Pair problems about gcd are often easier if you count by divisor:
- how many values are divisible by
1 - how many by
2 - how many by
3 - ...
Once you know cnt[d], you also know how many unordered pairs have gcd divisible by d:
So the problem is no longer pairwise gcd checking.
It becomes one divisor-frequency precompute.
Second Structural Observation¶
The event
gcd(x, y) = 1
is equivalent to:
the gcd has no prime divisor
That is an inclusion-exclusion statement.
But the clean contest version is not:
- enumerate prime subsets for every pair
It is:
- sum over divisors with Mobius weights
This is exactly where Mobius function earns its keep.
Core Invariant And Why It Works¶
1. Mobius Is Divisor-Side Inclusion-Exclusion¶
For a positive integer g,
acts like an indicator:
- it is
1wheng = 1 - it is
0otherwise
So for any pair (x, y):
That one line is the core invariant of this whole lane.
2. Exchange The Order Of Counting¶
Summing over all unordered pairs:
Swap the summations:
But the inner count is exactly:
So:
That is the flagship formula for this lane.
3. Why The Multiples Loop Works¶
If freq[x] is the count of occurrences of value x, then:
So you can compute every cnt[d] by iterating over multiples:
for d in 1..MAX:
for multiple in d, 2d, 3d, ...:
cnt[d] += freq[multiple]
This is why the full lane remains contest-usable once MAX is only around 10^6.
4. Mobius Inversion View¶
More generally, if:
then Mobius inversion says:
For contest work, the important lesson is not the algebraic slogan.
It is the route split:
- if the statement gives "sum over divisors" data, inversion may recover the exact function
- if the statement gives "pairs divisible by d" data, Mobius may recover the exact gcd layer
Variant Chooser¶
Use Direct Divisor-Frequency Counting When¶
- you only need a monotone condition like "at least two numbers share divisor
d" - the answer is something like the largest feasible gcd
- no exact cancellation by Mobius is needed
That is the route in:
Use Ordinary Inclusion-Exclusion When¶
- the prime set is explicit and small
- the natural universe is subsets of primes, not all divisors up to
MAX
That is the route in:
Use This Lane When¶
- all divisors up to
MAXmatter - divisor frequencies are easy to aggregate
- exact gcd conditions like
gcd = 1or exact divisor-side cancellation are the real bottleneck
Do Not Overreach To Heavier Number-Theory Machinery¶
For the first ship of this lane, stay inside:
- one linear sieve
- one multiples loop
- one Mobius-weighted sum
Do not jump straight to:
- Dirichlet convolution as a separate topic
- Min_25 sieve
- large-
nprefix sums of multiplicative functions
unless the statement truly needs them.
Worked Examples¶
Example 1. Small Coprime-Pairs Array¶
Take:
[2, 3, 4, 9]
The unordered pairs are:
(2, 3)coprime(2, 4)not coprime(2, 9)coprime(3, 4)coprime(3, 9)not coprime(4, 9)coprime
So the true answer is 4.
Now compute by divisors:
cnt[1] = 4cnt[2] = 2cnt[3] = 2cnt[4] = 1cnt[9] = 1
Relevant Mobius values:
mu[1] = 1mu[2] = -1mu[3] = -1mu[4] = 0mu[6] = 1, butcnt[6] = 0
So:
The Mobius sum exactly cancels every pair whose gcd is greater than 1.
Example 2. CSES Counting Coprime Pairs¶
For:
5 4 20 1 16 17 5 15
the key observation is:
1is divisible by everyd = 1only, but it is coprime with every other value- values like
20,16, and15create many overlaps under divisors2,4,5 - the correct route is still the same:
- frequency array
cnt[d]via multiples- Mobius-weighted pair sum
That yields the sample answer:
19
The implementation lesson is that once the formula is correct, the problem becomes routine arithmetic precompute.
Example 3. Exact GCD Layers¶
Sometimes you want:
how many unordered pairs have gcd exactly g?
Then the same worldview still works.
Let:
and apply Mobius to the scaled divisor structure:
This is a good mental extension, even if the first flagship note only needs the g = 1 case.
Pseudocode¶
read values
MAX = max(values)
mu = mobius_linear_sieve(MAX)
freq[x] = occurrence count of x
for d in 1..MAX:
cnt[d] = 0
for multiple in d, 2d, 3d, ...:
cnt[d] += freq[multiple]
ans = 0
for d in 1..MAX:
pairs = cnt[d] * (cnt[d] - 1) / 2
ans += mu[d] * pairs
print ans
Implementation Notes¶
mu[d] = 0means "ignore this divisor completely" in the final sum.- For unordered pairs, always use:
not cnt * cnt.
cnt[d]is usually built from a frequency array, not by factoring each input value separately.- Use
long longfor the final answer and pair counts. - If
MAXis the maximum array value, thefor (multiple += d)loops are the real complexity driver.
References¶
- Reference: OI Wiki - Mobius Inversion Formula
- Reference: cp-algorithms - Linear Sieve
- Reference: cp-algorithms - Inclusion-Exclusion Principle
- Reference: Competitive Programmer's Handbook
- Practice: CSES - Counting Coprime Pairs