Cod sursa(job #3380617)

Utilizator carinaa_leetAmarie Carina carinaa_leet Data 2 decembrie 2026 08:05:41
Problema Curcubeu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <fstream>
#include <algorithm>

using namespace std;

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

const int MAXN = 1000005;

int A[MAXN], B[MAXN], C[MAXN];
int nxt[MAXN];
int ans[MAXN];

int find_next(int x)
{
    if (nxt[x] == x)
        return x;
    return nxt[x] = find_next(nxt[x]);
}

int main()
{
    int n;
    if (!(fin >> n))
        return 0;

    fin >> A[1] >> B[1] >> C[1];

    // Restul operatiilor se genereaza cu formula din enunt
    for (int i = 2; i < n; ++i)
    {
        A[i] = (1LL * A[i - 1] * i) % n; // 1LL ca sa nu dea overflow
        B[i] = (1LL * B[i - 1] * i) % n;
        C[i] = (1LL * C[i - 1] * i) % n;
    }

    for (int i = 1; i <= n; ++i)
    {
        nxt[i] = i;
    }

    for (int i = n - 1; i >= 1; --i)
    {
        int st = A[i];
        int dr = B[i];
        if (st > dr)
            swap(st, dr);

        if (st == 0) // poate aparea din % n
            st = 1;

        int poz = find_next(st);
        while (poz <= dr)
        {
            ans[poz] = C[i];
            nxt[poz] = find_next(poz + 1);
            poz = nxt[poz];
        }
    }

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

    return 0;
}