Pagini recente » Cod sursa (job #2528009) | Cod sursa (job #888317) | Cod sursa (job #686614) | Cod sursa (job #1958148) | Cod sursa (job #2509061)
#include <bits/stdc++.h>
#define DMAX 10000000 + 1
using namespace std;
int numbers[DMAX];
int n, a, b, c;
void citire();
void solve();
void afisare();
inline int getByte(const int &, const int &);
void radixSort(int *, int *, const int &);
int main()
{
citire();
solve();
afisare();
return 0;
}
void afisare()
{
ofstream fout("radixsort.out");
for (int i = 0; i < n; i += 10)
fout << numbers[i] << ' ';
fout << '\n';
fout.close();
}
void radixSort(int *from, int *to, const int &byte)
{
int buckets[1 << 8];
memset(buckets, 0, sizeof(buckets));
for (int i = 0; i < n; ++i)
++buckets[getByte(from[i], byte)];
for (int i = 1; i < (1 << 8); ++i)
buckets[i] += buckets[i - 1];
for (int i = n - 1; i >= 0; --i)
{
--buckets[getByte(from[i], byte)];
to[buckets[getByte(from[i], byte)]] = from[i];
}
}
void solve()
{
// sortez dupa bytes
int no_bytes = sizeof(numbers[0]);
int copy[DMAX];
for (int i = 0; i < no_bytes; ++i)
if (i % 2 == 0)
radixSort(numbers, copy, i);
else
radixSort(copy, numbers, i);
}
inline int getByte(const int &nr, const int &byte)
{
return (nr >> (byte * 8)) & ((1 << 8) - 1);
}
void citire()
{
ifstream fin("radixsort.in");
fin >> n >> a >> b >> c;
numbers[0] = b % c;
for (int i = 1; i < n; i++)
numbers[i] = (1LL * a * numbers[i - 1] % c + b) % c;
fin.close();
}