#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int radixsort(int *s, int n, int p, int b);
int main()
{
int n,a,b,c,*s;
int mx,bt,i;
FILE *f;
f = fopen("radixsort.in", "r");
fscanf(f,"%d",&n);
fscanf(f,"%d",&a);
fscanf(f,"%d",&b);
fscanf(f,"%d",&c);
fclose(f);
s = (int*)calloc(n,sizeof(int));
s[0] = b;
for(i=1;i<n;i++)
{
s[i]= ((1LL*a*s[i-1]) + b)%c;
}
bt = log2(c);
radixsort(s,n-1,0,bt);
f = fopen("radixsort.out", "w");
for(i=0; i<n; i+=10)
fprintf(f, "%d ", s[i]);
fclose(f);
// for(i=1; i<n; i++)
// if(s[i-1]>s[i]) printf("Greseala %d %d %d %d", i,s[i-1],s[i],c);
free(s);
return 0;
}
int radixsort(int *s, int n, int p, int b)
{
int i,t,c;
t = n;
if((p == n) || (b < 0)) return 0;
i=p;
while(i<t)
{
if (((s[i]>> b)&1) == 1) { c = s[t]; s[t]=s[i]; s[i]=c; t--;}
else i++;
}
if (((s[i]>> b)&1) == 1) i--; else t++;
radixsort(s, i, p, b-1);
radixsort(s, n, t, b-1);
return 0;
}