Cod sursa(job #1240764)

Utilizator ArkinyStoica Alex Arkiny Data 12 octombrie 2014 00:21:49
Problema Radix Sort Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
#include<fstream>

using namespace std;


ifstream in("radixsort.in");
ofstream out("radixsort.out");

unsigned long v[1000001];
struct Node
{
   unsigned long data;
   Node *next;
};

Node *list[10];
Node *last[10];

void addToList(int id,unsigned long data)
{
	if(list[id]==0)
	{
		list[id]=new Node;
		list[id]->data=data;
		list[id]->next=0;
		last[id]=list[id];
		
	}
	else
	{
		Node *p=new Node;
		p->data=data;
		p->next=0;
		last[id]->next=p;
		last[id]=p;
	}
}


int main()
{

	unsigned long N,A,B,C;

	in>>N>>A>>B>>C;

	v[1]=B;
	unsigned long max=v[1];
	int m=10;
	int d=1;
	  for(unsigned long i=2;i<=N;i++)
	  {
		 v[i]=(A*v[i-1]+B)%C;
		if(v[i]>max)
			max=v[i];
	  }


	  for(;max/d>0;)
	  {
		  for(unsigned long i=1;i<=N;i++)
				addToList((v[i]%m)/d,v[i]);
          int i=0;
		  int j=1;

		  while(i<10)
		  {
			  Node *p=list[i];
			  while(p!=NULL)
			  {
				   v[j++]=p->data;
				 
				 Node *q=p;
				  p=p->next;
				 delete q;
			  }
			  list[i]=0;
			  last[i]=0;
			  i++;
		  }
          if(max/d>10)
		  {
			 m=m*10;
			 d=d*10;
		  }
		  else
			  break;
	  }

	  for(unsigned long i=1;i<=N;i++)
		  if(i%10==1)
			  out<<v[i]<<" ";


	in.close();
	out.close();
	return 0;
}