Cod sursa(job #3379995)

Utilizator teogrecigrecioiu teo teogreci Data 1 decembrie 2026 18:39:17
Problema Curcubeu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <fstream>
using namespace std;

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

const int NMAX = 1000005;

int n;
int a[NMAX], b[NMAX], c[NMAX];
int culoare[NMAX];
int tata[NMAX];

int gaseste(int x)
{
    int radacina = x;
    while (tata[radacina] != radacina)
        radacina = tata[radacina];

    while (tata[x] != radacina)
    {
        int urmator = tata[x];
        tata[x] = radacina;
        x = urmator;
    }
    return radacina;
}

int main()
{
    fin >> n >> a[1] >> b[1] >> c[1];

    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++)
        tata[i] = i;

    for (int i = n - 1; i >= 1; i--)
    {
        int st = a[i], dr = b[i];
        if (st > dr)
        {
            int aux = st;
            st = dr;
            dr = aux;
        }

        int poz = gaseste(st);
        while (poz <= dr)
        {
            culoare[poz] = c[i];
            tata[poz] = poz + 1;
            poz = gaseste(poz + 1);
        }
    }

    for (int i = 1; i < n; i++)
        fout << culoare[i] << "\n";

    return 0;
}