structSum { int s, c, d; booloperator<(const Sum& t) const// 小于号重载,用于排序 { if (s != t.s) return s < t.s; if (c != t.c) return c < t.c; return d < t.d; } }sum[N];
int n, m;// m表示存下了多少个s
intmain() { cin >> n; // 枚举s,记录此时的c和d for (int c = 0; c * c <= n; c++) for (int d = c; c * c + d * d <= n; d++) sum[m++] = { c * c + d * d,c,d };
// 排序 sort(sum, sum + m);
// 枚举a和b for (int a = 0; a * a <= n; a++) for (int b = a; a * a + b * b <= n; b++) { int t = n - a * a - b * b; // 二分查找表中是否有t int l = 0, r = m - 1; while (l < r) { int mid = (l + r) / 2; if (sum[mid].s >= t) r = mid; else l = mid + 1; } if (sum[l].s == t) { printf("%d %d %d %d\n", a, b, sum[l].c, sum[l].d); return0; } } return0; }
#include<iostream> #include<algorithm> #include<cstdio> #include<unordered_map> #define x first #define y second usingnamespace std; typedef pair<int, int> PII;
constint N = 2500010; int n; unordered_map<int, PII> S; // 记录字典序最小的c和d
intmain() { cin >> n; // 枚举s,记录最小的c和d for (int c = 0; c * c <= n; c++) for (int d = c; c * c + d * d <= n; d++) { int t = c * c + d * d; if (S.count(t) == 0) S[t] = { c,d }; }
// 枚举a和b for (int a = 0; a * a <= n; a++) for (int b = a; a * a + b * b <= n; b++) { int t = n - a * a - b * b; if (S.count(t)) { printf("%d %d %d %d\n", a, b, S[t].x, S[t].y); return0; } } return0; }