Counting GCD Values in Range Using Integer Division Block
Given integers l, r, and k, determine how many distinct greatest common divisors (GCDs) can be formed by selecting any k numbers from the range [l, r].
The constraint is: 1 ≤ l ≤ r ≤ 10^12, 2 ≤ k ≤ r - l + 1.
Rather than computing all possible GCD values directly, we count all potential divisors. Any integer m = i × j that divides two numbers x ...
Posted on Thu, 27 Aug 2026 16:53:21 +0000 by AMCH
Solving Math and Graph Problems from a Competitive Programming Contest
Problem 1: Counting Valid Pairs with LCM Condition
Problem Description
Given an integer n where 1 ≤ n ≤ 10^8, determine the number of pairs (x, y) such that 1 ≤ x, y ≤ n and the following inequality holds:
lcm(x, y) / gcd(x, y) ≤ 3
Note that lcm(x, y) = (x * y) / gcd(x, y)^2.
Solution Approach
This is a straightforward number theory problem. L ...
Posted on Mon, 10 Aug 2026 16:39:42 +0000 by HaVoC
Advanced Algorithmic Strategies: Subtree DP Reconstruction and Prime-Power Modular Counting
Subtree Budget Allocation and Optimal Path Reconstruction
The core challenge revolves around allocating a fixed budget across a binary tree structure to maximize a specific threshold value. The solution begins by analyzing the root's contribution and propagating constraints downward. We define a dynamic programming state dp_max[u][b] representi ...
Posted on Fri, 07 Aug 2026 16:35:09 +0000 by arun4444
Solving the Ternary Goldbach Conjecture via Sieve of Eratosthenes
The Ternary Goldbach Conjecture asserts that any odd integer greater than 7 can be represented as the sum of three prime numbers. While proven for sufficiently large numbers, verifying this for smaller integers requires an effiicent computational approach. Given an odd integer n (9 < n < 20,000), our objective is to find a triplet of prim ...
Posted on Thu, 06 Aug 2026 16:46:07 +0000 by santrowithu
Computing All Integer Factors in Ascending Order with Rust
A straightforward Rust implementation for finding all factors of a number is shown below. This approach iterates up to the square root of the input value.
fn compute_factors_simple(num: u64) -> Vec<u64> {
let sqrt_val = (num as f64).sqrt().floor() as u64;
let mut factors = Vec::new();
for divisor in 1..=sqrt_val {
i ...
Posted on Tue, 28 Jul 2026 16:25:21 +0000 by pazzy
Algorithmic Solutions for Nowcoder Weekly Contest Round 6
Problem A: Counting Digit Holes
The task requires calculating the total number of closed loops (holes) in a sequence of digits. Digits '0', '6', and '9' contain one loop each, while '8' contains two loops. The solution involves iterating through the string and accumulating the count based on the digit encountered.
#include <iostream>
#inc ...
Posted on Mon, 06 Jul 2026 17:24:28 +0000 by briand
Essential Mathematical Algorithms for Programming
Fast Exponentiation
Recursive Approach
function fastExponentiation(base, exponent, modulus) {
if (exponent === 0) return 1;
const halfExp = fastExponentiation(base, Math.floor(exponent / 2), modulus);
let result = (halfExp * halfExp) % modulus;
if (exponent % 2 === 1) {
result = (result * base) % modulus;
...
Posted on Wed, 01 Jul 2026 17:48:25 +0000 by varghesedxb
The Universal Euclidean Algorithm: Computing RU Strings and Summation Problems
Introduction
Consider the following geometric problem: mark all vertical lines x = c and horizontal lines y = c where c ∈ ℤ on a plane. Now consider a line y = (px + r)/q where p, r ∈ ℕ and q ∈ ℕ₊. Since the residue class of r modulo q determines the behavior, we can assume r < q without loss of generality.
Imagine a moving point traveling a ...
Posted on Sun, 28 Jun 2026 18:06:32 +0000 by gli
Technical Analysis of Xiangtan University Spring 2023 Freshman Programming Contest
Problem A: Strategic Allocation
This challenge involves selecting a subset of items to meet a weight capacity requirement with the minimum count. The optimal approach utilizes a greedy strategy. By prioritizing larger weights first, we minimize the number of items required to reach the target threshold.
void processAllocation() {
int itemCo ...
Posted on Sat, 27 Jun 2026 16:02:21 +0000 by mattpointblank
Counting Valid 2x2 Submatrices and Optimizing Zero-Prefix Products
To solve this problem, we scan every possible 2x2 submatrix in a n × m grid and check whether the four characters within it collectively contain at least one 'y', one 'o', and one 'u'. The order does not matter—only the presence of all three required characters.
The algorithm iterates over all valid top-left corners of 2x2 blocks (from row 1 to ...
Posted on Wed, 24 Jun 2026 17:58:58 +0000 by webdes03