Cod sursa(job #2609271)

Utilizator AndreeaGherghescuAndreea Gherghescu AndreeaGherghescu Data 2 mai 2020 13:08:51
Problema Radix Sort Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.19 kb
#include <iostream>
#include <fstream>

using namespace std;

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

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

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

int main()
{
    int n,a,b,c,maxx;
    in>>n>>a>>b>>c;
    //v[0]=b;
    m[0][0]=b;
    maxx=b;
    for (int i=1;i<n;i++)
    {
        m[0][i]=(1LL*a*m[0][i-1]+b)%c;
        if (m[0][i]>maxx) maxx=m[0][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=(m[k%2][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=(m[k%2][i]>>p) & (B-1);
            m[1-k%2][poz[cif]++]=m[k%2][i];
        }
        /*for (int i=0;i<n;i++)
        {
            v[i]=aux[i];
        }*/
    }
    for(int i=0; i<n; i+=10)
        out<<m[NC%2][i]<<' ';
    return 0;
}