Cod sursa(job #2139408)

Utilizator inquisitorAnders inquisitor Data 22 februarie 2018 15:25:03
Problema Radix Sort Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.18 kb
#include <cstdio>
#include <fstream>

using i32 = int;
using u32 = unsigned int;
using u64 = unsigned long long;

u32 N, A, B, C, v[0x989680], w[0x989680], mod, inv, r2;

__attribute__((always_inline)) void CountingSort(u32 A[], u32 B[], u32 digit)
{
    u32 frequence[0x100]{}, index[0x100]; index[0x0] = ~0x0;

    for(u32 i = N; i; ++frequence[A[--i] >> digit & 0xFF]);

    for(u32 i = 0x0; i != 0x100; index[++i] = index[~-i] + frequence[~-i]);

    for(u32 i = 0x0; i != N; ++i) B[++index[A[i] >> digit & 0xFF]] = A[i];
}

__attribute__((always_inline)) void read(u32 &number)
{
    static char inBuffer[0x40];

    static u32 p = 0x0; number = 0x0;

    fread(inBuffer, 0x1, 0x40, stdin);

    while(inBuffer[p] < 0x30 | inBuffer[p] > 0x39)
    {
        ++p;
    }

    while(inBuffer[p] > 0x2F & inBuffer[p] < 0x3A)
    {
        number = number * 0xA + inBuffer[p] - 0x30;

        ++p;
    }
}

char outBuffer[0xA7D8C0]; i32 p = 0xA7D8C0;

__attribute__((always_inline)) void write(u32 x)
{
    outBuffer[--p] = 0x20;

    do
    {
        outBuffer[--p] = x % 0xA + 0x30;

        x /= 0xA;
    }
    while(x);
}

struct Mod64
{
    __attribute__((always_inline)) Mod64(u32 n)
    {
        n_ = reduce(u64(n) * r2);
    }

    __attribute__((always_inline)) static u32 reduce(u64 x)
    {
        u32 y = u32(x >> 0x20) - u32((u64(u32(x) * inv) * mod) >> 0x20);

        return i32(y) < 0x0 ? y + mod : y;
    }

    __attribute__((always_inline)) Mod64& operator += (Mod64 rhs)
    {
        n_ += rhs.n_ - mod;

        if(i32(n_) < 0) n_ += mod;

        return *this;
    }

    __attribute__((always_inline)) Mod64& operator *= (Mod64 rhs)
    {
        n_ = reduce(u64(n_) * rhs.n_);

        return *this;
    }

    __attribute__((always_inline)) u32 get() const
    {
        return reduce(n_);
    }

    u32 n_;
};

i32 main()
{
    freopen("radixsort.in", "r", stdin);

    read(N); read(A); read(B); read(C);

    v[0x0] = B;

    if(C & 0x1)
    {
        mod = inv = C;

        for(i32 i = 0x0; i != 0x5; ++i)

            inv *= 0x2 - inv * C;

        r2 = -u64(C) % C;

        Mod64 rb = Mod64(B), ra = Mod64(A);

        Mod64 el = rb;

        for(i32 i = 0x1; i != N; ++i)
        {
            el *= ra;
            el += rb;
            v[i] = el.get();
        }
    }
    else if(C != 0x7CE67EF6)
    {
        A %= C, B %= C;

        for(i32 i = 1; i != N; ++i)
        {
            const u64 t = A * v[~-i] + B;

            const u32 thi = t >> 0x20, tlo = t; i32 aux;

            asm("divl\t%4" : "=a"(aux), "=d"(v[i]) : "0"(tlo), "1"(thi), "r"(C));
        }
    }
    else
    {
        for(i32 i = 0x1; i != N; ++i)
        {
            u64 j = u64(v[~-i]) * A + B;

            v[i] = j < 0x7CE67EF6 ? j : j - j / 0x7CE67EF6 * 0x7CE67EF6;
        }
    }

    CountingSort(v, w, 0x0);
    CountingSort(w, v, 0x8);
    CountingSort(v, w, 0x10);
    CountingSort(w, v, 0x18);

    for(int i = N - 1 - (N - 1) % 10; i >= 0; i -= 10)

        write(v[i]);

    std :: ofstream("radixsort.out").write(outBuffer + p, 0xA7D8BF - p);

    return 0x0;
}