Cod sursa(job #2136472)

Utilizator inquisitorAnders inquisitor Data 19 februarie 2018 22:20:32
Problema Radix Sort Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
#include <cstdio>
#include <queue>

int N, A, B, C, x, y, ten;

std :: queue<int> bucket[2][256];

int main()
{
    freopen("radixsort.in", "r", stdin);
    freopen("radixsort.out", "w", stdout);

    scanf("%d %d %d %d", &N, &A, &B, &C);

    x = B;

    bucket[0][x & 255].push(x);

    for(int i = 1; i < N; ++i)
    {
        y = (1LL * x * A + B) % C;

        bucket[0][y & 255].push(y);

        x = y;
    }

    for(int i = 0; i < 256; i++)
    {
        while(!bucket[0][i].empty())
        {
            x = bucket[0][i].front(); bucket[0][i].pop();

            bucket[1][x >> 8 & 255].push(x);
        }
    }

    for(int i = 0; i < 256; i++)
    {
        while(!bucket[1][i].empty())
        {
            x = bucket[1][i].front(); bucket[1][i].pop();

            bucket[0][x >> 16 & 255].push(x);
        }
    }

    for(int i = 0; i < 256; i++)
    {
        while(!bucket[0][i].empty())
        {
            x = bucket[0][i].front(); bucket[0][i].pop();

            bucket[1][x >> 24 & 255].push(x);
        }
    }

    for(int i = 0; i < 256; i++)
    {
        while(!bucket[1][i].empty())
        {
            if(ten++ % 10 == 0) printf("%d ", bucket[1][i].front());

            bucket[1][i].pop();
        }
    }

    return 0;
}