(暴力枚举) $O(N^3)$
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Main {
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private static PrintWriter writer = new PrintWriter(System.out);
public static void main(String[] args) throws IOException{
int n = Integer.parseInt(reader.readLine());
reader.close();
for (int a = 0; a * a * 4 <= n; a ++)
for (int b = a; b * b * 3 <= n - a * a; b ++)
for (int c = b; c * c * 2 <= n - a * a - b * b; c ++) {
int d = (int) Math.sqrt(n - a * a - b * b - c * c);
if (n == a * a + b * b + c * c + d * d) {
if (c > d) {
int temp = c;
c = d;
d = temp;
}
writer.println(a + " " + b + " " + c + " " + d);
writer.flush();
writer.close();
return;
}
}
}
}
(二分) $O(N^2logN)$
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
public class Main {
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private static PrintWriter writer = new PrintWriter(System.out);
static class Sum {
int s, c, d;
public Sum(int s, int c, int d) {
this.s = s;
this.c = c;
this.d = d;
}
}
private static List<Sum> list = new ArrayList<Sum>();
public static void main(String[] args) throws IOException{
int n = Integer.parseInt(reader.readLine());
reader.close();
for (int c = 0; c * c <= n; c ++)
for (int d = 0; c * c + d * d <= n; d ++)
list.add(new Sum(c * c + d * d, c, d));
// 字典序排序
list.sort((o1, o2) -> {
if (o1.s != o2.s) return o1.s - o2.s;
if (o1.c != o2.c) return o1.c - o2.c;
return o1.d - o2.d;
});
// 枚举符合条件的前两项
for (int a = 0; a * a <= n; a ++) {
for (int b = a; a * a + b * b <= n; b ++) {
int x = n - a * a - b * b;
// 二分查到x是否存在后两项中
int l = 0, r = list.size() - 1;
while (l < r) {
int mid = l + r >> 1;
if (list.get(mid).s >= x) r = mid;
else l = mid + 1;
}
// 如果找到的话
if (list.get(l).s == x) {
int c = list.get(l).c;
int d = list.get(l).d;
writer.println(a + " " + b + " " + c + " " + d);
writer.flush();
writer.close();
return;
}
}
}
}
}
(哈希表) $O(N^2)$
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
public class Main {
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private static PrintWriter writer = new PrintWriter(System.out);
static class Node {
int c, d;
public Node(int c, int d) {
this.c = c;
this.d = d;
}
}
private static HashMap<Integer, Node> S = new HashMap<>();
public static void main(String[] args) throws IOException{
int n = Integer.parseInt(reader.readLine());
reader.close();
for (int c = 0; c * c <= n; c ++) {
for (int d = 0; c * c + d * d <= n; d ++) {
int t = c * c + d * d;
if (!S.containsKey(t)) {
S.put(t, new Node(c, d));
}
}
}
for (int a = 0; a * a <= n; a ++) {
for (int b = 0; a * a + b * b <= n; b ++) {
int t = n - a * a - b * b;
if (S.containsKey(t)) {
writer.println(a + " " + b + " " + S.get(t).c + " " + S.get(t).d);
writer.flush();
writer.close();
return;
}
}
}
}
}
666