Solving CF468C: Modulo Constraint with Digit Sum Define S(x) = Σ_{i=1}^{x} f(i), where f(i) is the sum of digits of i. For a given modulus a ≤ 1e18, we need to find l, r such that S(r) - S(l-1) ≡ 0 (mod a). Observe that shifting the interval by one changes the sum modulo a by one: S(10^18 + k) - S(k...
Problem 1: Freshman Challenge The answer is a constant value. #include <cstdio> int main() { printf("%d\n", 15); return 0; } Problem 2: Flooring We only need the product of the dimensions to be divisible by 6, and both dimensions must be large enough for a 2×3 or 3×2 tile. #include &...
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...
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...
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...