Cod sursa(job #1319575)

Utilizator wGEORGEWGeorge Cioti wGEORGEW Data 17 ianuarie 2015 09:59:56
Problema Aliens Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.79 kb
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <utility>
 
using namespace std;
 
#define maxn 66
#define offset 30
 
int d[maxn][maxn][maxn], nd[maxn][maxn][maxn], o[maxn][3];
int n, x, y;
long long sol, cand;
 
int decompose(int x, int f)
{
    int sol=0;
 
    while(x%f==0)
    {
        ++sol;
        x/=f;
    }
 
    return sol;
}
 
int check(int a, int b, int c)
{
    if(a<0 || a>2*offset || b<0 || b>2*offset || c<0 || c>2*offset)
        return 0;
    return d[a][b][c];
}
 
int main()
{
    freopen("aliens.in", "r", stdin);
    freopen("aliens.out", "w", stdout);
 
    scanf("%d", &n);
    for(int i=1; i<=n; ++i)
    {
        scanf("%d%d", &x, &y);
 
        o[i][0]=decompose(x, 2)-decompose(y, 2);
        o[i][1]=decompose(x, 3)-decompose(y, 3);
        o[i][2]=decompose(x, 5)-decompose(y, 5);
    }
 
    nd[offset][offset][offset]=1;
 
    for(int i=1; i<=n; ++i)
    {
        memcpy(d, nd, sizeof(nd));
        for(int a=0; a<=2*offset; ++a)
            for(int b=0; b<=2*offset; ++b)
                for(int c=0; c<=2*offset; ++c)
                    nd[a][b][c]=max(nd[a][b][c], check(a-o[i][0], b-o[i][1], c-o[i][2]));
    }
 
    for(int a=offset; a<=2*offset; ++a)
        for(int b=offset; b<=2*offset; ++b)
            for(int c=offset; c<=2*offset; ++c)
            {
                if(nd[a][b][c]==0)
                    continue;
 
                cand=1;
                for(int i=1; i<=a-offset; ++i)
                    cand=cand*2;
                for(int i=1; i<=b-offset; ++i)
                    cand=cand*3;
                for(int i=1; i<=c-offset; ++i)
                    cand=cand*5;
 
                sol=max(sol, cand);
            }
 
    printf("%lld\n", sol);
 
    return 0;
}