Essential Algorithm Templates in C++

Number Theory Primality Testing via Trial Division #include <iostream> using namespace std; bool isPrime(int num) { if (num < 2) return false; for (int d = 2; d <= num / d; ++d) if (num % d == 0) return false; return true; } int main() { int queries; cin >> queries; while (queries--) { ...

Posted on Sat, 13 Jun 2026 17:12:21 +0000 by sebjlan

Optimization and Strategy Problems on Sequences and Grids

Resource Redistribution with Asymmetric Operations Given a sequence (a) of length (n) and two positive integers (x, y) where (y \le x). You may repeatedly pick two elements and transfer resources: subtract (x) from one and add (y) to the other, provided the first remains non‑negative. Determine the maximum possible value of any element after op ...

Posted on Tue, 09 Jun 2026 17:33:25 +0000 by obesechicken13

2012 NOIP Popularization Group Finals Walkthrough

Task 1 – Factor Splitting Given a positive integer n that is the product of two distinct primes, output the larger one. Input: One integer n (≤ 2·109). Output: The larger prime factor. Observation: The first divisor (other than 1) must be the smaller prime, so the answer is n / i once the smallest i > 1 with n % i == 0 is found. #include &lt ...

Posted on Thu, 28 May 2026 22:28:35 +0000 by dannyd

Base Number Conversion Algorithms and Implementation

Problem B: Arbitrary Base Conversion This code converts a number from one arbitrary base to another. Implementation #include <stdio.h> #include <string.h> // Convert from base `src_base` string `src_num` to decimal integer. int convert_to_decimal(int src_base, const char *src_num) { int result = 0; int place_value = 1; ...

Posted on Mon, 18 May 2026 01:51:55 +0000 by thebluebus

Core Algorithmic Building Blocks for Competitive Programming

Mathematical Algorithms Fast Exponentiation Reduces the time complexity of computing powers to logarithmic time by leveraging binary decomposition of the exponent. long long binary_pow(long long base, long long exp, long long mod) { long long res = 1; base %= mod; while (exp > 0) { if (exp & 1) res = (res * base) % mo ...

Posted on Sun, 17 May 2026 15:51:07 +0000 by TheMagician

Advanced Number Theory Algorithms and Applications

Number Theory 1.1 Chinese Remainder Theorem Problem Statement Given a system of congruences: \[ \begin{cases} x \equiv a_1 \pmod{b_1} \\ x \equiv a_2 \pmod{b_2} \\ \vdots \\ x \equiv a_n \pmod{b_n} \end{cases} \] Find the smallest positive integer solution for \(x\), where \(b_i\) (for \(i \in [1,n]\)) are pairwise coprime. Theoretical Analy ...

Posted on Sun, 17 May 2026 04:39:00 +0000 by xymbo

Algorithmic Problem Solving: Maximum Independent Set, Bomb Chain Reactions, SG Functions, and Tree Queries

A Given a sequence ${a_n}$, select the largest possible subset such that no two selected elements sum to a prime number. Constraints: $T \leq 4$, $n \leq 750$, $a_i \leq 10^9$. Identical values can be merged into counts. Note that at most one instance of 1 may be included. This becomes a bipartite graph problem: odd numbers connect to the sourc ...

Posted on Thu, 14 May 2026 02:12:22 +0000 by supergrame

Lucas Theorem and Its Extended Application

Lucas Theorem Mathematical Statement Given prime $p$, the following congruence holds: $$\binom{n}{m} \equiv \binom{\lfloor n/p \rfloor}{\lfloor m/p \rfloor} \cdot \binom{n \bmod p}{m \bmod p} \pmod{p}$$ Proof Foundation Lemma 1 For prime $p$: $$\binom{p}{k} \equiv 0 \pmod{p} \text{ when } 0 < k < p$$ The numerator contains factor $p$, mak ...

Posted on Tue, 12 May 2026 18:23:17 +0000 by Moneypenny

Advanced Algorithmic Problem Solving Techniques

Problem A: Digit Frequency Analysis Given a functon (f(x)) that counts the frequency of the most common digit in number (x), compute (\sum_{i=l}^{r}f(i)) for large ranges (up to (10^{18})). Approach: Represent digit counts as a state vector (S = {c_0, \dots, c_9}) Transform into frequency-of-counts representation (S' = {a_0, \dots, a_{18}}) Us ...

Posted on Sat, 09 May 2026 21:20:21 +0000 by vestax1984

Algorithmic Paradigms and Optimizations in Competitive Programming

Probabilistic Path Enumeration in Directed Acyclic Graphs Given a directed acyclic graph (DAG) consisting of $V$ vertices and $E$ edges, each edge $e_k$ possesses an independent activation probability $p_k$. The objective is to compute the expected number of functional paths originating from vertex $0$ and terminating at vertex $V-1$. A path is ...

Posted on Thu, 07 May 2026 22:27:40 +0000 by PDP11