ACM (UVa) : 406


#include <stdio.h>
#include <math.h>

long a [1010], index;

void is_prime (long x)
{
	long i, flag = 0;
	long length = (long) sqrt (x);

	for (i = 2; i <= length; i++) {
		if (x % i == 0) {
			flag = 1;
			break;
		}
	}

	if (!flag)
		a [index++] = x;
}

int main ()
{
	long n, c;

	while (scanf ("%ld %ld", &n, &c) != EOF) {

		printf("%ld %ld:", n, c);
		
		index = 0;
		long i;
		for (i = 1; i <= n; i++)
			is_prime (i);

		if (index % 2) {
			c = (c * 2) - 1;
			i = (long) (ceil(index/2) - floor(c/2));

			if (i <= 0) {
				for (i = 0; i < index; i++)
					printf(" %ld", a [i]);
			}

			else {
				while (i <= (ceil(index/2) + floor(c/2))) 
					printf(" %ld", a [i++]);
			}
		}

		else {
			c = c * 2;
			i = ((index/2)+1) - (c/2) - 1;

			if (i <= 0) {
				for (i = 0; i < index; i++)
					printf(" %ld", a [i]);
			}

			else {
				while (i < ((index/2) + (c/2)))
					printf(" %ld", a [i++]);
			}
		}
		printf ("\n\n");
	}

	return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s