Cod sursa(job #25155)

Utilizator raula_sanChis Raoul raula_san Data 4 martie 2007 11:06:32
Problema Puteri Scor 40
Compilator cpp Status done
Runda preONI 2007, Runda 3, Clasa a 10-a Marime 1.36 kb
#include <cstdio>

#define dim 100001

long N, Sol;

struct powers
{ int i, j, k; } P[dim];

int putere( int, int );
int cmmdc( int, int );

int main()
{
    freopen("puteri.in", "r", stdin);
    freopen("puteri.out", "w", stdout);
    
    scanf("%d", &N);
    
    long i, j;
    
    for(i=1; i<=N; ++i)
             scanf("%d %d %d", &P[i].i, &P[i].j, &P[i].k);

    for(i=1; i<N; ++i)
             for(j=i+1; j<=N; ++j)
                        Sol += putere(i, j);
    
    printf("%ld", Sol);

    fclose(stdin);
    fclose(stdout);

    return 0;
}

int cmmdc( int x, int y )
{
    int r;
    
    while( y )
    {
           r = x % y;
           x = y;
           y = r;
    }
    
    return x;
}

int putere( int i, int j )
{
    int a = P[i].i + P[j].i;
    int b = P[i].j + P[j].j;
    int c = P[i].k + P[j].k;
    int z = 0, x, y;
    
    if( !a ) ++ z;
    if( !b ) ++ z;
    if( !c ) ++ z;
    
    if( z == 2 ) return 1;
    if( z == 1 )
    {
        if( !a )
            x = cmmdc(b, c);
        if( !b )
            x = cmmdc(a, c);
        if( !c )
            x = cmmdc(a, b);
        
        if( x > 1 ) return 1;
        else return 0;
    }
    if( !z )
    {
        x = cmmdc(a, b);
        y = cmmdc(x, c);
        
        if( y > 1 ) return 1;
        else return 0;
    }
}