-
Notifications
You must be signed in to change notification settings - Fork 0
/
914.java
105 lines (91 loc) · 3.32 KB
/
914.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
95
96
97
98
99
100
101
102
103
104
105
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
private static final int N = 1000000;
private static final int BIGGEST_DIFF = 114;
private static int[] mark = new int[N + 2];
private static int[] max = new int[BIGGEST_DIFF + 2];
private static Vector<Integer> primeList = new Vector<Integer>();
private static void sieve()
{
Arrays.fill(mark, 1);
primeList.add(2);
int limit = (int)Math.sqrt(N);
for(int i = 4; i <= N; i += 2)mark[i] = 0;
for(int i = 3; i <= N; i += 2)
{
if(mark[i] == 1)
{
primeList.add(i);
if(i <= limit)
for(int j = i * i; j <= N; j += i * 2)mark[j] = 0;
}
}
}
private static int binarySearch(int firstIndex, int lastIndex, int num)
{
if(firstIndex >= primeList.size())return primeList.size() - 1;
if(lastIndex < 0) return 0;
int mid = (int) (firstIndex + lastIndex) / 2;
if(primeList.get(mid) == num) return mid;
else if(mid - 1 > 0 && primeList.get(mid) > num && primeList.get(mid - 1) < num) return mid - 1;
else if(num < primeList.get(mid)) return binarySearch(firstIndex, mid - 1, num);
else return binarySearch(mid + 1, lastIndex, num);
}
public static void main(String[] args) throws Exception
{
sieve();
int size = primeList.size() - 1;
Scanner read = new Scanner(System.in);
/*Scanner read = FileFactory.read();
BufferedWriter write = FileFactory.write();*/
int testCase = read.nextInt();
for(int test = 1; test <= testCase; test++)
{
int lowerLimit = read.nextInt();
int upperLimit = read.nextInt();
int lowerIndex = binarySearch(0, size, lowerLimit);
int upperIndex = binarySearch(0, size, upperLimit);
if(primeList.get(lowerIndex) < lowerLimit) lowerIndex++;
if(primeList.get(upperIndex) > upperLimit) upperIndex--;
Arrays.fill(max, 0);
int ans = 0, number;
for(int i = lowerIndex + 1; i <= upperIndex; i++)
{
int index = primeList.get(i) - primeList.get(i - 1);
max[index]++;
}
number = - 1;
for(int i = 1; i <= BIGGEST_DIFF; i++)
{
//System.out.print(max[i]+" ");
if(ans < max[i])
{
ans = max[i];
number = i;
}
else if(ans == max[i])
{
number = -1;
}
}
// System.out.println();
if(number == -1)
{
System.out.println("No jumping champion");
/* write.write("No jumping champion");
write.write("\n");*/
}
else
{
/*write.write("The jumping champion is " + Integer.toString(number));
write.write("\n");*/
System.out.println("The jumping champion is " + number);
}
}
/* FileFactory.close();*/
read.close();
}
}