Cod sursa(job #1774311)

Utilizator danielNiculaeDaniel Niculae danielNiculae Data 8 octombrie 2016 19:51:48
Problema Radix Sort Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.72 kb
#include "fstream"

//using namespace std;

std::ifstream fin("radixsort.in");
std::ofstream fout("radixsort.out");

const int NMAX = 10000015;
const int DIGITS = (1<<8) + 5;
int shiftDigits[4] = {0, 8, 16, 24};
unsigned int parts[5] = {0, (1<<8) - 1, (1<<16) - 1, (1<<24) - 1, (unsigned int)(1<<31) - 1 + (1<<31)};

unsigned int a[NMAX];
unsigned int ordered[NMAX];
unsigned int cnt[DIGITS];


void radix(int pow, int N) {

    for(int i = 0 ; i < DIGITS ; i++) {
        cnt[i] = 0;
    }

    for(int i = 1 ; i <= N ; i++) {
//        fout << ((a[i]&(parts[pow] - 1)) >> shiftDigits[pow - 1]) << '\n';
//        fout.flush();
        cnt[(a[i]&parts[pow]) >> shiftDigits[pow - 1]]++;
    }

//    for(int i = 0 ; i <= N ; i++) {
//        fout << "a[i]: " << a[i] << " ";
//    }
//    fout << "\n";
//
//    for(int i = 0 ; i <= 100 ; i++) {
//        fout << "cnt[i]: " << cnt[i] << " ";
//    }
//    fout << "\n";
//    fout.flush();

    for(int i = 1 ; i < DIGITS; i++) {
        cnt[i] += cnt[i - 1];
    }


    for(int i = N ; i ; i--) {
//        if(cnt[(a[i]%parts[pow])/parts[pow - 1]] < 0) {
//            break;
//        }
        ordered[cnt[(a[i]&parts[pow]) >> shiftDigits[pow - 1]]--] = a[i];
    }

    for(int i = 1 ; i <= N ; i++) {
        a[i] = ordered[i];
    }
}

int main() {
    int N;
    int A, B, C;

    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 pow = 1 ; pow <= 4 ; pow++) {
//        for(int i = 1 ; i <= N ; i++) {
//            fout << a[i] << " ";
//        }
//        fout << "\n";
        radix(pow, N);
    }


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

    return 0;
}