forked from improper4/uva
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UVa00406_PrimeCuts.java
60 lines (49 loc) · 1.46 KB
/
UVa00406_PrimeCuts.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
package uva;
/* USER: 46724 (sfmunera) */
/* PROBLEM: 347 (406 - Prime Cuts) */
/* SUBMISSION: 09078835 */
/* SUBMISSION TIME: 2011-07-25 17:08:48 */
/* LANGUAGE: 2 */
import java.util.*;
import java.io.*;
public class UVa00406_PrimeCuts {
static boolean[] sieve(int n) {
boolean[] primes = new boolean[n + 1];
Arrays.fill(primes, true);
primes[0] = false;
for (int p = 2; p * p <= n; ++p)
if (primes[p])
for (int i = p + p; i <= n; i += p)
primes[i] = false;
return primes;
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
boolean[] primes = sieve(1000);
String line;
while ((line = in.readLine()) != null) {
StringTokenizer stk = new StringTokenizer(line);
int N = Integer.parseInt(stk.nextToken());
int C = Integer.parseInt(stk.nextToken());
List<Integer> P = new ArrayList<Integer>();
for (int i = 1; i <= N; ++i)
if (primes[i])
P.add(i);
int len = P.size();
int mid = len / 2;
int a = Math.max(0, mid - C);
int b = Math.min(len - 1, a + 2 * C - 1);
if (len % 2 == 1) {
a = Math.max(0, mid - C + 1);
b = Math.min(len - 1, a + 2 * C - 2);
}
System.out.print(N + " " + C + ":");
for (int i = a; i <= b ; ++i)
System.out.print(" " + P.get(i));
System.out.println();
System.out.println();
}
in.close();
System.exit(0);
}
}