Cod sursa(job #277476)

Utilizator Addy.Adrian Draghici Addy. Data 11 martie 2009 19:09:57
Problema Economie Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <stdio.h>
#define DIM 1001

int a[DIM],set[DIM],v[50002];
int n,i,ok,k,max,j,w,aux,x;

  FILE *f = fopen("economie.in", "r");
  FILE *g = fopen("economie.out", "w");

int main(){

  fscanf(f,"%d",&n);

  for (i=1; i<=n; i++)
    fscanf(f,"%d",&a[i]);

  do {
    ok = 0;
    for (i=1; i<n; i++)
      if (a[i] > a[i+1]) {
	aux = a[i];
	a[i] = a[i+1];
	a[i+1] = aux;
	ok = 1;
      }
  } while (ok);

  max = a[n];

  for (i=1; i<=n; i++)
    if (v[a[i]]==0) {
      set[++k] = a[i];
      j = 1;
      w = a[i];
      while (w*j <= max) {
	v[w*j] = 1;
	j++;
      }
      for (x=1; x<k; x++) {
	j = 1;
	w = set[x]+set[k];
	while (w*j <= max) {
	v[w*j] = 1;
	j++;
	}
      }
    }
    else {
      for (x=1; x<n; x++) {
	j = 1;
	w = a[x]+a[i];
	while (w*j <= max) {
	v[w*j] = 1;
	j++;
	}
      }
    }

  fprintf(g,"%d\n",k);
  for (i=1; i<=k; i++)
    fprintf(g,"%d\n",set[i]);

  fclose(f);
  fclose(g);

  return 0;
}