Skip to content
We areTOP 1%
DSA · Competitive Programmer · Pattern 1 of 9

Number Theory

14 problems.
Your progress0%
1st Visit
0/14
2nd Visit
0/14
Revise
0/14

About this pattern

Number theory problems revolve around divisibility, primes, gcd and lcm, and modular arithmetic. The core tools are the Euclidean algorithm, a sieve of Eratosthenes or a smallest-prime-factor sieve for fast factorisation, Euler's totient function and fast modular exponentiation. Look for statements about divisors, coprimality, multiples inside a range, or huge values that matter only through their prime factors. A frequent trick is to iterate over a divisor or a multiple instead of over pairs of elements, which turns quadratic work into a harmonic sum of order O(M log M). Inclusion-exclusion and binary search on the answer combine well with counting multiples.

long long power(long long b, long long e, long long mod) {
    long long r = 1; b %= mod;
    while (e > 0) {
        if (e & 1) r = r * b % mod;
        b = b * b % mod; e >>= 1;
    }
    return r;
}
// spf[x] = smallest prime factor of x, for 2 <= x <= N
vector<int> buildSpf(int N) {
    vector<int> spf(N + 1, 0);
    for (int i = 2; i <= N; i++)
        if (spf[i] == 0)
            for (int j = i; j <= N; j += i)
                if (spf[j] == 0) spf[j] = i;
    return spf;
}

Problems

Number Theory questions
#ProblemPlatformDifficultyOpen1st1st Visit2nd2nd VisitRevRevise
1Orac and LCMCodeforcesMedium 1600Solve Orac and LCM on Codeforces (opens in a new tab)
2Ugly Number IIILeetCodeMedium Solve Ugly Number III on LeetCode (opens in a new tab)
3Same GCDsCodeforcesHard 1800Solve Same GCDs on Codeforces (opens in a new tab)
4Check If It Is a Good ArrayLeetCodeHard Solve Check If It Is a Good Array on LeetCode (opens in a new tab)
5K-goodCodeforcesHard 1900Solve K-good on Codeforces (opens in a new tab)
6Strange BeautyCodeforcesHard 1900Solve Strange Beauty on Codeforces (opens in a new tab)
7Number of Different Subsequences GCDsLeetCodeHard Solve Number of Different Subsequences GCDs on LeetCode (opens in a new tab)
8Count Array Pairs Divisible by KLeetCodeHard Solve Count Array Pairs Divisible by K on LeetCode (opens in a new tab)
9The Football SeasonCodeforcesHard 2000Solve The Football Season on Codeforces (opens in a new tab)
10CGCDSSQCodeforcesHard 2000Solve CGCDSSQ on Codeforces (opens in a new tab)
11Two DivisorsCodeforcesHard 2000Solve Two Divisors on Codeforces (opens in a new tab)
12Maximum ValueCodeforcesHard 2100Solve Maximum Value on Codeforces (opens in a new tab)
13The Number of PairsCodeforcesHard 2100Solve The Number of Pairs on Codeforces (opens in a new tab)
14Count PairsCodeforcesHard 2300Solve Count Pairs on Codeforces (opens in a new tab)

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account