Cod sursa(job #2675362)

Utilizator Marius7122FMI Ciltea Marian Marius7122 Data 21 noiembrie 2020 14:43:08
Problema Radix Sort Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
 
using namespace std;

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

typedef long long ll;

ll a, b, c;
int n;
vector<int> v;

// 101011 10101101 01010110 
// 000000 00000000 11111111 &
// 000000 00000000 01010110

// 2^0 + 2^1 + ... + 2^7 = 2^8 - 1

void radixSort(vector<int> &v, int basePow = 8)
{
    int base = 1 << basePow;
    int mask = base - 1;

    vector<int> aux(v.size()), cnt(base), start(base);
    int elMax = *max_element(v.begin(), v.end());
    int sh = 0;
    while((elMax >> sh) > 0)
    {
        cnt.assign(base, 0);
        for(int x : v)
            cnt[(x >> sh) & mask]++;
        start[0] = 0;
        for(int i = 1; i < base; i++)
            start[i] = start[i-1] + cnt[i-1];

        for(int x : v)
        {
            int coef = (x >> sh) & mask;
            aux[start[coef]] = x;
            start[coef]++;
        }

        v = aux;
        
        sh += basePow;
    }
}

int main()
{
    fin >> n >> a >> b >> c;
    v.resize(n);

    v[0] = b;
    for(int i = 1; i < n; i++)
        v[i] = (a * v[i-1] + b) % c;

    radixSort(v);

    for(int i = 0; i < n; i += 10)
        fout << v[i] << ' ';

    return 0;
}