Cod sursa(job #1926115)

Utilizator cordun_cristinaCristina Maria Cordun cordun_cristina Data 13 martie 2017 23:18:22
Problema Radix Sort Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <iostream>
#include <fstream>
#include <vector>

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

int n, nr;
int v[10000005];
const int comp =(1<<8) - 1;

void Read()
{
    int a, b, c;
    f>>n>>a>>b>>c;
    v[1] = b;
    for(int i = 2; 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)
    {
        vector <int> bucket[1<<8];
        nr = 1;
        for(int i = 1; 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];
        }
    }
}

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

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