Cod sursa(job #277640)

Utilizator Addy.Adrian Draghici Addy. Data 11 martie 2009 20:22:48
Problema Economie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.81 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];
  v[0] = 1;

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


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

  fclose(f);
  fclose(g);

  return 0;
}