Cod sursa(job #2224839)

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

using namespace std;

vector <int> v;

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.push_back(b);
    for (int i = 2; i <= n; ++i) {
        int newValue = static_cast<int>((1LL * a * v.back() + b) % c);
        v.push_back(newValue);
    }

    for (auto x : v) {
        int currentPosition = x & mask;
        countingSort[currentPosition].push_back(x);
    }

    v.clear();

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

    mask <<= 16;

    for (auto x : v) {
        long long currentPosition = x & mask;
        currentPosition >>= 16;
        countingSort[currentPosition].push_back(x);
    }

    v.clear();

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

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