forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB.java
More file actions
48 lines (40 loc) · 1.17 KB
/
Copy pathB.java
File metadata and controls
48 lines (40 loc) · 1.17 KB
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
import java.util.*;
import java.io.*;
import java.math.*;
public class B
{
public static void main (String[] argv) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(in.readLine());
int kase = Integer.parseInt(st.nextToken());
for (int k = 1; k <= kase; ++k) {
System.out.print(String.format("Case #%s: ", k));
st = new StringTokenizer(in.readLine());
int n = Integer.parseInt(st.nextToken());
ArrayList<BigInteger> list = new ArrayList<BigInteger>();
for (int i = 0; i < n; ++i) {
BigInteger b = new BigInteger(st.nextToken());
list.add(b);
}
Collections.sort(list);
BigInteger g = list.get(1).subtract(list.get(0));
for (int i = 2; i <= n - 1; ++i) {
BigInteger d = list.get(i).subtract(list.get(i - 1));
g = B.gcd(g, d);
}
BigInteger y = list.get(0).mod(g);
if (y != BigInteger.ZERO) y = g.subtract(y);
System.out.println(y);
}
}
private static BigInteger gcd(BigInteger a, BigInteger b) {
while (true) {
if (b == BigInteger.ZERO) return a;
a = a.mod(b);
BigInteger c = b;
b = a;
a = c;
}
}
}