Pagini recente » Cod sursa (job #2827987) | Cod sursa (job #2609021) | Cod sursa (job #855613) | Cod sursa (job #2655291) | Cod sursa (job #1313250)
#include <cstdio>
#include <deque>
#define MAXN 10000001
#define pb push_back
#define pf pop_front
using namespace std;
int sol[MAXN], nr;
deque <int> stiva0, stiva1, stiva2, stiva3, stiva4, stiva5, stiva6, stiva7, stiva8, stiva9;
int nrcif (int x)
{
int m=x, nr=0;
while (m>0)
{
m/=10;
nr++;
}
return nr;
}
int cif_i (int x, int i)
{
int m=x, nr=1;
while (m>0)
{
if (nr==i) return m%10;
m/=10; nr++;
}
}
void goleste_si_toarna_stivele (void)
{
nr=0;
while (!stiva0.empty())
{
sol[++nr]=stiva0.front();
stiva0.pf();
}
while (!stiva1.empty())
{
sol[++nr]=stiva1.front();
stiva1.pf();
}
while (!stiva2.empty())
{
sol[++nr]=stiva2.front();
stiva2.pf();
}
while (!stiva3.empty())
{
sol[++nr]=stiva3.front();
stiva3.pf();
}
while (!stiva4.empty())
{
sol[++nr]=stiva4.front();
stiva4.pf();
}
while (!stiva5.empty())
{
sol[++nr]=stiva5.front();
stiva5.pf();
}
while (!stiva6.empty())
{
sol[++nr]=stiva6.front();
stiva6.pf();
}
while (!stiva7.empty())
{
sol[++nr]=stiva7.front();
stiva7.pf();
}
while (!stiva8.empty())
{
sol[++nr]=stiva8.front();
stiva8.pf();
}
while (!stiva9.empty())
{
sol[++nr]=stiva9.front();
stiva9.pf();
}
}
int main()
{
int n, x, y, z, i, j, m, maxcif;
freopen("radixsort.in","r",stdin);
freopen("radixsort.out","w",stdout);
scanf("%d%d%d%d",&n,&x,&y,&z);
sol[1]=y; maxcif=nrcif(sol[1]);
for (i=2; i<=n; i++)
{
sol[i]=(x*sol[i-1]+y)%z;
m=nrcif(sol[i]);
if (m>maxcif) maxcif=m;
}
for (i=1; i<=maxcif; i++)
{
for (j=1; j<=n; j++)
{
m=cif_i(sol[j],i);
if (m==0) stiva0.pb(sol[j]);
if (m==1) stiva1.pb(sol[j]);
if (m==2) stiva2.pb(sol[j]);
if (m==3) stiva3.pb(sol[j]);
if (m==4) stiva4.pb(sol[j]);
if (m==5) stiva5.pb(sol[j]);
if (m==6) stiva6.pb(sol[j]);
if (m==7) stiva7.pb(sol[j]);
if (m==8) stiva8.pb(sol[j]);
if (m==9) stiva9.pb(sol[j]);
}
goleste_si_toarna_stivele();
}
for (i=1; i<=n; i+=10) printf("%d ",sol[i]);
printf("\n");
fclose(stdin);
fclose(stdout);
return 0;
}