#include <iostream>
#include <vector>
#include <cmath>
#include <tuple>
using namespace std;
const int MAX_SIZE = 75000000;
bool* isSquareTable;
int* squareRoots;
void initializeSquares(int maxValue) {
isSquareTable = new bool[maxValue + 1]();
squareRoots = new int[maxValue + 1]();
int maxRoot = static_cast<int>(sqrt(maxValue)) + 1;
for (int i = 0; i <= maxRoot; ++i) {
long long square = static_cast<long long>(i) * i;
if (square <= maxValue) {
isSquareTable[square] = true;
squareRoots[square] = i;
}
}
}
bool isSquare(int y) {
if (y < 0 || y > MAX_SIZE) return false;
return isSquareTable[y];
}
// funkcja znajdująca i wypisująca wszystkie rozwiązania dla danego x
int findSolutions(int x) {
int x2 = x * x;
int result = 0;
for (int a = 1; a <= x; ++a) {
int a2 = a * a;
if (a2 > x2 / 3) break;
for (int b = a; b <= x; ++b) {
int b2 = b * b;
if (a2 + b2 > x2) break;
int c2 = x2 - a2 - b2;
if (isSquare(c2)) {
int c = squareRoots[c2];
if (b <= c) {
if (a == b && b == c) {
result++;
}
else if (a != b && b != c && a != c) {
result += 3;
}
else {
result += 2;
}
}
}
}
}
return result;
}
int main() {
int x;
cin >> x;
initializeSquares(3 * x * x);
int total = 0;
for (int i = 1; i <= x; i++){
total += findSolutions(i);
//std::cout<< "i:" << i << " findSolutions:" << findSolutions(i) << std::endl;
}
cout << total;
return 0;
}
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 77 78 79 80 | #include <iostream> #include <vector> #include <cmath> #include <tuple> using namespace std; const int MAX_SIZE = 75000000; bool* isSquareTable; int* squareRoots; void initializeSquares(int maxValue) { isSquareTable = new bool[maxValue + 1](); squareRoots = new int[maxValue + 1](); int maxRoot = static_cast<int>(sqrt(maxValue)) + 1; for (int i = 0; i <= maxRoot; ++i) { long long square = static_cast<long long>(i) * i; if (square <= maxValue) { isSquareTable[square] = true; squareRoots[square] = i; } } } bool isSquare(int y) { if (y < 0 || y > MAX_SIZE) return false; return isSquareTable[y]; } // funkcja znajdująca i wypisująca wszystkie rozwiązania dla danego x int findSolutions(int x) { int x2 = x * x; int result = 0; for (int a = 1; a <= x; ++a) { int a2 = a * a; if (a2 > x2 / 3) break; for (int b = a; b <= x; ++b) { int b2 = b * b; if (a2 + b2 > x2) break; int c2 = x2 - a2 - b2; if (isSquare(c2)) { int c = squareRoots[c2]; if (b <= c) { if (a == b && b == c) { result++; } else if (a != b && b != c && a != c) { result += 3; } else { result += 2; } } } } } return result; } int main() { int x; cin >> x; initializeSquares(3 * x * x); int total = 0; for (int i = 1; i <= x; i++){ total += findSolutions(i); //std::cout<< "i:" << i << " findSolutions:" << findSolutions(i) << std::endl; } cout << total; return 0; } |
English