Cod sursa(job #2609261)

Utilizator AndreeaGherghescuAndreea Gherghescu AndreeaGherghescu Data 2 mai 2020 13:01:56
Problema Radix Sort Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int NB=16;
const int B=1<<16;
const int N=10000000;

int poz[B],nr[B];
int v[N],aux[N];
int a[2][N];

int main()
{
    int n,a,b,c,maxx;
    in>>n>>a>>b>>c;
    v[0]=b;
    maxx=v[0];
    for (int i=1;i<n;i++)
    {
        v[i]=(1LL*a*v[i-1]+b)%c;
        if (v[i]>maxx) maxx=v[i];
    }
    int NC=0;
    while (maxx)
    {
        NC++;
        maxx/=B;
    }
    int p=0;
    for (int k=0;k<NC;k++)
    {
        p=k*NB;
        for (int j=0;j<B;j++)
            nr[j]=0;
        for (int i=0;i<n;i++)
        {
            int cif=(v[i]>>p) & (B-1);
            nr[cif]++;
        }
        poz[0]=0;
        for (int j=1;j<B;j++)
        {
            poz[j]=poz[j-1]+nr[j-1];
        }
        for (int i=0;i<n;i++)
        {
            int cif=(v[i]>>p) & (B-1);
            aux[poz[cif]++]=v[i];
        }
        for (int i=0;i<n;i++)
        {
            v[i]=aux[i];
        }
    }
    for(int i=0; i<n; i+=10)
        out<<v[i]<<' ';
    return 0;
}