Cod sursa(job #1668328)

Utilizator Burbon13Burbon13 Burbon13 Data 29 martie 2016 18:55:21
Problema Radix Sort Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include <cstdio>
#include <vector>

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];
vector <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_back(v[j]);

        v[0] = 0;
        for(int j = 0; j < p2_8; ++j)
            if(l[j].size())
            {
                for(vector<int>::iterator it = l[j].begin(); it != l[j].end(); ++it)
                    v[++v[0]] = *it;
                l[j].clear();
            }
    }

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

    return 0;
}