Fading Coder

One Final Commit for the Last Sprint

Nordic Collegiate Programming Contest 2021 Solutions

A - Antenna Analysis The mathematical expression can be decomposed into two separate components: #include <iostream> #include <queue> #include <vector> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, c; cin >> n >> c; priority_que...

Algorithmic Solutions for Competitive Programming Problems

Algorithmic Solutions for Competitive Programming Problems Extracting Initial Characters from Input Strings When solving problems that require extracting the first letters of words from input strings, a direct character-by-character approach can be effective: #include <iostream> #include <s...

Essential Number Theory and Linear Algebra Algorithms

Number Theory Fundamentals Extended Euclidean Algorithm and Linear Diophantine Equations For integers a and b, the equation ax + by = d has integer solutions if and only if the greatest common divisor gcd(a, b) divides d. This is known as Bézout's Identity. The extended Euclidean algorithm alows us...

Competitive Programming Core Algorithm Reference with Implementations

Number Theory Min-Max Inclusion-Exclusion The core identities for min-max inclusion-exclusion over finite sets are: $$\max(S) = \sum_{T \subseteq S} (-1)^{|T|+1} \min(T)$$ $$\min(S) = \sum_{T \subseteq S} (-1)^{|T|+1} \max(T)$$ Expanding the summation shows all non-extremal values cancel out. The id...

Constructing an n for Linear Congruence gcd(an+b, c)=1

A solution exists for the equation (\gcd(an+b, c) = 1) if and only if (\gcd(a, b, c) = 1). If (\gcd(a, b, c) > 1), the problem has no solution and (-1) is output. Given the condition (\gcd(a, b, c) = 1), we can construct a valid (n). Let (x = \gcd(b, c)). If (x = 1), then (n = 0) satisfies (\gcd(...

Binary Exponentiation and Modular Arithmetic

Efficient computation of $a^b \bmod m$ utilizes the binary representation of the exponent $b$. By expressing $b$ as $\sum_{i=0}^{k} c_i \cdot 2^i$ where $c_i \in {0,1}$, the power decomposes into a product of squared terms: $a^b = \prod_{i=0}^{k} (a^{2^i})^{c_i}$. The algorithm iterates through each...

Elementary Number Theory: Congruences and Modular Arithmetic

Modular Arithmetic Basics The expression a mod b denotes the remainder when a is divided by b. Addition: (a + b) % p Subtraction: (a - b + p) % p (adding p avoids negative results) Multiplication: (a * b) % p Division: Not directly supported; requires modular inverses (discussed later) Exponentiatio...