Cod sursa(job #2224879)

Utilizator andreisontea01Andrei Sontea andreisontea01 Data 25 iulie 2018 14:09:45
Problema Radix Sort Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

const int MAXBIT = 1 << 16;

vector<int> num, counting[MAXBIT];

int main()
{
    ifstream fin("radixsort.in");
    ofstream fout("radixsort.out");
    int n, a, b, c;
    fin >> n >> a >> b >> c;
    long long mask = (1LL << 16) - 1;
    num.push_back(b);
    for(int i = 1; i < n; ++i)
        num.push_back((a * num[i - 1] + b) % c);
    for(int i = 0; i < n; ++i){
        int pos = num[i] & mask;
        counting[pos].push_back(num[i]);
    }
    num.clear();
    for(int i = 0; i < MAXBIT; ++i){
        for(int j = 0; j < counting[i].size(); ++j)
            num.push_back(counting[i][j]);
        counting[i].clear();
    }
    mask <<= 16;
    for(int i = 0; i < n; ++i){
        int pos = num[i] & mask;
        pos >>= 16;
        counting[pos].push_back(num[i]);
    }
    int index = 1;
    for(int i = 0; i < MAXBIT; ++i){
        for(int j = 0; j < counting[i].size(); ++j){
            if(index % 10 == 1)
                fout << counting[i][j] << " ";
            index++;
        }
    }
    return 0;
}