Cod sursa(job #2609273)

Utilizator stefanpiturStefan Alexandru Pitur stefanpitur Data 2 mai 2020 13:10:00
Problema Radix Sort Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <fstream>
#include <cstdio>
#include <iostream>	
using namespace std;
	
ifstream fin("radixsort.in");
ofstream fout("radixsort.out");
	
const int N = 10000000;
const int BAZA = 11;
const int B = (1 << BAZA);
	
int poz[B], nr[B];
int v[2][N];
	
int main()	
{
    int n,a,b,c,i,j,k,vmax,nc,p,cif;
    fin >> n >> a >> b >> c;
    v[0][0] = vmax = b;
    for(i=1; i<n; i++){
        v[0][i] = (1LL * a * v[0][i-1] + 1LL * b) % c;
        vmax = max(vmax, v[0][i]);
    }
    nc = 0;
    while(vmax){
        nc++;
        vmax /= B;
    }
    p = 0;
    for(k=0; k<nc; k++){
        for(j=0; j<B; j++)
            nr[j] = 0;
        for(i=0; i<n; i++){
            cif = (v[k % 2][i] >> p) & (B - 1);
            nr[cif]++;
        }
        poz[0] = 0;
        for(j=1; j<B; j++)
            poz[j] = poz[j-1] + nr[j-1];
        for(i=0; i<n; i++){
            cif = (v[k % 2][i] >> p) & (B - 1);
            v[1 - k%2][poz[cif]++] = v[k % 2][i];
        }
        p += BAZA;
    }
    for(i=0; i<n; i+=10)
        fout << v[nc%2][i] << " ";	
    return 0;
}