Cod sursa(job #2839206)

Utilizator rares89_Dumitriu Rares rares89_ Data 25 ianuarie 2022 15:27:53
Problema Curcubeu Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <fstream>

using namespace std;

ifstream fin("curcubeu.in");
ofstream fout("curcubeu.out");

int n, a, b, c, T[1000005], A[1000005], B[1000005], C[1000005], sol[1000005];

int next(int x) {
    int a = x;
    while(x != T[x]) {
        x = T[x];
    }
    int ans = x;
    x = a;
    while(ans != T[x]) {
        int aux = T[x];
        T[x] = ans;
        x = aux;
    }
    return ans;
}

int main() {
    fin >> n >> a >> b >> c;
    A[1] = a; B[1] = b; C[1] = c;
    for(int i = 2; i <= n; i++) {
        A[i] = (1LL * A[i - 1] * i) % n;
        B[i] = (1LL * B[i - 1] * i) % n;
        C[i] = (1LL * C[i - 1] * i) % n;
    }
    for(int i = 1; i <= n; i++) {
        T[i] = i;
    }
    for(int i = n - 1; i >= 0; i--) {
        for(int j = next(A[i]); j <= B[i]; j = T[j]) {
            sol[j] = C[i];
            T[j] = next(j + 1);
        }
    }
    for(int i = 1; i <= n - 1; i++) {
        fout << sol[i] << "\n";
    }
    return 0;
}