Cod sursa(job #3380170)

Utilizator Vlad_prisVlad Prismareanu Vlad_pris Data 1 decembrie 2026 21:23:07
Problema Curcubeu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <algorithm>
#include <fstream>
#include <vector>

using namespace std;

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

int findNext(int x, vector<int>& parent)
{
    int root = x;
    while (parent[root] != root)
        root = parent[root];

    while (parent[x] != x)
    {
        int next = parent[x];
        parent[x] = root;
        x = next;
    }

    return root;
}

int main()
{
    int n, a, b, c;
    fin >> n >> a >> b >> c;

    vector<int> A(n), B(n), C(n);
    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;
    }

    vector<int> color(n, 0);
    vector<int> nextUnpainted(n + 1);
    for (int i = 0; i <= n; ++i)
        nextUnpainted[i] = i;

    for (int i = n - 1; i >= 1; --i)
    {
        int left = max(1, min(A[i], B[i]));
        int right = max(A[i], B[i]);
        int position = findNext(left, nextUnpainted);

        while (position <= right)
        {
            color[position] = C[i];
            nextUnpainted[position] = findNext(position + 1, nextUnpainted);
            position = nextUnpainted[position];
        }
    }

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

    return 0;
}