Pagini recente » Cod sursa (job #3282324) | Cod sursa (job #236359) | Cod sursa (job #559009) | Cod sursa (job #611524) | Cod sursa (job #2618850)
#include <cstdio>
#include <vector>
#include <fstream>
#include <cstring>
using namespace std;
const int NMAX = 10000505;
const int LMAX = (1 << 8);
int N, A, B, C;
int nums[2][NMAX];
int sliceStart[LMAX], cntByte[LMAX];
inline int getByte(int& value, int from) {
return (value >> from) & (LMAX - 1);
}
int main() {
ifstream in("radixsort.in");
ofstream out("radixsort.out");
in >> N >> A >> B >> C;
nums[0][1] = B;
for (int idx = 2; idx <= N; idx++) {
nums[0][idx] = (1LL * nums[0][idx - 1] * A + B) % C;
}
for (int byteIdx = 0; byteIdx < 4; byteIdx++) {
memset(cntByte, 0, sizeof(cntByte));
for (int idx = 1; idx <= N; idx++) {
int byteValue = getByte(nums[byteIdx & 1][idx], byteIdx * 8);
cntByte[byteValue]++;
}
sliceStart[0] = 1;
for (int value = 1; value < LMAX; value++) {
sliceStart[value] = sliceStart[value - 1] + cntByte[value - 1];
}
for (int idx = 1; idx <= N; idx++) {
int byteValue = getByte(nums[byteIdx & 1][idx], byteIdx * 8);
nums[(byteIdx + 1) & 1][sliceStart[byteValue]++] = nums[byteIdx & 1][idx];
}
}
for (int idx = 1; idx <= N; idx += 10) {
out << nums[0][idx] << " ";
}
out << "\n";
return 0;
}