0% found this document useful (0 votes)
71 views19 pages

Competitive Programming: Totient Möbius

The document discusses number theory concepts including Euler's totient function φ(n), which counts the positive integers less than n that are relatively prime to n. It also discusses the Möbius function μ(n), which provides information about the prime factorization of n. The document presents formulas and properties relating these functions, as well as code examples for computing them. It explains how the Möbius function can be used in inclusion-exclusion problems to count sets with certain factorization properties. Finally, it lists some problems relating to these number theory topics.

Uploaded by

Angkur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views19 pages

Competitive Programming: Totient Möbius

The document discusses number theory concepts including Euler's totient function φ(n), which counts the positive integers less than n that are relatively prime to n. It also discusses the Möbius function μ(n), which provides information about the prime factorization of n. The document presents formulas and properties relating these functions, as well as code examples for computing them. It explains how the Möbius function can be used in inclusion-exclusion problems to count sets with certain factorization properties. Finally, it lists some problems relating to these number theory topics.

Uploaded by

Angkur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

P T H I N K F A ST S

Competitive Programming
From Problem 2 Solution in O(1)

Number Theory
Totient and Möbius Functions

Mostafa Saad Ibrahim


PhD Student @ Simon Fraser University
Euler's totient function

◼ φ(n), the Phi Function


◼ Count integers i < n such that gcd(i, n) = 1
◼ gcd(a, b) = 1 => then coprimes: gcd(5, 7), gcd(4, 9)
◼ gcd(prime, i) = 1 for i < prime
◼ φ(10) = 4 => 1, 3, 7, 9
◼ φ(5) = 4 => 1, 2, 3, 4 …. φ(prime) = prime-1
◼ If a, b, c are pairwise coprimes, then
◼ φ(a*b*c) = φ(a) * φ(b) * φ(c)
◼ if k >= 1
Euler's totient numbers

◼ Online Sequence
◼ φ(n) = 1, 1, 2, 2, 4, 2, 6, 4, 6, 4, 10, 4, 12, 6, 8,
8, 16, 6, 18, 8, 12, 10, 22, 8, 20, 12, 18, 12, 28,
8, 30, 16, 20, 16, 24, 12, 36, 18, 24, 16, 40, 12
◼ φ(1) = φ(2) = 1. φ(5) = 4
◼ φ(n) is even for n > 2
◼ sqrt(n) <= φ(n) <= n - sqrt(n): Except 2, 6
◼ φ(nk) = nk-1 * φ(n)
◼ n = ∑iφ(di) where d are the divisors of n
Menon's identity

d(n): # of n divisors

The identity is not important in competitive :)


Phi Code: Brute Force
Phi Code: Prime Factorization
Phi Code: Range Generator
Phi Factorial Code

◼ Can you prove?


Square-free integer

◼ Is not divisible by perfect square (except 1)


◼ perfect square: sqrt(n) = is integer. sqrt(16) = 4
◼ SQ: e.g. not divisible by 16=4x4...or 49=7x7...etc
◼ In other words, no prime number occurs more
than once: e.g. n = 2*5*11 is square free, but n
= 2*3*3*3*7 is not (divisible by 9 = 3x3)
◼ I-th square free: 1, 2, 3, 5, 6, 7, 10, 11, 13, 14,
15, 17, 19, 21, 22, 23, 26, 29, 30, 31, 33, 34
◼ F(13) = 19
Möbius function

◼ μ(1) = 1
◼ μ(n) = 1 if n is a square-free positive integer
with an even number of prime factors.
◼ E.g. μ(2*3*5*7) = 1
◼ μ(n) = −1 if n is a square-free positive integer
with an odd number of prime factors.
◼ E.g. μ(2*3*5) = -1
◼ μ(n) = 0 if n has a squared prime factor.
◼ E.g. μ(2*3*3*7) = 0
Möbius sequence

◼ μ(n) = 1, -1, -1, 0, -1, 1, -1, 0, 0, 1, -1, 0, -1, 1,


1, 0, -1, 0, -1, 0, 1, 1, -1, 0, 0, 1, 0, 0, -1, -1, -1,
0, 1, 1, 1, 0, -1, 1, 1, 0, -1, -1, -1, 0, 0, 1, -1, 0
◼ μ(n) +1 = 2, 0, 0, 1, 0, 2, 0, 1, 1, 2, 0, 1, 0, 2
Möbius function code
Möbius generator code
Möbius and Inclusion Exclusion

◼ Recall, in IE we compute all subsets, and add


odd subsets and remove negative subsets
◼ Assume generating all subsets of primes but in
implicit way (e.g. iterate on numbers), Möbius
can tell you if number is odd subset or even
◼ Typically, ignoring numbers with repeated
prime factors is target
◼ Then Möbius(n) plays perfect role in that
Möbius and Inclusion Exclusion

◼ Given square free number, find its index?


◼ E.g. F_reverse(n = 19) = 13
◼ Reverse thinking: Can we remove non SFree?
◼ In range n, remove who divides by 2x2, 3x3,
4x4, 5x5, 6x6...etc
◼ 4x4 and 6x6 already computed by previous ones
◼ Ignore duplicate primes (4x4)...use IE for others F(2)+F(3)
+F(5)-F(6)
Möbius and Inclusion Exclusion

◼ Count the triples (a,b,c) such a, b, c <= n, and


gcd(a, b, c) = 1
◼ Reverse thinking, total - (# triples gcd > 1)
◼ How many triples with gcd multiple of 2: (n/2)3
◼ How many triples with gcd multiple of 3: (n/3)3
◼ and 4? Ignore any numbers of internal duplicate primes
◼ and 6? already computed in 2, 3. Remove it: -(n/6)3
◼ This is Inclusion Exclusion
Totient and Möbius connection

◼ Sum over divisors d of n

◼ Read Totient functions proves


‫ﺗﻢ ﺑﺤﻤﺪ ﷲ‬

‫ﻋﻠﻤﻜﻢ ﷲ ﻣﺎ ﯾﻨﻔﻌﻜﻢ‬

‫وﻧﻔﻌﻜﻢ ﺑﻤﺎ ﺗﻌﻠﻤﺘﻢ‬

‫وزادﻛﻢ ﻋﻠﻤﺎ ً‬
Problems

◼ UVA 417, 11417, 10179, 10820, 10990,


11426, 10299, 11327
◼ TIMUS: 1673
◼ http://www.geeksforgeeks.org/eulers-totient-
function/

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy