Cod sursa(job #2611207)

Utilizator maramihaliMara Mihali maramihali Data 6 mai 2020 16:08:26
Problema Radix Sort Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <bits/stdc++.h>

using namespace std;

ifstream in("radixsort.in");
ofstream out("radixsort.out");

const int MAX = 10000000;
const int B = 1 << 11;

int nr[B], aux[B], poz[MAX], v[MAX];
int a, b, c, n, vmax, nc, cif;

int main()
{
    in >> n >> a >> b >> c;
    v[0] = vmax = b;
    for(int i = 1; i < n; i++)
    {
        v[i] = (1LL * a * v[i - 1] + 1LL * b) % c;
        vmax = max(vmax, v[i]);
    }
    nc = 0;
    while(vmax)
    {
        nc++;
        vmax /= B;
    }
    int p = 1;
    for(int k = 0; k < nc; k++)
    {
        for(int j = 0; j < B; j++)
        {
            nr[j] = 0;
        }
        for(int i = 0; i < n; i++)
        {
            int cif = v[i] / p % B;
            nr[cif]++;
        }
        poz[0] = 0;
        for(int j = 1; j < B; j++)
        {
            poz[j] = poz[j - 1] + nr[j - 1];
        }
        for(int i = 0; i < n; i++)
        {
            int cif = v[i] / p % B;
            aux[poz[cif]++] = v[i];
        }
        for(int i = 0; i < n; i++)
        {
            v[i] = aux[i];
        }
        p *= B;
    }
    for(int i = 0; i < n; i += 10)
    {
        out << v[i] << " ";
    }
    return 0;
}