Cod sursa(job #109749)

Utilizator mihai0110Bivol Mihai mihai0110 Data 25 noiembrie 2007 12:37:00
Problema Aliens Scor 10
Compilator cpp Status done
Runda preONI 2008, Runda 1, Clasa a 10-a Marime 0.8 kb
#include<stdio.h>
FILE *f,*g;
long x,max,n,i,m,x2,x3,x5,j,a2[100],a3[100],a5[100];
int main(void)
{
f=fopen("aliens.in","r");
g=fopen("aliens.out","w");
fscanf(f,"%ld",&n);
for(i=1;i<=n;i++)
{
fscanf(f,"%ld",&x);
while(x%2==0)
{
a2[i]++;
x/=2;
}
while(x%3==0)
{
a3[i]++;
x/=3;
}
while(x%5==0)
{
a5[i]++;
x/=5;
}
fscanf(f,"%ld",&x);
while(x%2==0)
{
a2[i]--;
x/=2;
}
while(x%3==0)
{
a3[i]--;
x/=3;
}
while(x%5==0)
{
a5[i]--;
x/=5;
}
}
m=1<<n;
for(i=1;i<=m;i++)
{
x=i;
x2=0;
x3=0;
x5=0;
for(j=1;j<=n;j++)
{
if(x%2==1)
{
x2+=a2[j];
x3+=a3[j];
x5+=a5[j];
}
x/=2;
}
if(x2>=0&&x3>=0&&x5>=0)
{
x=1;
for(j=1;j<=x2;j++)
x*=2;
for(j=1;j<=x3;j++)
x*=3;
for(j=1;j<=x5;j++)
x*=5;
if(x>max)
max=x;
}
}
fprintf(g,"%ld\n",max);
fclose(f);
fclose(g);
return 0;
}