1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <bits/stdc++.h>
using namespace std;

#define all(a) begin(a), end(a)
using ll = long long;

void solve() {
    int n;
    cin >> n;

    vector<int> val(n * n + 1);

    vector<int> count(n * n + 1);
    for (int i = 1; i <= n; i++)
        for (int j = i; j <= n; j++) {
            int t = i * i + j * j;
            if (t <= n * n)
                count[t]++;
        }

    int last = n;
    int result = 0;
    for (int pr = n * n; pr >= 1; --pr) {
        // cerr << pr << "\n";
        while (last * last >= pr) {
            for (int h = 1; h <= last; h++)
                val[last * last - h * h]++;
            last--;
        }

        result += val[pr] * count[pr];
    }
    cout << result << "\n";
}

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

    int tests = 1;
    // cin >> tests;

    while (tests--)
        solve();
}