Cod sursa(job #2611766)

Utilizator georgecristian2002Raducanu George-Cristian georgecristian2002 Data 7 mai 2020 16:19:16
Problema Radix Sort Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
#include <fstream>

using namespace std;

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

const int dim=10000005;
int v[dim], aux[dim], nr[(1<<8)+100], poz[(1<<8)+100], n;

int main()
{
    long long int a,b,c;
    fin>>n>>a>>b>>c;
    v[0]=b;
    int maxi=b;
    for(int i=1; i<n; i++)
    {
        v[i]=(a*v[i-1]+b)%c;
        maxi=max(maxi, v[i]);
    }
    int B= (1<<8), NC=0;
    while (maxi != 0)
    {
        NC++;
        maxi /= B;
    }
    const int NB=8;
    int mod=B-1;
    for(int k=0; k<NC; k++)
    {
        int nb=k*NB;
        for(int j=0; j<B; j++)
            nr[j]=0;
        for(int i=0; i<n; i++)
        {
            int cif=(v[i]>>nb)&mod;
            nr[cif]++;
        }
        poz[0]=0;
        for(int j=0; j<B; j++)
            poz[j]=poz[j-1]+nr[j-1];
        for(int i=0; i<n; i++)
        {
            int cif=(v[i]>>nb)&mod;
            aux[poz[cif]++]=v[i];
        }
        for(int i=0; i<n; i++)
            v[i]=aux[i];
    }
    for(int i=0; i<n; i=i+10)
        fout<<v[i]<<' ';

    fout.close();
    return 0;
}