Cod sursa(job #1425167)

Utilizator smaraldaSmaranda Dinu smaralda Data 26 aprilie 2015 21:43:13
Problema Curcubeu Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 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])
        return node;
    return nxt[node] = getNext(nxt[node]);
}

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

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

    for(i = n - 1; i >= 1; -- i) {
        left = getNext(a[i]);
        while(left <= b[i]) {
            color[left] = c[i];
            ant = left;

            left = getNext(left + 1);
            nxt[ant] = left;
        }
    }

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