Cod sursa(job #2582539)

Utilizator DordeDorde Matei Dorde Data 16 martie 2020 21:04:39
Problema Radix Sort Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <fstream>
#include <algorithm>

using namespace std;
int const N = 1e7 + 1 , bk = 16;
int index [N] , v [N] , fq [1 + (1 << 8)] , v2 [N];
ifstream f ("radix.in");
ofstream g ("radix.out");
int main()
{
    int n , a , b , c;
    f >> n >> a >> b >> c;
    v [1] = b % c;
    for(int i = 2 ; i <= n ; ++ i)
        v [i] = (1LL * v [i - 1] * a % c + b) % c;
    for(int i = 1 ; i < 5 ; ++ i){
        long long nrb;
        fill (fq , fq + (1 << 8) , 0);
        if (i == 4)
            nrb = (1LL << 31) - 1 - (1 << 25) + 1 ;
        else
            nrb = 1LL * (1LL << (8 * i)) - 1 - ((1 << (8 * (i - 1))) - 1);
        for (int j = 1 ; j <= n ; ++ j)
            ++ fq [v [j] & nrb];
        index [0] = 1;
        for (int r = 1 ; r < (1 << 8) ; ++ r)
            index [r] = index [r - 1] + fq [r - 1];
        for(int j = 1 ; j <= n ; ++ j){
            v2 [index [v [j] & nrb]] = v [j];
            ++ index [v [j] & nrb];
        }
        swap (v , v2);
    }
    for(int i = 1 ; i <= n ; i += 10)
        g << v2 [i] << ' ';
    return 0;
}