Pagini recente » Cod sursa (job #722830) | Cod sursa (job #729927) | Cod sursa (job #722780) | Cod sursa (job #2019292) | Cod sursa (job #2785800)
#include <fstream>
#include <vector>
#include <cmath>
using namespace std;
ifstream fin("radixsort.in");
ofstream fout("radixsort.out");
int n, a, b, c;
int arr[10000013];
int max(int arr[], int n)
{
int m = arr[0];
for (int i = 0; i < n; ++i)
{
if (m < arr[i])
{
m = arr[i];
}
}
return m;
}
void countingSort(int exp, int arr[], int n)
{
int count[10];
fill(count, count + 10, 0);
int pos[10];
int temp[n + 1];
for (int i = 1; i <= n; ++i)
{
++count[(arr[i] / exp) % 10];
}
pos[0] = 1;
for (int i = 1; i < 10; ++i)
{
pos[i] = pos[i - 1] + count[i - 1];
}
for (int i = 1; i <= n; ++i)
{
int digit = (arr[i] / exp) % 10;
int idx = pos[digit];
temp[idx] = arr[i];
++pos[digit];
}
for (int i = 1; i <= n; ++i)
{
arr[i] = temp[i];
}
}
void radixSort(int arr[], int n)
{
int m = max(arr, n);
for (int exp = 1; m / exp > 0; exp *= 10)
{
countingSort(exp, arr, n);
}
}
int main()
{
fin >> n >> a >> b >> c;
arr[1] = b;
for (int i = 2; i <= n; ++i)
{
arr[i] = (1L * a * arr[i - 1] + b) % c;
}
radixSort(arr, n);
for (int i = 1; i <= n; i += 10)
{
fout << arr[i] << " ";
}
return 0;
}