Cod sursa(job #2389710)

Utilizator AndreiTudorSpiruAndrei Spiru AndreiTudorSpiru Data 27 martie 2019 13:30:05
Problema Radix Sort Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb

#include <cstdio>
#include <queue>
using namespace std;

#define MOD 0xff
queue <int> bucket[2][256];

int swc,n,a,b,c,i;
void read()
{
    freopen("radixsort.in", "r", stdin);
    freopen("radixsort.out", "w", stdout);
    scanf("%i%i%i%i", &n, &a, &b, &c);
    int prev=b,x;
    bucket[swc][prev%MOD].push(prev);
    for(i=1; i<n; i++)
    {
        x=(1LL *a*prev+b)%c;
        bucket[swc][x & MOD].push(x);
        prev=x;
    }
}
void solve()
{
    for(i=1; i<4; i++)
    {
        for(int j=0; j<256; j++)
        {
            while(!bucket[swc][j].empty())
            {
                int x=bucket[swc][j].front();
                bucket[swc][j].pop();
                bucket[1-swc][(x>>(8*i))%MOD].push(x);
            }
        }
        swc = 1-swc;
    }
}
void print()
{
    int k=1;
    for(i=0; i<256; i++)
    {
        while( !bucket[swc][i].empty() )
        {
            if(k%10==1)  printf("%i ", bucket[swc][i].front());
            bucket[swc][i].pop();
            k++;
        }
    }
}
int main()
{
    read();
    solve();
    print();
    return 0;
}