Cod sursa(job #2389707)

Utilizator AndreiTudorSpiruAndrei Spiru AndreiTudorSpiru Data 27 martie 2019 13:27:56
Problema Radix Sort Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <fstream>
#include <queue>
using namespace std;

#define MOD 0xff
queue <int> bucket[2][256];
ifstream f("radixsort.in");
ofstream g("radixsort.out");
int swc,n,a,b,c,i;
void read()
{
    f>>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)g<<bucket[swc][i].front()<<" ";
            bucket[swc][i].pop();
            k++;
        }
    }
}
int main()
{
    read();
    solve();
    print();
    return 0;
}