Determining Representability of an Integer as a Sum of 2020 and 2021 Multiples

Given a positive integer $ n $, determine whether it can be expressed as a non-negative integer combination of 2020 and 2021 — that is, whether there exist integers $ a \geq 0 $ and $ b \geq 0 $ such that:

$$ n = 2020a + 2021b $$

Rewriting the equation reveals a useful structure:

$$ n = 2020(a + b) + b = 2020k + b, $$

where $ k = a + b $. Since $ a = k - b \geq 0 $, it follows that $ 0 \leq b \leq k $. Substituitng $ b = n - 2020k $, the feasibility condition becomes:

$$ 0 \leq n - 2020k \leq k. $$

Rearanging the right inequality gives $ n \leq 2021k $, so $ k \geq \lceil n / 2021 \rceil $. The left inequality yields $ k \leq \lfloor n / 2020 \rfloor $. Thus, only values of $ k $ in the integer interval $ [\lfloor n/2021 \rfloor, \lfloor n/2020 \rfloor] $ need to be checked.

For each candidate $ k $, compute $ b = n - 2020k $ and verify $ 0 \leq b \leq k $. If any $ k $ satisfies this, the answer is "YES"; otherwise, "NO".

#include <iostream>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t--) {
        long long n;
        cin >> n;
        bool feasible = false;

        const int base_low = 2020;
        const int base_high = 2021;

        // k represents total count: k = a + b
        long long min_k = n / base_high;
        if (base_high * min_k < n) min_k++;
        long long max_k = n / base_low;

        for (long long k = min_k; k <= max_k; ++k) {
            long long remainder = n - base_low * k;
            if (remainder >= 0 && remainder <= k) {
                feasible = true;
                break;
            }
        }

        cout << (feasible ? "YES" : "NO") << '\n';
    }

    return 0;
}

Tags: Mathematics number-theory greedy competitive-programming

Posted on Wed, 14 Oct 2026 16:39:25 +0000 by Spiz