Cod sursa(job #2268629)

Utilizator SqueekDanielTodasca Daniel SqueekDaniel Data 25 octombrie 2018 00:49:30
Problema Radix Sort Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.03 kb
#include <bits/stdc++.h>

#define MaxN 10000005

class parser{
    public:
        parser() {}
        parser(const char *file_name){
            input_file.open(file_name,std::ios::in | std::ios::binary);
            input_file.sync_with_stdio(false);
            index=0;
            input_file.read(buffer,SIZE);}
        inline parser &operator >>(int &n){
            for (;buffer[index]<'0' or buffer[index]>'9';inc());
            n=0;
            for (;'0'<=buffer[index] and buffer[index]<='9';inc())
                n=10*n+buffer[index]-'0';
            return *this;}
            ~parser(){
            input_file.close();}
    private:
        std::fstream input_file;
        static const int SIZE=0x400000;
        char buffer[SIZE];
        int index=0;
        inline void inc(){
            if(++index==SIZE)
                index=0,input_file.read(buffer,SIZE);}
};

class writer{
    public:
        writer() {};
        writer(const char *file_name){
            output_file.open(file_name,std::ios::out | std::ios::binary);
            output_file.sync_with_stdio(false);
            index=0;}
        inline writer &operator <<(int target){
            aux=0;
            n=target;
            if (!n)
                nr[aux++]='0';
            for (;n;n/=10)
                nr[aux++]=n%10+'0';
            for(;aux;inc())
                buffer[index]=nr[--aux];
            return *this;}
        inline writer &operator <<(const char *target){
            aux=0;
            while (target[aux])
                buffer[index]=target[aux++],inc();
            return *this;}
        ~writer(){
            output_file.write(buffer,index);output_file.close();}
    private:
        std::fstream output_file;
        static const int SIZE=0x200000;
        int index=0,aux,n;
        char buffer[SIZE],nr[24];
        inline void inc(){
            if(++index==SIZE)
                index=0,output_file.write(buffer,SIZE);}
};

parser InFile("radixsort.in");
writer OutFile("radixsort.out");

int N, A, B, C;
int V[MaxN];

inline int GetBits(int x, int nbytes) {
    return (x >> (8*nbytes)) & 255;
}

int Aux[MaxN];
int DigitCount[256];

void CountSort(int byte) {
    for (int i=1; i<=255; ++i)
        DigitCount[i] = 0;

    for (int i=1; i<=N; ++i)
        DigitCount[GetBits(V[i], byte)] ++;

    for (int i=1; i<=255; ++i)
        DigitCount[i] += DigitCount[i-1];

    for (int i=N, Digit; i>0; --i) {
        Digit = GetBits(V[i], byte);
        Aux[DigitCount[Digit]] = V[i];
        --DigitCount[Digit];
    }

    for (int i=1; i<=N; ++i)
        V[i] = Aux[i];
}

void Radix() {
    for (int byte=0; byte<4; ++byte)
        CountSort(byte);
}

void Citire() {
    InFile >> N >> A >> B >> C;

    V[1] = B;
    for (int i=2; i<=N; ++i)
        V[i] = (1LL * A * V[i-1] + B) % C;
}

void Rezolvare() {
    Radix();

    for (int i=1; i<=N; i+=10)
        OutFile << V[i] << " " ;
}

int main()
{
    Citire();
    Rezolvare();

    return 0;
}