Cod sursa(job #1425151)

Utilizator smaraldaSmaranda Dinu smaralda Data 26 aprilie 2015 21:13:10
Problema Curcubeu Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include<stdio.h>
#include<algorithm>
using namespace std;

#define LL long long
const int NMAX = 1e6 + 5;

int color[NMAX], nxt[NMAX], a[NMAX], b[NMAX], c[NMAX];

int getNext (int node) {
    if(nxt[node] == node)
        return node;
    return nxt[node] = getNext(nxt[node]);
}

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

    scanf("%d%d%d%d", &n, &a[1], &b[1], &c[1]);
    
    for(i = 2; i <= n; ++ i) {
        nxt[i] = i;
        a[i] = ((LL)a[i - 1] * i) % n;
        b[i] = ((LL)b[i - 1] * i) % n;
        c[i] = ((LL)c[i - 1] * i) % n;
    }
    nxt[1] = 1;

    for(i = n - 1; i >= 1; -- i) {
        if(a[i] > b[i])
            swap(a[i], b[i]);

        left = getNext(a[i]);
        nxt[b[i]] = b[i] + 1;
        while(left <= b[i]) {
            if(!color[left])
                color[left] = c[i];
            nxt[left] = max(nxt[left], nxt[b[i]]);

            ++ left;
            left = getNext(left);
        }
    }

    for(i = 1; i < n; ++ i)
        printf("%d\n", color[i]);
    return 0;
}