Cod sursa(job #565469)

Utilizator cosmyoPaunel Cosmin cosmyo Data 27 martie 2011 19:57:13
Problema Curcubeu Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <cstdio>
const int N = 1 << 20;
int  t[N];

struct tip{int st, dr, c, cl;} v[N];

inline int root(int x) {
    int r, y;
    for(r = x; r != t[r]; r = t[r]);

    for(;x != r; ) {
        y = t[x];
        t[x] = r;
        x = y;
    }

    return r;
}

inline void unite(int x, int y) {
    if(x < y) 
		t[x] = y;
	else
		t[y] = x;
}

int main() {
    freopen("curcubeu.in", "r", stdin);
    freopen("curcubeu.out", "w", stdout);
    int n, i, a, b, c;

    scanf("%d %d %d %d", &n, &a, &b, &c);
    for(i = 1; i < n; ++i) {
        if(a > b) {
			v[i].st = b;
			v[i].dr = a;
		}
		else {
			v[i].st = a;
			v[i].dr = b;
		}
        v[i].c = c;
        a = ((long long)a * (i + 1)) % n;
        b = ((long long)b * (i + 1)) % n;
        c = ((long long)c * (i + 1)) % n;
    }

    int  rd, j;

    for(i = 1; i <= n; ++i)
        t[i] = i;

    for(i = n - 1; i >= 1 ; --i) {


        rd = root(v[i].dr);

        for(j = v[i].st; j <= v[i].dr; ++j){
            if(t[j] == j) {
                v[j].cl = v[i].c;
            }
			else 
				j = root(j);
			unite(j, rd);
        }

	}

    for(i = 1; i <= n - 1; ++i)
        printf("%d\n", v[i].cl);

    return 0;
}