Cod sursa(job #2577159)

Utilizator RazvanPanaiteRazvan Panaite RazvanPanaite Data 8 martie 2020 16:10:02
Problema Curcubeu Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.29 kb
#include <bits/stdc++.h>
#define pb push_back

using namespace std;

void debug_out() { cerr << '\n'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}
#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"

typedef pair<int,int> pii;
typedef long long int ll;
typedef long double ld;

const int DMAX = 1e6+10;

int a[DMAX];
int b[DMAX];
int c[DMAX];

int ans[DMAX];
int tata[DMAX];
int lg[DMAX];

bool uz[DMAX];

int n;

int findd(int node);
void unire(int x,int y);
int gint();
void wint(int x);

int main(){
    freopen("curcubeu.in", "r", stdin);
	freopen("curcubeu.out", "w", stdout);

    int t,i,j;
    int x,y;

    n=gint();
    a[1]=gint();
    b[1]=gint();
    c[1]=gint();

    lg[1]=1;
    for(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;
        lg[i]=1;
    }
    for(i=n-1;i>=1;i--){
        x=min(a[i],b[i]);
        y=max(a[i],b[i]);
        for(;x<=y;x+=lg[x]){
            if(!uz[x]){
                uz[x]=true;
                ans[x]=c[i];
                if(uz[x-1])
                    unire(x-1,x);
                if(uz[x+1])
                    unire(x,x+1);
            }
            x=findd(x);
        }
    }
    for(i=1;i<n;i++)
        wint(ans[i]);

    return 0;
}
int findd(int node){
    int ans,aux;
    ans=node;
    while(tata[ans])
        ans=tata[ans];
    while(tata[node]){
        aux=node;
        node=tata[node];
        tata[aux]=ans;
    }
    return ans;
}
void unire(int x,int y){
    x=findd(x);
    y=findd(y);
    if(x == y)
        return;
    tata[y]=x;
    lg[x]+=lg[y];
}
int gint(){
	char ch = getchar();
	while(ch < '0' || '9' < ch)
        ch = getchar();
	int x = 0;
	while('0' <= ch && ch <= '9'){
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	return x;
}
void wint(int x){
	if(x == 0){
		putchar('0');
		putchar('\n');
		return;
	}

	vector<char> v;
	while(x){
		v.push_back(x % 10 + '0');
		x /= 10;
	}
	for(int i = (int)v.size() - 1; i >= 0; i--)
		putchar(v[i]);
	putchar('\n');
}