Cod sursa(job #2330829)

Utilizator DavidLDavid Lauran DavidL Data 28 ianuarie 2019 21:00:55
Problema Radix Sort Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ifstream fi("radixsort.in");
ofstream fo("radixsort.out");

const int BASE = 1e5;

int n, a, b, c;
int v[10000005];
int v2[10000005];
int cnt[100005];

void radixSort()
{
    ll p = 1;
    for (int rep = 1; rep <= 2; rep++)
    {
        memset(cnt, 0, sizeof(cnt));

        p *= BASE;
        for (int i = 1; i <= n; i++)
            cnt[(v[i] / (p / BASE)) % BASE]++;

        for (int i = 0; i < BASE; i++)
            cnt[i] += cnt[i - 1];

        memset(v2, 0, sizeof(v2));
        for (int i = 1; i <= n; i++)
        {
            int poz = cnt[(v[i] / (p / BASE)) % BASE - 1] + 1;
            v2[poz] = v[i];
            cnt[(v[i] / (p / BASE)) % BASE - 1]++;
        }
        memcpy(v, v2, sizeof(v2));
    }
}

int main()
{
    fi >> n >> a >> b >> c;
    v[1] = b;
    for (int i = 2; i <= n; i++)
        v[i] = (a * v[i - 1] + b) % c;

    radixSort();

    for (int i = 1; i <= n; i += 10)
        fo << v[i] << " ";

    return 0;
}