miércoles, 21 de septiembre de 2011

406 - Prime Cuts

#include<cstdio>
#define MAX 1000
using namespace std;
int crivo[MAX + 1];
int primos[MAX + 1];
int n_primos = 0;
void init_crivo() {
 primos[n_primos++] = 1;
 for (int i = 2; i <= MAX; i++)
  if(!crivo[i]) {
   primos[n_primos++] = i;
   for (int j = i; i * j <= MAX; j++)
    crivo[i * j] = 1;
  }
}
int main() {
 int n, c;
 init_crivo();
 while(scanf("%d%d", &n, &c) != EOF) {
  int cont = 0;
  while(primos[cont++] <= n && cont <= n_primos);
  cont--;
  int a, b;
  if(cont % 2 != 0) {
   a = cont / 2 - (2 * c - 1) / 2;
   if(a < 0) a = 0;
   b = cont / 2 + (2 * c - 1) / 2;
   if(b >= cont) b = cont - 1;
  }  
  else {
   a = cont / 2 - c;
   if(a < 0) a = 0; 
   b = cont / 2 + c - 1;
   if(b >= cont) b = cont - 1;
  }
  printf("%d %d:", n, c);
  for (int i = a; i <= b; i++)
   printf(" %d", primos[i]);
  printf("\n\n");
 }
 return 0;
}
In this problem we just simply find primes with crivo and then find the number of primes that we are going to use, or be it those number which are primes and less or equal to n, after that we just simply calculate if cont is even we have to show the amount 2*c from the center to both sides , c to left and c to right from the center, if cont is odd then we have another formula almost equal to the even and we expands it from the center to the left and to the right , that's it.

No hay comentarios:

Publicar un comentario