/**
* Angewandte Mathematik, SS11
* Problem: 406 - Prime Cuts
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=6&page=show_problem&problem=347
*
* @author Marco Wolff
* @author Christian Weber
* @author Christoph Waldleitner
* @version 1.0, 10/23/2011
*
* Method : Ad-Hoc
* Status : Runtime Error
* Runtime: 0.000
*/
import java.io.*;
import java.util.StringTokenizer;

public class Main
{
public static void main(String[] args)
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
boolean state = true;
while (state)
{
StringTokenizer tk = null;
try
{
tk = new StringTokenizer(br.readLine());
} catch (IOException e)
{
}
try
{
if (!tk.hasMoreTokens())
state = false;
int first = Integer.parseInt(tk.nextToken());
if (tk.hasMoreTokens())
{
int second = Integer.parseInt(tk.nextToken());
if (first > 0 && second > 0&&first<=1000)
{
boolean[] sieve = new boolean[first + 1];
int[] primes = new int[200];
int counter = 0;
int start = 0;
String out = "";

sieve[0] = true;
for (int i = 4; i < sieve.length; i = i + 2)
{
sieve[i] = true;
}
for (int i = 3; i <= Math.sqrt(sieve.length - 1); i = i + 2)
{
if (!sieve[i])
{
for (int j = i + 2; j < sieve.length; j = j + 2)
{
if (j % i == 0)
{
sieve[j] = true;
}
}
}
}
for (int i = 0; i < sieve.length; i++)
{
if (!sieve[i])
{
primes[counter] = i;
counter++;
}
}
out += first + " " + second + ":";
if (counter - (second * 2) < 0)
{
for (int i = 0; primes[i] != 0; i++)
{
out += " " + primes[i];
}
} else if (counter % 2 == 0)
{
start = (counter - second * 2) / 2;
int i = start + (second * 2);
for (; start < i; start++)
{
out += " " + primes[start];
}
} else
{
start = (counter - (second * 2 - 1)) / 2;
int i = start + ((second * 2) - 1);
for (; start < i; start++)
{
out += " " + primes[start];
}
}
System.out.println(out);
System.out.println();

} else
{
System.out.println(first + " " + second + ":" + "\n");
}
}
} catch (NumberFormatException e)
{
}
}
System.exit(0);
}

}


--------------------------------

1.


/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, SS10
* Problem: 406 Prime Cuts
* Link: http://uva.onlinejudge.org/external/4/406.pdf
*
* @author Anton Pavlushko, IBB7B,
* @version 1.0, 10/10/2010
*
* Status : Accepted
* Runtime: 2.616
*/

import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String current_line, empty = "";
int number, border, i, j, length, number_local, count;
int primes [];
Iterator iter;
int znak,count5,count5_already,multiply;
StringTokenizer input_string;
boolean status, at_least_one;
Vector counts = new Vector();
double try_it, base_log;
Map<Integer,Integer> mp_number=new HashMap<Integer,Integer>();
Map<Integer,Integer> mp_factorial=new HashMap<Integer,Integer>();

try {
while ((current_line=in.readLine())!= null && !current_line.equals("0")) {
input_string = new StringTokenizer(current_line);
number = Integer.parseInt(input_string.nextToken());
border = Integer.parseInt(input_string.nextToken());

if (!counts.isEmpty()) counts.clear();

primes = new int[number/2+1];
for(i=1;i<number/2+1;i++){
for(j=1;j<number/2+1 && j<=i && i+j+2*i*j<number/2+1;j++){
primes[i+j+2*i*j] = 1;
}
}

counts.add(1);
if (number>1) counts.add(2);
for(i=1;i<number/2+1;i++){
j=2*i+1;
if(primes[i]==0 && j<=number) {
counts.add(j);
}

}

if (counts.size()%2==0)
length=2*border;
else
length=2*border-1;

if (length>=counts.size()) length=counts.size();

count=(counts.size()-length)/2;

iter = counts.iterator();
// System.out.print(number+" "+border+"(size "+length+", from "+count+")"+":");
System.out.print(number+" "+border+":");
i=0;
j=1;
while(iter.hasNext()) {
// System.out.print(" "+iter.next());
if (i>=count && j<=length) {
System.out.print(" "+iter.next());
j++;
} else iter.next();
i++;
}
System.out.println();
System.out.println();

} // end while
} // end try
catch (IOException e) {
System.err.println("Error: " + e);
}
}
}


2.

/**

* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, SS10

 * Problem: 406 Prime Cuts

* Link: http://uva.onlinejudge.org/external/4/406.pdf

*

 * @author Viktoriya Ryabova

* @version 1.0, 05/30/2010

*

 * Method : Ad-Hoc

* Status : Accepted

* Runtime: 2.132

*/

import java.util.Scanner;

 

public class Main {

 

      public static void main(String[] args) {

            Scanner sc = new Scanner(System.in);

            while (sc.hasNext()) {

 

                  int n = sc.nextInt();

                  int c = sc.nextInt();

                  // abbruchbedingung

                  if (n==0 || c==0)return;

                  int count = 0;

                  int printCount;

                  int limit;

                  int start;

                 

                  int[] primeNumbers = new int[n];

                  // alle primzahlen von 1 bis n in array hinzufügen

                  for (int i = 1; i <= n; i++) {

                        if (isPrime(i)) {

                             primeNumbers[count] = i;

                             count++;

                        }

                  }

                  //es wird gerechnet, wie viele primzahlen ausgegeben werden und genauer abschnitt

                  //des arrays, der ausgegeben wird

                  if (count % 2 == 0) {

                        printCount = c * 2;

                        if (count > printCount) {

                             start = (count / 2 - printCount / 2);

                             limit = (count - printCount) / 2 + printCount;

                        } else {

                             start = 0;

                             limit = count;

                        }

 

                  } else {

                        printCount = c * 2 - 1;

                        if (count > printCount) {

                             start = (count - printCount + 1) / 2;

                             limit = (count - printCount + 1) / 2 + printCount;

                        } else {

                             start = 0;

                             limit = count;

                        }

                  }

                  //Ausgabe des ergebnisses

                  System.out.print(n + " " + c + ": ");

                  for (int i = start; i < limit; i++) {

                        if (primeNumbers[i] != 0 && i!=(limit-1))

                             System.out.print(primeNumbers[i] + " ");

                        else System.out.print(primeNumbers[i]);

                  }

                  System.out.println("\n");

            }

      }

 

      // Prüfen ob die zahl eine primzagl ist

      public static boolean isPrime(int n) {

            if (n == 1) {

                  return true;

            } else if (n == 2) {

                  return true;

            } else if (n % 2 == 0) {

                  return false;

            } else {

                  for (int i = 3; i * i <= n; i += 2) {

                        if (n % i == 0) {

                             return false;

                        }

                  }

                  return true;

            }

      }

}