-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathariprog.java
94 lines (87 loc) · 2.31 KB
/
ariprog.java
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.StringTokenizer;
/*
ID: Xu Yan
LANG: JAVA
TASK: ariprog
*/
/**
* Thoughts:
* Pitfalls:
*
* Take-away tips:
*
* @author Xu Yan
* @date May 10th,2016
*/
public class ariprog {
private static class Result {
int base;
int diff;
public Result(int base, int diff) {
this.base = base;
this.diff = diff;
}
}
public static void main (String [] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("ariprog.in"));
int N = Integer.parseInt(new StringTokenizer(f.readLine()).nextToken()); // The length of required progressions
int M = Integer.parseInt(new StringTokenizer(f.readLine()).nextToken()); // The search upper bound
f.close();
ariprog solver = new ariprog();
boolean[] isBiSquare = new boolean[2 * M * M + 1];
for (int i = 0; i <= M; i++) {
for (int j = i; j <= M; j++) {
isBiSquare[i * i + j * j] = true;
}
}
List<Result> results = solver.solve(isBiSquare, M, N);
Collections.sort(results, new Comparator<Result>() {
@Override
public int compare(Result o1, Result o2) {
return o1.diff != o2.diff ? (o1.diff - o2.diff) : (o1.base - o2.base);
}
});
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("ariprog.out")));
if (results.isEmpty()) {
out.println("NONE");
} else {
for (Result r : results) {
out.println(r.base + " " + r.diff);
}
}
out.close();
}
private List<Result> solve(boolean[] isBiSquare, int M, int N) {
List<Result> results = new ArrayList<Result>();
for (int i = 0; i < isBiSquare.length; i++) {
if (isBiSquare[i]) {
for (int j = i + 1; i + (N-1) * (j-i) <= 2 * M * M; j++) {
if (isBiSquare[j]) {
if (search(isBiSquare, i, j - i, N)) {
results.add(new Result(i, j - i));
}
}
}
}
}
return results;
}
private boolean search(boolean[] isBiSquare, int i, int diff, int N) {
for (int k = 1; k < N; k++) {
if (!isBiSquare[i + k * diff]) {
return false;
}
}
return true;
}
}