Cod sursa(job #1475581)

Utilizator salam1Florin Salam salam1 Data 24 august 2015 11:07:29
Problema Radix Sort Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <cstdio>
#include <cstring>
const int NMAX = 10000505;
const int FBYTE = (1 << 8) - 1;
const int LMAX = (1 << 8) + 1;
int n, a, b, c, A[2][NMAX];

int getByte(int idx, int val) {
  return (val >> (idx * 8)) & FBYTE;
}

void read() {
  scanf("%d%d%d%d", &n, &a, &b, &c);
  A[0][1] = b;
  for (int i = 2; i <= n; i++) {
    A[0][i] = (1LL * a * A[0][i - 1] + b) % c;
  }
}

void radixSort(int left, int right, int byte) {
  if (byte == 4) {
    return ;
  }
  int cnt[LMAX] = {0};
  
  for (int i = left; i <= right; i++) {
    cnt[getByte(byte, A[byte & 1][i])]++;
  }
  for (int i = 1; i <= FBYTE; i++) {
    cnt[i] += cnt[i - 1];
  }
  cnt[LMAX - 1] = cnt[FBYTE];
  
  for (int i = left; i <= right; i++) {
    int val = getByte(byte, A[byte & 1][i]);
    A[1 - (byte & 1)][left + cnt[val] - 1] = A[byte & 1][i];
    cnt[val]--;
  }
  
  radixSort(left, right, byte + 1);
}

int main() {
  freopen("radixsort.in", "r", stdin);
  freopen("radixsort.out", "w", stdout);
  
  read();
  radixSort(1, n, 0);
  
  for (int i = 1; i <= n; i += 10)
    printf("%d ", A[1][i]);
  printf("\n");
  return 0;
}