Cod sursa(job #3378147)

Utilizator prares06Papacioc Rares-Ioan prares06 Data 25 noiembrie 2026 12:58:56
Problema Curcubeu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.99 kb

#include <bits/stdc++.h>
using namespace std;

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

#define int long long

int n;
vector<int> a, b, c, ans, P;

int find(int x) {
    if(P[x] == x)
        return x;

    return P[x] = find(P[x]);
}

signed main() {
    fin >> n;

    a.resize(n + 1);
    b.resize(n + 1);
    c.resize(n + 1);
    ans.resize(n + 1, 0);
    P.resize(n + 2);

    fin >> a[1] >> b[1] >> c[1];

    for(int i = 2; i < n; ++i) {
        a[i] = a[i - 1] * i % n;
        b[i] = b[i - 1] * i % n;
        c[i] = c[i - 1] * i % n;
    }

    for(int i = 1; i <= n; ++i)
        P[i] = i;

    for(int i = n - 1; i >= 1; --i) {
        int l = min(a[i], b[i]);
        int r = max(a[i], b[i]);

        int pos = find(l);

        while(pos <= r) {
            ans[pos] = c[i];
            P[pos] = find(pos + 1);
            pos = find(pos);
        }
    }

    for(int i = 1; i < n; ++i)
        fout << ans[i] << '\n';
}