Cod sursa(job #2619109)

Utilizator AlexVulpoiuAlexandru Vulpoiu AlexVulpoiu Data 26 mai 2020 23:25:50
Problema Radix Sort Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include <fstream>
#include <vector>
#include <cmath>

using namespace std;

ifstream f("radixsort.in");
ofstream g("radixsort.out");

int n, i, j, k, max1;
long long int a, b, c, x, p, q;
vector<int> v, buck[65536];

int main()
{
    f >> n >> a >> b >> c;
    v.push_back(b);
    max1 = b;
    for(i = 1; i < n; i++)
    {
        x = (a * v[i - 1] + b) % c;
        v.push_back(x);
        if(v[i] < 0)
            v[i] += c;
        max1 = max(max1, v[i]);
    }
    f.close();

    if(max1 > 0)
    {
        p = pow(65536, floor(double(log2(max1) / 16)));
        q = 1;
        while(q <= p)
        {
            for(i = 0; i < n; i++)
                buck[(v[i] / q) & 65535].push_back(v[i]);
            k = 0;
            for(i = 0; i < 65536; i++)
            {
                for(j = 0; j < buck[i].size(); j++)
                    v[k++] = buck[i][j];
                buck[i].clear();
            }
            q *= 65536;
        }
    }

    for(i = 0; i < n; i += 10)
        g << v[i] << " ";
    g << "\n";

    g.close();

    return 0;
}