Cod sursa(job #1926104)

Utilizator cordun_cristinaCristina Maria Cordun cordun_cristina Data 13 martie 2017 23:10:41
Problema Radix Sort Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 2.14 kb
/*#include <fstream>
#include <vector>

using namespace std;

const int BIT_COUNT = 8;
const int MASK = (1 << BIT_COUNT) - 1;
const int MAX_BITS = 32;
const int MAX_N = 10000000;

int N, Values[MAX_N];

void RadixSort(const int shift) {
    vector<int> buckets[MASK + 1];
    for (int i = 0; i < N; ++i)
        buckets[(Values[i] >> shift) & MASK].push_back(Values[i]);
    for (int b = 0, i = 0; b <= MASK; ++b)
        for (vector<int>::const_iterator v = buckets[b].begin(); v != buckets[b].end(); ++v)
            Values[i++] = *v;
}

void RadixSort() {
    for (int shift = 0; shift < MAX_BITS; shift += BIT_COUNT)
        RadixSort(shift);
}

void Read() {
    ifstream cin("radixsort.in");
    int a, b, c;
    cin >> N >> a >> b >> c;
    Values[0] = b;
    for (int i = 1; i < N; ++i)
        Values[i] = (1LL * Values[i - 1] * a + b) % c;
    cin.close();
}

void Print() {
    ofstream cout("radixsort.out");
    for (int i = 0; i < N; i += 10)
        cout << Values[i] << " ";
    cout << "\n";
    cout.close();
}

int main() {
    Read();
    RadixSort();
    Print();
    return 0;
}*/

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;
ifstream f("radixsort.in");
ofstream g("radixsort.out");

int n, nr;
long long v[10000000];
vector <int> bucket[1<<8];
const int comp =(1<<8) - 1;

void Read()
{
    int a, b, c;
    f>>n>>a>>b>>c;
    v[0] = b;
    for(int i = 1; i < n; i++) v[i] = (1LL*v[i-1]*a+b) % c;
    //for(int i = 1; i <= n ;i++) cout<<v[i]<<' ';
}

void Solve()
{
    for(int pos = 0; pos < 32 ; pos += 8)
    {
        nr = 0;
        for(int i = 0; i < n; i++)
            bucket[(v[i]>>pos) & comp].push_back(v[i]);
        for(int i = 0; i <= comp; i++)
        {
            for(int j = 0; j < (int)bucket[i].size(); j++)
                v[nr++] = bucket[i][j];
        }
        for(int i = 0; i <= comp; i++) bucket[i].clear();
    }
}

void Print()
{
    for(int i = 0; i < n; i += 10)
    {
        //cout<<i<<' ';
        g<<v[i]<<' ';}
    g<<'\n';
}

int main()
{
    Read();
    Solve();
    Print();
    return 0;
}