#include <bits/stdc++.h> using namespace std; int sumSquare(int n, int start, const unordered_map<int, int>& squares) { for (int i = start; i * i <= n; ++i) { if (squares.find(n - i * i) != squares.end()) { return i; } } return 0; } int addToSum(int asq, int bsq, int hsq) { if (asq == bsq) { if (asq == hsq) { return 1; } return 2; } if (asq == hsq || bsq == hsq) { return 2; } return 3; } int sumSquare3(int n, int start, const unordered_map<int, int>& squares) { for (int i = start; i * i <= n; ++i) { int b = sumSquare(n - i * i, 1, squares); if (b > 0) { return addToSum(b * b, n - i * i - b * b, n - i * i); } } return 0; } int findBase(unordered_map<int, int> squares, int csq, int hsq) { int a = sumSquare(csq, 1, squares); if (a > 0) { int asq = a * a; int bsq = csq - asq; if (asq == bsq) { if (asq == hsq) { return 1; } return 2; } if (asq == hsq || bsq == hsq) { return 2; } return 3; } return 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; unordered_map<int, int> squares; int sum = 0; for (int i = 1; i * i <= n * n; ++i) squares[i * i] = 1; for (int d = 1; d <= n; d ++) { sum += sumSquare3(d * d, 1, squares); } cout << sum << endl; }
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | #include <bits/stdc++.h> using namespace std; int sumSquare(int n, int start, const unordered_map<int, int>& squares) { for (int i = start; i * i <= n; ++i) { if (squares.find(n - i * i) != squares.end()) { return i; } } return 0; } int addToSum(int asq, int bsq, int hsq) { if (asq == bsq) { if (asq == hsq) { return 1; } return 2; } if (asq == hsq || bsq == hsq) { return 2; } return 3; } int sumSquare3(int n, int start, const unordered_map<int, int>& squares) { for (int i = start; i * i <= n; ++i) { int b = sumSquare(n - i * i, 1, squares); if (b > 0) { return addToSum(b * b, n - i * i - b * b, n - i * i); } } return 0; } int findBase(unordered_map<int, int> squares, int csq, int hsq) { int a = sumSquare(csq, 1, squares); if (a > 0) { int asq = a * a; int bsq = csq - asq; if (asq == bsq) { if (asq == hsq) { return 1; } return 2; } if (asq == hsq || bsq == hsq) { return 2; } return 3; } return 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; unordered_map<int, int> squares; int sum = 0; for (int i = 1; i * i <= n * n; ++i) squares[i * i] = 1; for (int d = 1; d <= n; d ++) { sum += sumSquare3(d * d, 1, squares); } cout << sum << endl; } |