Cod sursa(job #2633289)

Utilizator Alex_tz307Lorintz Alexandru Alex_tz307 Data 6 iulie 2020 23:40:33
Problema Radix Sort Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.6 kb
#include <fstream>
#include <vector>

using namespace std;

class OutParser {
private:
    FILE *fout;
    char *buff;
    int sp;
    void write_ch(char ch) {
        if (sp == 50000) {
            fwrite(buff, 1, 50000, fout);
            sp = 0;
            buff[sp++] = ch;
        } else {
            buff[sp++] = ch;
        }
    }
public:
    OutParser(const char* name) {
        fout = fopen(name, "w");
        buff = new char[50000]();
        sp = 0;
    }
    ~OutParser() {
        fwrite(buff, 1, sp, fout);
        fclose(fout);
    }
    OutParser& operator << (int vu32) {
        if (vu32 <= 9) {
            write_ch(vu32 + '0');
        } else {
            (*this) << (vu32 / 10);
            write_ch(vu32 % 10 + '0');
        }
        return *this;
    }
    OutParser& operator << (char ch) {
        write_ch(ch);
        return *this;
    }
    OutParser& operator << (const char *ch) {
        while (*ch) {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
};

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

int n, a, b, c, v[10000000];

void Sort (int bits) {
	vector < int > h[256];
	for (int i = 0; i < n; i ++)
		h[(v[i] >> bits) & 255].emplace_back (v[i]);
	for (int i = 0, n = 0; i < 256; i ++)
		for (auto it : h[i])
			v[n ++] = it;
}

int main () {
	fin >> n >> a >> b >> c;
	v[0] = b;
	for (int i = 1; i < n; i ++)
		v[i] = ((long long)a * v[i - 1] + b) % c;
  Sort (0); Sort (8); Sort (16), Sort(24);
	for (int i = 0; i < n; i += 10)
		fout << v[i] << ' ';
	return 0;
}