Cod sursa(job #2842948)

Utilizator andu9andu nita andu9 Data 1 februarie 2022 19:00:37
Problema Curcubeu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <fstream>

using namespace std;

ifstream f("curcubeu.in");
ofstream g("curcubeu.out");

const int INF = 1e6 + 10;
int a[INF], b[INF], c[INF], colour[INF], urm[INF];

int root (int x)
{
    if (urm[x] != x)
        urm[x] = root (urm[x]);
    return urm[x];
}

int main ()
{
    int n, i, j, stanga, dreapta, total;
    f >> n >> a[1] >> b[1] >> c[1];
    for (i = 2; i <= n - 1; i += 1)
    {
        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 (i = 1; i <= n; i += 1)
        urm[i] = i;
    total = 0;
    for (i = n - 1; i >= 1 and total < n - 1; i -= 1)
    {
        stanga = min (a[i], b[i]);
        dreapta = max (a[i], b[i]);
        j = root (stanga);
        while (j <= dreapta)
        {
            colour[j] = c[i], total += 1;
            urm[j] = j + 1;
            j = root (j);
        }
    }
    for (i = 1; i <= n - 1; i += 1)
        g << colour[i] << '\n';
    return 0;
}