tinycodinglm / math /math-0178.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 16)
d517dc9 verified
|
Raw History Blame
1.01 kB
opencode trace
System You are an expert mathematician and programmer.

You are an expert mathematician and programmer.

User

Explain modular arithmetic for competitive programming

Assistant

Modular arithmetic: a = b (mod m) means m divides (a-b).

Operations: (a+b) mod m = ((a mod m)+(b mod m)) mod m. Same for sub and mult.
Division: a/b mod m = a * b^{-1} mod m.

Modular inverse: b^{-1} mod m exists iff gcd(b,m)=1. Extended Euclidean: find x,y with bx+my=1, then x=b^{-1} mod m. O(log m).

Fermat: if p prime, a^{p-1} = 1 (mod p), so a^{-1} = a^{p-2} (mod p). Fast exponentiation O(log p).

CRT: x = a_i (mod m_i) pairwise coprime -> unique solution mod M=product(m_i).
x = sum(a_i * M_i * M_i^{-1}) mod M, M_i = M/m_i.

Applications: counting mod 10^9+7, hashing, overflow avoidance.