#include<iostream>
#include<fstream>
using namespace std;
ifstream f("radixsort.in");
ofstream g("radixsort.out");
struct nod
{
int info;
nod *next;
};
nod* v0=NULL,*s0=NULL,*v1=NULL,*s1=NULL,*v2=NULL,*s2=NULL,*v3=NULL,*s3=NULL,* v4=NULL,*s4=NULL,*v5=NULL,*s5=NULL,*v6=NULL,*s6=NULL,*v7=NULL,*s7=NULL,*v8=NULL,*s8=NULL,*v9=NULL,*s9=NULL;
void push(nod* &v, nod* &sf, int x)
{
nod *c;
if(!v)
{
v=new nod;
v->info=x;
v->next=0;
sf=v;
}
else
{
c=new nod;
sf->next=c;
c->info=x;
sf=c;
sf->next=0;
}
}
void afisare(nod *v)
{
nod *c;
c=v;
while(c)
{
cout<<c->info<<" ";
c=c->next;
}
}
void pop(nod* &v)
{
nod* p;
if(!v) cout<<"nu";
else
{
p=v;
v=v->next;
delete p;
}
}
int peek(nod*&varf)
{
return varf->info;
}
bool empty(nod*&varf)
{
if (!varf) return 0;
else return 1;
}
void add(int i,int c,int v[])
{
switch(c)
{
case 0: {push(v0,s0,v[i]); break;}
case 1: {push(v1,s1,v[i]); break;}
case 2: {push(v2,s2,v[i]); break;}
case 3: {push(v3,s3,v[i]); break;}
case 4: {push(v4,s4,v[i]); break;}
case 5: {push(v5,s5,v[i]); break;}
case 6: {push(v6,s6,v[i]); break;}
case 7: {push(v7,s7,v[i]); break;}
case 8: {push(v8,s8,v[i]); break;}
case 9: {push(v9,s9,v[i]); break;}
default: break;
}
}
void out(nod*&v0,int &i,int v[])
{
int y;
while(empty(v0)==1)
{
y=peek(v0); v[i]=y;
pop(v0);
i++;
}
}
int main()
{
int n,i,j,maxi,b,c,nrcif=0,m,d,a;
bool ok;
cout<<"Introduceti n,a,b,c ";
f>>n>>a>>b>>c;
int v[n];
v[1]=b;
maxi=b;
for (i =2; i <=n; i++)
{
v[i]=(a*v[i-1]+b)%c;
if(v[i]>maxi)
maxi=v[i];
}
while(maxi)
{
nrcif++;
maxi/=10;
}
m=10; d=1;
while(nrcif)
{
for(i=1;i<=n;i++)
{
c=(v[i]%m)/d;
add(i,c,v);
}
for(j=0;j<=9;j++)
{
i=1;
out(v0,i,v);
out(v1,i,v);
out(v2,i,v);
out(v3,i,v);
out(v4,i,v);
out(v5,i,v);
out(v6,i,v);
out(v7,i,v);
out(v8,i,v);
out(v9,i,v);
}
m*=10;
d*=10;
nrcif--;
}
for(i=1;i<=n;i+=10)
g<<v[i]<<" ";
return 0;
}