Cod sursa(job #1668336)

Utilizator Burbon13Burbon13 Burbon13 Data 29 martie 2016 18:57:45
Problema Radix Sort Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include <cstdio>
#include <queue>

using namespace std;

const int nmx = 10000002;
const int p2_8 = 256;
const long long galeti[] = {255,255 << 8,255 << 16, 1LL*255 << 24};
const int shift[] {0,8,16,24};

int n,a,b,c;
unsigned int v[nmx];
queue <int> l[260];

int pos(int val, int i)
{
    return (val & galeti[i]) >> shift[i];
}

int main()
{
    freopen("radixsort.in", "r", stdin);
    freopen("radixsort.out", "w", stdout);
    scanf("%d %d %d %d", &n, &a, &b, &c);
    v[1] = b;
    for(int i = 2; i <= n; ++i)
        v[i] = (1LL * a * v[i-1] + b) % c;
    for(int i = 0; i < 4; ++i)
    {
        for(int j = 1; j <= n; ++j)
                    l[pos(v[j],i)].push(v[j]);

        v[0] = 0;
        for(int j = 0; j < p2_8; ++j)
            while(not l[j].empty())
            {
                    v[++v[0]] = l[j].front();
                l[j].pop();
            }
    }

    for(int i = 1; i <= n; i += 10)
        printf("%d ", v[i]);

    return 0;
}