Cod sursa(job #2410076)

Utilizator LucaSeriSeritan Luca LucaSeri Data 19 aprilie 2019 18:26:19
Problema Radix Sort Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include <bits/stdc++.h>
 
using namespace std;
 
int getByte(int x, int i) {
	return (x>>(16*i))&((1<<16)-1);
}
 
const int MAXN = 1e7 + 10;
queue< int > radix[65536];
int v[MAXN];
 
int main() {
	ifstream f("radixsort.in");
	ofstream g("radixsort.out");

	ios::sync_with_stdio(false);
	f.tie(0);
	g.tie(0);
 
 
	int n, a, b, c;
	f >> n >> a >> b >> c;
 
	v[1] = b;
	for(int i = 2; i <= n; ++i) {
		v[i] = (1ll*a*v[i-1] + 1ll*b) % c;
	}
 
	for(int i = 0; i < 2; ++i) {
		for(int j = 1; j <= n; ++j) {
			radix[getByte(v[j], i)].push(v[j]);
		}
 
		int ind = 0;
		for(int j = 0; j < 65536; ++j) {
			while(radix[j].size()) {
				v[++ind] = radix[j].front();
				radix[j].pop();
			}
		}
 
 
		assert(ind == n);
	}
 
	for(int i = 1; i <= n; i += 10) g << v[i] << ' ';
	return 0;
}