Cod sursa(job #3178432)

Utilizator verde.cristian2005Verde Flaviu-Cristian verde.cristian2005 Data 1 decembrie 2023 17:57:47
Problema Radix Sort Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.16 kb
#include <bits/stdc++.h>
using namespace std;

const int baza = (1 << 16);

int v[10000001];
int aux[10000001];
int fv[baza];

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;
    }
};

#ifndef HOME
    ifstream in("radixsort.in");
    OutParser out("radixsort.out");
    #define cin in
    #define cout out
#endif

int main()
{
#ifdef HOME
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, a, b, c;
    cin >> n >> a >> b >> c;
    v[1] = b;
    for(int i = 2; i <= n; i++)
        v[i] = (1LL * a * v[i - 1] + b) % c;
    for(int i = 1; i <= n; i++)
        fv[v[i] & (baza - 1)]++;
    for(int i = 1; i <= baza - 1; i++)
        fv[i] += fv[i - 1];
    for(int i = n; i >= 1; i--)
        aux[fv[v[i] & (baza - 1)]--] = v[i];
    for(int i = 0; i <= baza - 1; i++)
        fv[i] = 0;
    ///a doua oara
    for(int i = 1; i <= n; i++)
        fv[(aux[i] >> 16) & (baza - 1)]++;
    for(int i = 1; i <= baza - 1; i++)
        fv[i] += fv[i - 1];
    for(int i = n; i >= 1; i--)
        v[fv[(aux[i] >> 16) & (baza - 1)]--] = aux[i];
    for(int i = 1; i <= n; i += 10)
        cout << v[i] << " ";
    return 0;
}