Cod sursa(job #2608650)

Utilizator MatteoalexandruMatteo Verzotti Matteoalexandru Data 1 mai 2020 17:12:05
Problema Radix Sort Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.97 kb
/*
                `-/oo+/-   ``
              .oyhhhhhhyo.`od
             +hhhhyyoooos. h/
            +hhyso++oosy- /s
           .yoooossyyo:``-y`
            ..----.` ``.-/+:.`
                   `````..-::/.
                  `..```.-::///`
                 `-.....--::::/:
                `.......--::////:
               `...`....---:::://:
             `......``..--:::::///:`
            `---.......--:::::////+/`
            ----------::::::/::///++:
            ----:---:::::///////////:`
            .----::::::////////////:-`
            `----::::::::::/::::::::-
             `.-----:::::::::::::::-
               ...----:::::::::/:-`
                 `.---::/+osss+:`
                   ``.:://///-.
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cmath>

using namespace std;

const int INF = 2e9;
const int N = 1e7;
const int B = 10;
const int NC = 8;

int v[5 + N];
int nr[5 + N], poz[5 + N], aux[5 + N];

int main() {
    freopen("radixsort.in", "r", stdin);
    freopen("radixsort.out", "w", stdout);
    int n, a, b, c, p;

    scanf("%d%d%d%d", &n, &a, &b, &c);
    //scanf("%d", &n);
    //for(int i = 1; i <= n; i++) scanf("%d", &v[i]);

    v[1] = b;
    for(int i = 2; i <= n; i++)
        v[i] = (1LL * a * v[i - 1] + b) % c;
    p = 1;
    for(int k = 1; k <= NC; k++) {
        for(int j = 0; j < B; j++)
            nr[j] = 0;

        for(int i = 1; i <= n; i++)
            nr[v[i] / p % 10]++;
        poz[0] = 0;

        for(int i = 1; i < B; i++)
            poz[i] = poz[i - 1] + nr[i - 1];

        for(int i = 1; i <= n; i++)
            aux[++poz[v[i] / p % 10]] = v[i];

        for(int i = 1; i <= n; i++)
            v[i] = aux[i];
        p *= 10;
    }

    for(int i = 1; i <= n; i += 10) {
        printf("%d ", v[i]);
    }
    return 0;
}