Cod sursa(job #1828918)

Utilizator DoubleNyNinicu Cristian DoubleNy Data 14 decembrie 2016 02:47:09
Problema Radix Sort Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.64 kb
#include <bits/stdc++.h>
using namespace std;
#define ios ios_base::sync_with_stdio(false);cin.tie(0);
#define setnow clock_t tStart=clock();
#define time (double)(clock() - tStart)/CLOCKS_PER_SEC;
typedef long long ll;
typedef long long int lli;
typedef pair < int, int> dbl;
const int maxInt = 1e9*2;
const lli maxLong = 1e18*2;
int nrMAX = 0;

void cntSort(int v[], int n, int exp){

        int ans[10000001];
        int out[10000001];
       // memset(ans, 0, sizeof(ans));
       // memset(out, 0, sizeof(out));
        for(int i = 0; i < n; i++)
                ans[(v[i] / exp) % 10]++;
        for(int i = 1; i < nrMAX; i++)
                ans[i] = ans [i]+ ans[i - 1];
        for(int i = n - 1; i > -1; i--){
                out[ ans[(v[i] / exp) % 10 ] - 1 ] = v[i];
                ans[(v[i] / exp) % 10]--;
        }
        for(int i = 0; i < n; i++)
                v[i] = out[i];
}

int main(){
           // setnow;
           ifstream cin("input.in");
           ofstream cout("output.out");
           int n, a, b, c;
           int v[10000001];
           cin >> n >> a >> b >> c;
           v[0] = b;
           //cout << n << endl;
           for(int i = 1; i < n; ++i){
                v[i] = (a * v[i-1] + b) % c;
                nrMAX = max(nrMAX, v[i]);
           }
           for(int exp = 1; nrMAX/exp > 0; exp *= 10){
                    cntSort(v, n, exp);
           }
       // sort(v, v + n);
           for(int i = 0; i < n; i+=10)
                cout << v[i] << ' ';
           //for(int i = 1; i<= n; i++)
           //    cout << v[i] << ' ';
           //
           //cout << fixed << time;
	return 0;
}