Cod sursa(job #2577171)

Utilizator AlexVulpoiuAlexandru Vulpoiu AlexVulpoiu Data 8 martie 2020 16:46:27
Problema Radix Sort Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 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[256];
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(256, floor(double(log2(max1) / 8)));
        q = 1;
        while(q <= p)
        {
            for(i = 0; i < n; i++)
                buck[(v[i] / q) % 256].push_back(v[i]);
            k = 0;
            for(i = 0; i < 256; i++)
            {
                for(j = 0; j < buck[i].size(); j++)
                    v[k++] = buck[i][j];
                buck[i].clear();
            }
            q *= 256;
        }
    }
    for(i = 0; i < n; i += 10)
        g << v[i] << " ";
    g << "\n";
    g.close();
    return 0;
}