Cod sursa(job #3267809)

Utilizator matei__bBenchea Matei matei__b Data 12 ianuarie 2025 13:51:30
Problema Curcubeu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.1 kb
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
#define ll long long
#define ull unsigned long long
#define ld long double
#define chad char
#define mod 1'000'000'007
#define dim 100005
#define lim 1000000
#define BASE 31
#define NMAX 21'005
#define FOR(i,a,b) for(int i=(a); i<=(b); i++)
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define nr_biti __builtin_popcount
using namespace __gnu_pbds; 
using namespace std;

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

int t=1;
int n;
int a[lim+5],b[lim+5],c[lim+5];
int ans[lim+5];

struct dsu 
{
    vector<int> t;
    vector<int> dr;

    void init(int n)
    {
        t=vector<int>(n+3);
        dr=vector<int>(n+3);

        for(int i=1; i<=n; i++)
        {
            t[i]=i;
            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;

        t[y]=x;
        dr[x]=max(dr[x],dr[y]);
        return 1;
    }
}v;

void solve()
{
    fin >> n >> a[1] >> b[1] >> c[1];
    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;
    }    

    v.init(n);
    for(int i=n-1; i>=1; i--)
    {
        int pos=min(a[i],b[i]);
        int u=max(a[i],b[i]);

        if(ans[pos])
            pos=v.dr[v.root(pos)];

        int aux=pos;

        while(pos<=u)
        {
            ans[pos]=c[i];
            if(pos>1)
                v.unite(aux-1,pos);
            pos=v.dr[v.root(pos)];
        }
    }

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

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    //cin >> t;
    while(t--)
        solve();

    return 0;
}