Cod sursa(job #1456890)

Utilizator greenadexIulia Harasim greenadex Data 2 iulie 2015 12:49:34
Problema Curcubeu Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <bits/stdc++.h>

using namespace std;

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

const int MAX = 1000005;
int f[MAX], n, a, b, c, qa[MAX], qb[MAX], qc[MAX], col[MAX];

int find_root(int x) {
    if (x != f[x])
        f[x] = find_root(f[x]);
    return f[x];
}

void unite (int x, int y) {
    int a = find_root(x), b = find_root(y);
    f[min(a, b)] = max(a, b);
}


int main() {
    fin >> n >> qa[1] >> qb[1] >> qc[1];
    col[0] = -1;
    col[1] = -1;
    col[n] = -1;
    f[1] = 1;
    for (int i = 2; i < n; i++) {
        col[i] = -1;
        f[i] = i;
        qa[i] = (1LL * qa[i - 1] * i) % n;
        qb[i] = (1LL * qb[i - 1] * i) % n;
        qc[i] = (1LL * qc[i - 1] * i) % n;
    }

    for (int i = n - 1; i >= 1; i--)
        for (int x = max(1, min(qa[i], qb[i])), y = max(qb[i], qa[i]); x <= y; ) {
            if(col[x] == -1)
                col[x] = qc[i];
            if (col[x - 1] != -1)
                unite(find_root(x - 1), find_root(x));
            if (x == y && col[x + 1] != -1)
                unite(find_root(x), find_root(x + 1));
            x = find_root(x) + 1;
        }

    for (int i = 1; i < n; i++)
        fout << (col[i] == -1 ? 0 : col[i]) << '\n';

    return 0;
}