Cod sursa(job #2677830)

Utilizator mihaipriboimihailucapriboi mihaipriboi Data 27 noiembrie 2020 16:48:03
Problema Radix Sort Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
// Mihai Priboi

#include <bits/stdc++.h>

#define MAXN 10000000
#define MAX_BITS 32
#define BITS_PER_STEP 8

const int BASE = ( 1 << BITS_PER_STEP );
const int MASK = BASE - 1;

int x[MAXN], y[MAXN];
int freq[BASE], ind[BASE];

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

void sort( int v[], int aux[], int n, int bits ) {
  if( bits == MAX_BITS )
    return;

  memset(freq, 0, sizeof( freq ));

  int i;
  for( i = 0; i < n; i++ )
    freq[v[i] >> bits & MASK]++;

  ind[0] = 0;
  for( i = 1; i < BASE; i++ )
    ind[i] = ind[i - 1] + freq[i - 1];

  for( i = 0; i < n; i++ )
    aux[ind[v[i] >> bits & MASK]++] = v[i];

  sort(aux, v, n, bits + BITS_PER_STEP);
}

int main() {
  int n, a, b, c, i;
  in >> n >> a >> b >> c;
  x[0] = b % c;
  for( i = 1; i < n; i++ )
    x[i] = (1LL * a * x[i - 1] % c + b) % c;

  sort(x, y, n, 0);
  for( i = 0; i < n; i++ )
    if( i % 10 == 0 )
      out << x[i] << " ";
  return 0;
}