Cod sursa(job #324829)

Utilizator Magnuscont cu nume gresit sau fals Magnus Data 17 iunie 2009 16:16:55
Problema Aliens Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.61 kb
#include <stdio.h>  
#include <math.h>  
#include <assert.h>  
  
#define nmax1 1805  
#define lmax1 900  
#define nmax2 1205  
#define lmax2 600  
#define cmax 1024  
#define nmax 50  
#define inf 30000  
#define FOR(i,s,d) for(i=(s);i<(d);++i)  
#define FORD(i,s,d) for(i=(d)-1;i>=(s);--i)  
  
int n,rez[cmax];  
short int A[nmax1][nmax2];  
  
void inm(int x)  
{  
    int i,t=0;  
    for(i=1;i<=rez[0]||t;++i)  
    {  
        t+=rez[i]*x;  
        rez[i]=t%10;  
        t/=10;  
    }  
    rez[0]=i-1;  
}  
  
int main()  
{  
    int i,a,b,c,ii,j,l1,l2,r1,r2,x;  
    assert(freopen("aliens.in","r",stdin));  
    freopen("aliens.out","w",stdout);  
    assert(scanf("%d",&n)==1);  
    assert(n<=50);  
    assert(n>=1);  
    FOR(i,0,nmax1) FOR(j,0,nmax2)  
        A[i][j]=-inf;  
    A[lmax1][lmax2]=0;  
    l1=lmax1, r1=l1+1;  
    l2=lmax2, r2=l2+1;  
    FOR(ii,0,n)  
    {  
        assert(scanf("%d",&x)==1);  
        assert(x<=400000000);  
        assert(x>0);  
        for(a=0;x%2==0;x/=2,a++);  
        for(b=0;x%3==0;x/=3,b++);  
        for(c=0;x%5==0;x/=5,c++);  
        assert(x==1);  
        assert(scanf("%d",&x)==1);  
        assert(x<=400000000);  
        assert(x>0);  
        for(;x%2==0;x/=2,a--);  
        for(;x%3==0;x/=3,b--);  
        for(;x%5==0;x/=5,c--);  
        assert(x==1);  
        if(b<=0&&c<=0)  
            FOR(i,l1,r1) FOR(j,l2,r2)  
                if(A[i][j]+a>A[i+b][j+c])  
                    A[i+b][j+c]=A[i][j]+a;  
        if(b>0&&c<=0)  
            FORD(i,l1,r1) FOR(j,l2,r2)  
                if(A[i][j]+a>A[i+b][j+c])  
                    A[i+b][j+c]=A[i][j]+a;  
        if(b<=0&&c>0)  
            FOR(i,l1,r1) FORD(j,l2,r2)  
                if(A[i][j]+a>A[i+b][j+c])  
                    A[i+b][j+c]=A[i][j]+a;  
        if(b>0&&c>0)  
            FORD(i,l1,r1) FORD(j,l2,r2)  
                if(A[i][j]+a>A[i+b][j+c])  
                    A[i+b][j+c]=A[i][j]+a;  
        if(b>=0)  
            r1+=b;  
        else  
            l1+=b;  
        if(c>=0)  
            r2+=c;  
        else  
            l2+=c;  
    }  
    double sol=-1,lg2=log(2),lg3=log(3),lg5=log(5);  
    a=b=c=0;  
    FOR(i,lmax1,nmax1) FOR(j,lmax2,nmax2)  
        if(A[i][j]>0&&(i-lmax1)*lg3+(j-lmax2)*lg5+A[i][j]*lg2>sol)  
            sol=(i-lmax1)*lg3+(j-lmax2)*lg5+A[i][j]*lg2,b=i-lmax1,c=j-lmax2,a=A[i][j];  
    rez[0]=rez[1]=1;  
    FOR(i,0,a) inm(2);  
    FOR(i,0,b) inm(3);  
    FOR(i,0,c) inm(5);  
    FORD(i,1,rez[0]+1)  
        printf("%d",rez[i]);  
    printf("\n");  
    return 0;  
}