Cod sursa(job #1475587)

Utilizator salam1Florin Salam salam1 Data 24 august 2015 11:10:34
Problema Radix Sort Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include <cstdio>
#include <cstring>
const int NMAX = 10000505;
const int FBYTE = (1 << 8) - 1;
const int LMAX = (1 << 8);
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], st[LMAX];
  memset(cnt, 0, sizeof(cnt));
   
  for (int i = left; i <= right; i++) {
    cnt[getByte(byte, A[byte & 1][i])]++;
  }
  st[0] = left - 1;
  for (int i = 1; i <= FBYTE; i++) {
    st[i] = st[i - 1] + cnt[i - 1];
  }
   
  for (int i = left; i <= right; i++) {
    int val = getByte(byte, A[byte & 1][i]);
    A[1 - (byte & 1)][++st[val]] = A[byte & 1][i];
  }
   
  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[0][i]);
  printf("\n");
  return 0;
}