Cod sursa(job #25361)

Utilizator a7893Nae Mihai a7893 Data 4 martie 2007 12:14:06
Problema Puteri Scor 40
Compilator cpp Status done
Runda preONI 2007, Runda 3, Clasa a 10-a Marime 1.49 kb
#include<stdio.h>
#include<math.h>
int  n,a[100000][3],nr=0,p[3];
void citire()
{
	int i,j;
	scanf("%d",&n);
	for(i=0;i<n;i++)
		for(j=0;j<3;j++)
			scanf("%d",&a[i][j]);
}
void init()
{
	for(int i=0;i<3;i++)
		p[i]=0;
}
void putere(int a,int b,int c)
{
	p[0]+=a;
	p[1]+=b;
	p[2]+=c;
}
int cmmdc1(int a,int b,int c)
{
	while(a!=b)
		if(a>b)
			a-=b;
		else
			b-=a;
	while(a!=c)
		if(a>c)
			a-=c;
		else
			c-=a;
	return a;
}
int cmmdc2(int a,int b)
{
	while(a!=b)
		if(a>b)
			a-=b;
		else
			b-=a;
	return a;
}
int verif()
{
	int cm;
	if(p[0]==0&&p[1]==0&&p[2])
		return 1;
	if(p[0]==0&&p[1]&&p[2]==0)
		return 1;
	if(p[0]&&p[1]==0&&p[2]==0)
		return 1;
	if(p[0]&&p[1]&&p[2])
	{
		cm=cmmdc1(p[0],p[1],p[2]);
		if(cm!=1)
		if(p[0]%cm==0&&p[1]%cm==0&&p[2]%cm==0)
			return 1;
	}
	if(p[0]==0&&p[1]&&p[2])
	{
		cm=cmmdc2(p[1],p[2]);
		if(cm!=1)
		if(p[1]%cm==0&&p[2]%cm==0)
			return 1;
	}
	if(p[0]&&p[1]==0&&p[2])
	{
		cm=cmmdc2(p[0],p[2]);
		if(cm!=1)
			if(p[0]%cm==0&&p[2]%cm==0)
				return 1;
	}
	if(p[0]&&p[1]&&p[2]==0)
	{
		cm=cmmdc2(p[0],p[1]);
		if(cm!=1)
			if(p[0]%cm==0&&p[1]%cm==0)
				return 1;
	}
	return 0;
}
int main()
{
	freopen("puteri.in","r",stdin);
	freopen("puteri.out","w",stdout);
	int i,k;
	citire();
	for(i=0;i<n-1;i++)
	{
		for(k=i+1;k<n;k++)
		{
			init();
			putere(a[i][0],a[i][1],a[i][2]);
			putere(a[k][0],a[k][1],a[k][2]);
			if(verif())
				nr++;
		}
	}
	printf("%d\n",nr);
	return 0;
}