Cod sursa(job #1774280)

Utilizator danielNiculaeDaniel Niculae danielNiculae Data 8 octombrie 2016 19:24:54
Problema Radix Sort Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.49 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;
long long parts[5] = {1, 1<<8, 1<<16, 1<<24, 1LL * 1<<32};

unsigned int a[NMAX];
unsigned int ordered[NMAX];
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])/parts[pow - 1] << '\n';
//        fout.flush();
        cnt[(a[i]%parts[pow])/parts[pow - 1]]++;
    }

//    for(int i = 0 ; i <= 9 ; i++) {
//        fout << "a[i]: " << a[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])/parts[pow - 1]]--] = a[i];
    }

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

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

    fin >> N >> A >> B >> C;
    a[1] = B;
    for(int i = 2 ; i <= N ; i++) {
        a[i] = ((long long)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;
}