Cod sursa(job #27271)

Utilizator mariusdrgdragus marius mariusdrg Data 6 martie 2007 12:06:55
Problema Puteri Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.13 kb
#include<stdio.h>
#include<algorithm>

const int pr[40] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 10000};

const int maxn = 100001;
const int maxn2 = 128;

short a[maxn];
short a1[maxn];
int j;
long long solf;
long long sol[maxn];
int n;
int i;
short b[maxn];
short c[maxn];
short mat[maxn2][maxn2][maxn2];
int prod;
int nr1;

int calc(int i)
{
                int sol = 0;
                int j = 0;
                
                for(j = 1; j <= n; ++j)
                {
                       sol += mat[(i-(a[j]%i))%i][(i-(b[j]%i))%i][(i-(c[j]%i))%i];
                       mat[a[j]%i][b[j]%i][c[j]%i]++;
                }
                for(j = 1; j <= n; ++j)
                {
                       mat[a[j]%i][b[j]%i][c[j]%i]--;
               
                }
                return sol;

}

void bkt(int i)
{
        if (pr[i] < 128 && prod * pr[i] <= 128)
        {
                for(a1[i] = 0; a1[i] <= 1; ++a1[i])
                {
                        if (a1[i] == 1)
                        {
                                nr1++;
                                prod *= pr[i];
                        }
                        bkt(i+1);
                        if (a1[i] == 1)
                        {
                                nr1--;
                                prod /= pr[i];
                        }
                }
        }
        else
        {
                
                if (prod < 128 && prod != 1)
                {
                        if (nr1 % 2 == 1) solf += calc(prod);
                                else  solf -= calc(prod);
                                
                }
        }

}


int main()
{
        freopen("puteri.in","r",stdin);
        freopen("puteri.out","w",stdout);
        scanf("%d",&n);
        prod = 1;
        for(i = 1; i <= n; ++i)
        {
                scanf("%hd %hd %hd", &a[i],&b[i],&c[i]);
        
        }

        bkt(1);
        printf("%lld\n",solf);
        

        return 0;
        
}