Cod sursa(job #2729113)

Utilizator IoanMihaiIoan Mihai IoanMihai Data 24 martie 2021 11:07:42
Problema Curcubeu Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("curcubeu.in");
ofstream fout("curcubeu.out");
#define NMAX 1000005
int n, j;
int a[NMAX], b[NMAX], c[NMAX], sef[NMAX], cul[NMAX];
int Find(int x)
{
    int v, rad = x;
    while(sef[rad])
        rad = sef[rad];
    while(sef[x] != 0)
    {
        v = sef[x];
        sef[x] = rad;
        x = v;
    }
    return rad;
}
inline void Union(int x, int y)
{
    if (x != y)
        sef[x] = y;
}
int main() {
    fin >> n >> a[1] >> b[1] >> c[1];
    if (a[1] > b[1])
        swap(a[1], b[1]);
    for (int i=2;i<n;i++){
        a[i] = (1LL * i * a[i-1]) % n;
        b[i] = (1LL * i * b[i-1]) % n;
        c[i] = (1LL * i * c[i-1]) % n;
        if (a[i] > b[i])
            swap(a[i], b[i]);
    }

    for (int i=n-1;i>=1;i--){
        j = Find(a[i]);
        while (j <= b[i]){
            if (cul[j] == 0){
                cul[j] = c[i];
                Union(j, b[i]);
            }
            j = Find(++j);
        }
    }
    for (int i=1;i<n;i++)
        fout << cul[i] << '\n';
    return 0;
}