Cod sursa(job #2904829)

Utilizator BluThund3rRadu George BluThund3r Data 18 mai 2022 08:42:03
Problema Radix Sort Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <iostream>
#include <fstream>

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

const int Nmax = 1e7 + 5;
int v[Nmax], a, b, c, n, aux[Nmax];

void radixSort(int v[], const int Max, const int n) {
    int p = 1, cifra;

    while(Max >= p){
        int poscif[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        for(int i = 1; i <= n; ++ i)
            ++ poscif[v[i] / p % 10];

        for(int i = 1; i < 10; ++ i)
            poscif[i] += poscif[i - 1];

        for(int j = n; j >= 1; -- j) {
            cifra = v[j] / p % 10;
            aux[poscif[cifra]] = v[j];
            -- poscif[cifra];
        }

        for(int j = 1; j <= n; ++ j)
            v[j] = aux[j];

        p *= 10;
    }
}

int main() {
    in >> n >> a >> b >> c;
    v[1] = b;
    int Max = v[1];
    for(int i = 2; i <= n; ++ i){
        v[i] = (1LL * a * v[i - 1] + b) % c;
        Max = max(Max, v[i]);
    }

    radixSort(v, Max, n);

    for(int i = 1; i <= n; i += 10)
        out << v[i] << ' ';

    return 0;
}