-
Notifications
You must be signed in to change notification settings - Fork 0
/
10852.java
60 lines (54 loc) · 1.54 KB
/
10852.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
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
private static final int N = 10000;
private static int[] prime = new int[N + 2];
private static List<Integer> primes = new ArrayList<>();
static
{
int lmt = (int)Math.sqrt(N);
prime[2] = 2;
// primes.add(2);
for(int i = 3; i <= N; i += 2)
{
if(prime[i] == 0)
{
primes.add(i);
prime[i] = i;
if(i <= lmt)
for(int j = i * i; j <= N; j += i * 2)prime[j] = 1;
}
}
/*for(int i = 3; i <= N; i++)
if(prime[i] == 1 || prime[i] == 0)prime[i] = prime[i - 1];*/
}
public static void main(String[] args)
{
Scanner read = new Scanner(System.in);
int testCase = read.nextInt();
int x, pm;
for(int test = 1; test <= testCase; test++)
{
int num = read.nextInt();
int size = primes.size();
int min = primes.get(size - 1) * N;
pm = 0;
x = 0;
int diff = 0;
for(int i = 0; i < size && primes.get(i) <= num; i++)
{
int xx = primes.get(i);
pm = (int) num / xx;
if(diff < num - pm * xx)
{
diff = num - pm * xx;
x = xx;
}
}
System.out.println(x);
}
read.close();
}
}