Cod sursa(job #1110436)

Utilizator cosmin79Carabet Cosmin Andrei cosmin79 Data 18 februarie 2014 03:34:34
Problema Radix Sort Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include <cstdio>
#include <cstring>
#define NMAX 10000005
#define ll long long
#define LMAX (1 << 4)
#define FBITS (LMAX - 1)
using namespace std;
int n, a, b, c, A[NMAX], B[NMAX], pos[LMAX];
 
int main()
{
    freopen("radixsort.in", "r", stdin);
    freopen("radixsort.out", "w", stdout);
    scanf("%d%d%d%d", &n, &a, &b, &c);
    A[1] = b;
    for (int i = 2; i <= n; i++)
        A[i] = ((ll)A[i - 1] * a + b) % c;
     
    for (int i = 1, curr = 0; i <= 8; i++, curr += 4)
    {
        memset(pos, 0, sizeof(pos));
        for (int j = 1; j <= n; j++)
            pos[(A[j] >> curr) & FBITS]++;
        for (int j = 1; j < LMAX; j++)
			pos[j] += pos[j - 1];
		for (int j = LMAX - 1; j >= 1; j--)
			pos[j] = pos[j - 1];
		pos[0] = 0;
            
        for (int j = 1; j <= n; j++)
            B[++pos[(A[j] >> curr) & FBITS]] = A[j];
        memcpy(A, B, sizeof(B));
    }
     
    for (int i = 1; i <= n; i += 10)
        printf("%d ", A[i]);
    printf("\n");
    return 0;
}