Algorithm Solutions for Codeforces Educational Round 164

A. Ribbon Coloring Strategy Alice's optimal strategy is to color the ribbon in a repeating pattern like "123123...". Bob's optimal counter-strategy is to recolor the ribbon to the most frequent color. The most frequent color appears at least ⌈n/m⌉ times, leaving Bob with at most (n - ⌈n/m⌉) recoloring operations. Compare this value wi ...

Posted on Fri, 19 Jun 2026 16:00:53 +0000 by []InTeR[]

Latin America Regional Programming Contest Solutions: Advanced Algorithm Techniques

Problem D: DiviDuelo Approach This problem requires number theory analysis and prime factorization techniques. The solution involves categorizing cases based on the prime factorization of the input number. The implementation uses advanced primality testing and factorization algorithms including Miller-Rabin and Pollard's rho method. Implementat ...

Posted on Thu, 18 Jun 2026 16:00:43 +0000 by sjaccaud

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