Cod sursa(job #2225031)

Utilizator vladm98Munteanu Vlad vladm98 Data 25 iulie 2018 18:34:33
Problema Radix Sort Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.19 kb
#include <bits/stdc++.h>

using namespace std;

int v[10000001];

vector <int> countingSort [1 << 16];

int main() {
    ifstream fin ("radixsort.in");
    ofstream fout ("radixsort.out");
    long long n, a, b, c, mask = (1 << 16) - 1;
    fin >> n >> a >> b >> c;
    v[1] = b;
    for (int i = 2; i <= n; ++i) {
        int newValue = static_cast<int>((1LL * a * v[i - 1] + b) % c);
        v[i] = newValue;
    }

    for (int i = 1; i <= n; ++i) {
        int currentPosition = v[i] & mask;
        countingSort[currentPosition].push_back(v[i]);
    }

    int position = 1;

    for (int i = 0; i < (1 << 16); ++i) {
        for (auto x : countingSort[i]) {
            v[position++] = x;
        }
        countingSort[i].clear();
    }

    mask <<= 16;

    for (int i = 1; i <= n; ++i) {
        long long currentPosition = v[i] & mask;
        currentPosition >>= 16;
        countingSort[currentPosition].push_back(v[i]);
    }

    position = 1;

    for (int i = 0; i < (1 << 16); ++i) {
        for (auto x : countingSort[i]) {
            v[position++] = x;
        }
        countingSort[i].clear();
    }

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