Cod sursa(job #2924098)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 25 septembrie 2022 13:45:02
Problema Curcubeu Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.12 kb
#include <fstream>

using namespace std;

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

const int max_size = 1e6 + 5;

int t[max_size], a[max_size], b[max_size], c[max_size], ans[max_size];

int rad (int x)
{
    if (x == t[x])
    {
        return x;
    }
    return rad(t[x]);
}

int main ()
{
    t[1] = 1;
    int n;
    in >> n >> a[1] >> b[1] >> c[1];
    if (a[1] > b[1])
    {
        swap(a[1], b[1]);
    }
    for (int i = 2; i < n; i++)
    {
        t[i] = i;
        a[i] = (a[i - 1] * i) % n;
        b[i] = (b[i - 1] * i) % n;
        c[i] = (c[i - 1] * i) % n;
        if (a[i] > b[i])
        {
            swap(a[i], b[i]);
        }
    }
    for (int i = n - 1; i > 0; i--)
    {
        int j = a[i];
        while (j <= b[i])
        {
            int rj = rad(j) + 1;
            if (ans[j] == 0)
            {
                ans[j] = c[i];
                t[j] = j + 1;
            }
            j = rj;
        }
    }
    for (int i = 1; i < n; i++)
    {
        out << ans[i] << '\n';
    }
    in.close();
    out.close();
    return 0;
}