C++ · Solution

2026

This C++ solution uses square-pair counting for 2026. Read the reasoning, inspect the code, or try your own test case below.

MathSquare-pair countingC++31 lines
Solution001of 248
Download C++ file ↓ Search the library → Open full Code Lab ↗ Report an issue ↗

Approach

Square-pair counting

Enumerate positive squares i² and j² with i < j and i² + j² ≤ n. Count how many pairs produce each sum, then count the sums produced by exactly one pair.

CountingTwo loops
Time
O(n)
Space
O(n)

Problem and code

Useful links.

Written by benbenyaojifen. Try the problem first, then compare your approach with the code.

View exact source file ↗
Implementation

C_2026.cpp

C++

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    int main() {
        ios::sync_with_stdio(0); cin.tie(0); 
        int n; cin >> n;
        vector<int> cnt(n + 1);
        for (int i = 1; i <= floor(sqrt(n)); i++) {
            ll x2 = 1LL * i * i;
            int remain = n - x2;
            if (remain < 0) continue;
            int my = floor(sqrt(remain));
            if (my < i) continue;
            for (int j = i + 1; j <= my; j++) {
                ll y2 = 1LL * j * j;
                cnt[y2 + x2]++;
            }
        }
        vector<int> ans;
        for (int i = 1; i <= n; i++) {
            if (cnt[i] == 1) {
                ans.push_back(i);
            }
        }
        cout << ans.size() << '\n';
        for (int i = 0; i < ans.size(); i++) {
            cout << ans[i] << " \n"[i == ans.size() - 1];
        }
        return 0;
    }
     
        

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗Keep studying →

Test this problem

Run your code here.

Paste your code, run a test case, compare the output, or trace selected values.

Full trace, comparison & stress testing ↗
StatusReady
Output
No run yet.
Diagnostics
No diagnostics yet.

Each run is isolated and has strict limits. Passing one test does not guarantee the judge will accept the solution.