Cod sursa(job #3040030)

Utilizator LucaMuresanMuresan Luca Valentin LucaMuresan Data 29 martie 2023 11:26:45
Problema Radix Sort Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#include <bits/stdc++.h>

using namespace std;

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

/**
N * nr_cifre
00005 - 1
00913 - 2
49038 - 3
00053 - 4
59321 - 5
24089 - 6
00124 - 7
52389 - 8

(1, 2, 3, 4, 6, 7, 8); (5)
(1, 2,4, 7, 8); (3, 6); (5)
**/

const int NMAX = 1e7;
const int BITS = 12;
const int MASK = (1LL << BITS) - 1;
const int LGMAX = 32;

int pos[MASK + 1];
int f[MASK + 1];
int a[NMAX], aux[NMAX];

#define octet(x) (x >> bt) & MASK

int main()
{
    int n, A, B, C;
    in >> n >> A >> B >> C;

    /// generam sirul
    a[0] = B;
    for (int i=1; i<n; i++)
        a[i] = ((long long) A * a[i-1] + B) % C;

    for (int bt=0; bt<LGMAX; bt += BITS) /// pt fiecare octet
    {
        memset(f, 0, sizeof(f));
        for (int i=0; i<n; i++) /// f[oct] = de cate ori apare fiecare octet
            f[octet(a[i])]++;
        for (int i=1; i<=MASK; i++) /// pos[i] = pe ce pozitie ajunge
            pos[i] = pos[i-1] + f[i-1];
        for (int i=0; i<n; i++)
            aux[pos[octet(a[i])]++] = a[i];
        swap(aux, a);
    }

    for (int i=0; i<n; i+=10)
        out << a[i] << ' ';

    return 0;
}