Cod sursa(job #3377427)

Utilizator matei__bBenchea Matei matei__b Data 23 noiembrie 2026 10:55:46
Problema Curcubeu Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.52 kb
// https://infoarena.ro/problema/curcubeu

#include <bits/stdc++.h>
using namespace std;

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

const int NMAX=1e6+8;

long long n;
long long a[NMAX],b[NMAX],c[NMAX];
int ans[NMAX];

struct dsu {
    vector<int> t, sz, dr;
    dsu(int n) : t(n+8), sz(n+8), dr(n+8) {
        for(int i=1; i<=n; i++) {
            t[i]=i;
            sz[i]=1;
            dr[i]=i+1;
        }
    }

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

    bool unite(int x,int y) {
        x=root(x);
        y=root(y);

        if(x==y) {
            return 0;
        }

        if(sz[x]<sz[y]) {
            swap(x,y);
        }

        sz[x]+=sz[y];
        t[y]=x;
        dr[x]=max(dr[x],dr[y]);

        return 1;
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    fin.tie(nullptr);
    fout.tie(nullptr);

    fin >> n >> a[1] >> b[1] >> c[1];
    for(long long i=2; i<n; i++) {
        a[i]=(a[i-1]*i)%n;
        b[i]=(b[i-1]*i)%n;
        c[i]=(c[i-1]*i)%n;
    }

    dsu v(n);
    for(int i=n-1; i>=1; i--) {
        int pos=min(a[i],b[i]);
        int st=pos, dr=max(a[i],b[i]);

        if(pos>1) {
            while(st<=dr) {
                v.unite(pos-1,st);
                st=v.dr[v.root(st)];
            }
        }

        st=pos;
        while(st<=dr) {
            ans[st]=c[i];
            st=v.dr[v.root(st)];
        }   
    }

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

    return 0;
}