Cod sursa(job #90381)

Utilizator ZeusCatalin Tiseanu Zeus Data 9 octombrie 2007 12:40:20
Problema Curcubeu Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.49 kb

// O( N ) 

#include <cstdio>

#define MN (1<<20)

int N, col[MN], next[MN], op[MN][3], repos[MN], rank[MN];

int find( int x )
{
    if( repos[x] == x )
        return x;
    return repos[x] = find( repos[x] );    
}

void unify( int x, int y )
{
    x = find(x), y = find(y);
    if( rank[x] > rank[y] ) repos[y] = x, rank[x] += rank[y];
    else repos[x] = y, rank[y] += rank[x]; 
}

void doit( int A, int B, int C )
{
    int last = -1;
    
    for( int i = A; i <= B; i++ )
    {
         if( i && col[i-1] )
             unify( i - 1, i );
         
         if( !col[i] )   
             col[i] = C;    
         else 
             i = next[ find( i ) ];
    }
    
    next[ find(A) ] = B;
}

int main()
{
    freopen("curcubeu.in", "r", stdin);
    freopen("curcubeu.out", "w", stdout);
    
    int A, B, C;
    scanf("%d %d %d %d", &N, &A, &B, &C);
    
    for( int i = 0; i < N - 1; i++ )
         next[i] = i, repos[i] = i, rank[i] = 0;
         
    for( int i = 2 ; i <= N; i++ )
    {
        if( A > B ){ int aux = A; A = B; B = aux; } 
        
        op[i-1][0] = A - 1, op[i-1][1] = B - 1, op[i-1][2] = C;
        
        A = ( (long long)(A) * i ) % N,
        B = ( (long long)(B) * i ) % N,
        C = ( (long long)(C) * i ) % N;  
    }
    
    for( int i = N - 1; i >= 1; --i )
        doit( op[i][0], op[i][1], op[i][2] );
    
    for( int i = 0; i < N - 1; i++ )
         printf("%d\n", col[i]);
    
    return 0;  
}