Pagini recente » Cod sursa (job #858080) | Cod sursa (job #2417660) | Cod sursa (job #624870) | Istoria paginii runda/aaaaa | Cod sursa (job #2051094)
#include<fstream>
#include<cmath>
#include<limits>
#include<cstdlib>
#include<vector>
#define MAX3 500
#define MAX5 500
using namespace std;
ifstream fin("aliens.in");
ofstream fout("aliens.out");
const short SHMIN = numeric_limits<short>::min();
struct str{
short x, y, z;
str( int a, int b, int c ){
this->x = a;
this->y = b;
this->z = c;
}
};
int n, ok;
short D[2][2 * MAX3 + 5][2 * MAX5 + 5];
inline str convert( int a, int b ){
str aux(0, 0, 0);
while( a % 2 == 0 ) aux.x++, a /= 2;
while( b % 2 == 0 ) aux.x--, b /= 2;
while( a % 3 == 0 ) aux.y++, a /= 3;
while( b % 3 == 0 ) aux.y--, b /= 3;
while( a % 5 == 0 ) aux.z++, a /= 5;
while( b % 5 == 0 ) aux.z--, b /= 5;
return aux;
}
int number[1005];
void mul( int x ){
int t = 0;
for( int i = 1; i <= number[0]; i++ ){
number[i] = number[i] * x + t;
t = number[i] / 10;
number[i] %= 10;
}
while( t != 0 ){
number[ ++number[0] ] = t % 10;
t /= 10;
}
return;
}
int main(){
fin >> n;
for( int i = 0; i <= 2 * MAX3; i++ )
for( int j = 0; j <= 2 * MAX5; j++ )
D[0][i][j] = SHMIN;
D[0][MAX3][MAX5] = 0;
ok = 1;
for( int i = 1; i <= n; i++ ){
int a, b;
fin >> a >> b;
str nr = convert( a, b );
for( int j = -MAX3; j <= MAX3; j++ ){
for( int k = -MAX5; k <= MAX5; k++ ){
int Y = j - nr.y + MAX3;
int Z = k - nr.z + MAX5;
D[ok][j + MAX3][k + MAX5] = D[!ok][j + MAX3][k + MAX5];
if( !( 0 <= Y && Y <= 2 * MAX3 && 0 <= Z && Z <= 2 * MAX5 ) )
continue;
if( D[!ok][Y][Z] != SHMIN ){
short cnt = D[!ok][Y][Z] + nr.x;
D[ok][j + MAX3][k + MAX5] = max( D[!ok][j + MAX3][k + MAX5], cnt );
}
}
}
ok = !ok;
}
ok = !ok;
str sol( 0, 0, 0 );
for( int i = MAX3; i >= 0; i-- ){
for( int j = MAX5; j >= 0; j-- ){
int val = D[ok][i + MAX3][j + MAX5];
if( val < 0 )
continue;
if( sol.x * log(2) + sol.y * log( 3 ) + sol.z * log( 5 ) < val * log(2) + i * log( 3 ) + j * log( 5 ) ){
sol.x = val;
sol.y = i;
sol.z = j;
}
}
}
number[0] = number[1] = 1;
while( sol.x != 0 ){
mul( 2 );
sol.x--;
}
while( sol.y != 0 ){
mul( 3 );
sol.y--;
}
while( sol.z != 0 ){
mul( 5 );
sol.z--;
}
for( int i = number[0]; i >= 1; i-- )
fout << number[i];
return 0;
}