Cod sursa(job #2608709)

Utilizator MatteoalexandruMatteo Verzotti Matteoalexandru Data 1 mai 2020 18:05:44
Problema Radix Sort Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.89 kb
/*
                `-/oo+/-   ``
              .oyhhhhhhyo.`od
             +hhhhyyoooos. h/
            +hhyso++oosy- /s
           .yoooossyyo:``-y`
            ..----.` ``.-/+:.`
                   `````..-::/.
                  `..```.-::///`
                 `-.....--::::/:
                `.......--::////:
               `...`....---:::://:
             `......``..--:::::///:`
            `---.......--:::::////+/`
            ----------::::::/::///++:
            ----:---:::::///////////:`
            .----::::::////////////:-`
            `----::::::::::/::::::::-
             `.-----:::::::::::::::-
               ...----:::::::::/:-`
                 `.---::/+osss+:`
                   ``.:://///-.
*/
#include <fstream>

using namespace std;

ifstream in ("radixsort.in");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 << (long long vu64) {
        if (vu64 <= 9) {
            write_ch(vu64 + '0');
        } else {
            (*this) << (vu64 / 10);
            write_ch(vu64 % 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;
    }
};
OutParser out ("radixsort.out");

const int N = 1e7;
const int NC = 2;
const int B = 1 << 16;

int v[2][5 + N];
int nr[5 + B], poz[5 + B];

int main() {
    int n, a, b, c;

    in >> n >> a >> b >> c;

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

    for(int k = 1; k <= NC; k++) {
        for(int j = 0; j < B; j++)
            nr[j] = 0;

        for(int i = 1; i <= n; i++)
            nr[(v[k & 1][i] >> ((k - 1) * 16)) & (B - 1)]++;
        poz[0] = 0;

        for(int i = 1; i < B; i++)
            poz[i] = poz[i - 1] + nr[i - 1];

        for(int i = 1; i <= n; i++)
            v[(k - 1) & 1][++poz[(v[k & 1][i] >> ((k - 1) * 16)) & (B - 1)]] = v[k & 1][i];
    }

    for(int i = 1; i <= n; i += 10) {
        out << v[(NC - 1) & 1][i] << " ";
    }
    return 0;
}