Cod sursa(job #2868094)

Utilizator QwertyDvorakQwerty Dvorak QwertyDvorak Data 10 martie 2022 18:41:56
Problema Radix Sort Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.67 kb
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define dbg(x) cout << #x <<": " << x << "\n";
using ll = long long;

class InP {
	FILE *fin;
	char *buff;
	int sp;

public:
	InP(const char *p) {
		fin = fopen(p, "r");
		buff = new char[4096]();
		sp = 4095;
	}
	~InP() {
		fclose(fin);
	}
	char read_ch() {
		sp++;
		if (sp == 4096) {
			fread(buff, 1, 4096, fin);
			sp = 0;
		}
		return buff[sp];
	}

	InP &operator >>(int &n) {
		char c;
		int sgn = 1;
		while (!isdigit(c = read_ch()) && c != '-');
		if (c == '-') {
			n = 0;
			sgn = -1;
		}
		else n = c - '0';
		while (isdigit(c = read_ch()))
			n = n * 10 + c - '0';
		n = n * sgn;
		return *this;
	}

	InP &operator >>(ll &n) {
		char c;
		int sgn = 1;
		while (!isdigit(c = read_ch()) && c != '-');
		if (c == '-') {
			n = 0;
			sgn = -1;
		}
		else n = c - '0';
		while (isdigit(c = read_ch()))
			n = n * 10LL + c - '0';
		n = n * sgn;
		return *this;
	}

	InP &operator >> (char &n) {
		n = read_ch();
		while ((n = read_ch()) != '\n' && n != ' ');
		return *this;
	}

};

class OuP {
	FILE *fout;
	char *buff;
	int sp;

public:

	OuP(const char *p) {
		fout = fopen(p, "w");
		buff = new char[50000];
		sp = 0;
	}
	~OuP() {
		fwrite(buff, 1, sp, fout);
		fclose(fout);
	}
	void write_ch(char c) {
		if (sp == 50000) {
			fwrite(buff, 1, sp, fout);
			sp = 0;
		}
		buff[sp++] = c;
	}

	OuP &operator <<(int n) {
		if (n <= 9)
			write_ch(n + '0');
		else {
			*(this) << (n / 10);
			write_ch(n % 10 + '0');
		}
		return *this;
	}

	OuP &operator <<(ll n) {
		if (n <= 9)
			write_ch(n + '0');
		else {
			*(this) << (n / 10);
			write_ch(n % 10 + '0');
		}
		return *this;
	}

	OuP &operator <<(char c) {
		write_ch(c);
		return *this;
	}

	OuP &operator <<(const char *c) {
		while (*c) {
			write_ch(*c);
			c++;
		}
		return *this;
	}

};

InP fin("radixsort.in");
OuP fout("radixsort.out");

// ifstream fin("file.in");
// ofstream fout("file.out");

int n;
int A, B, C;
int a[10000005], b[10000005];
int fr[256];
int main() {

	fin >> n >> A >> B >> C;
	a[1] = B;
	for (int i = 2; i <= n; ++i)
		a[i] = (1LL * A * a[i - 1] + B) % C;

	for (int i = 0; i < 32; i += 8) {
		for (int j = 1; j <= n; ++j)
			fr[(a[j] >> i) & 255]++;
		for (int j = 1; j < 256; ++j)
			fr[j] += fr[j - 1];
		for (int j = n; j >= 1; --j)
			b[fr[(a[j] >> i) & 255]--] = a[j];

		for (int j = 1; j <= n; ++j)
			a[j] = b[j];
		for (int j = 0; j < 256; ++j)
			fr[j] = 0;

	}
	for (int i = 1; i <= n; i += 10)
		fout << a[i] << " ";


// fin.close();
// fout.close();
	return 0;
}