Cod sursa(job #2897525)

Utilizator lolismekAlex Jerpelea lolismek Data 3 mai 2022 23:00:49
Problema Radix Sort Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int N = (int)1e7, BASE = 256;
int v[N + 1], w[N + 1], f[BASE + 1], poz[BASE + 1];

int main(){
	int n, a, b, c, maxi = -1;
	fin >> n >> a >> b >> c;

	v[1] = b;
	for(int i = 2; i <= n; i++)
		v[i] = (1ll * a * v[i - 1] + b) % c,
		maxi = max(maxi, v[i]);

	for(int p = BASE, e = 0; p <= maxi * BASE; p *= BASE, e += 8){
		for(int cif = 0; cif <= BASE; cif++)
			f[cif] = poz[cif] = 0;

		int masked_wolf_gang = p - 1;

		for(int i = 1; i <= n; i++)
			f[(v[i] & masked_wolf_gang) >> e]++;
		for(int cif = 1; cif <= BASE; cif++)
			f[cif] += f[cif - 1];

		for(int i = 1; i <= n; i++){
			int cif = ((v[i] & masked_wolf_gang) >> e);

			poz[cif]++;

			if(cif == 0)
				w[poz[cif]] = v[i];
			else
				w[f[cif - 1] + poz[cif]] = v[i];
		}

		for(int i = 1; i <= n; i++)
			v[i] = w[i];
	}

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

	return 0;
}