Cod sursa(job #109776)

Utilizator Adriana_SAdriana Sperlea Adriana_S Data 25 noiembrie 2007 12:41:15
Problema Aliens Scor 30
Compilator cpp Status done
Runda preONI 2008, Runda 1, Clasa a 10-a Marime 2.09 kb
#include <stdio.h>
#include <math.h>
#include <set>
#include <vector>

using namespace std;

const int N_MAX = 56;

set <pair <int, pair <int, int> > > v;
vector <pair <int, pair <int, int> > > aux;

int sus[N_MAX], jos[N_MAX];

void fac(int X, int &nr2, int &nr3, int &nr5)
{
	nr2 = 0, nr3 = 0, nr5 = 0;

	while (X % 2 == 0) {
		nr2 ++;
		X /= 2;
	}
	while (X % 3 == 0) {
		nr3 ++;
		X /= 3;
	}
	while (X % 5 == 0) {
		nr5 ++;
		X /= 5;
	}
}

char rez[512];
int lg;

void mul(char A[], char B)
{
	int i;
	char t = 0;
	for (i = 1; i <= lg || t; i++, t /= 10) {
		A[i] = (t += A[i] * B) % 10;
	}
	
	lg = i - 1;
}


int main()
{
	freopen("aliens.in", "r", stdin);
#ifndef _SCREEN_
	freopen("aliens.out", "w", stdout);
#endif

	int N, i;
	scanf("%d\n", &N);
	for (i = 1; i <= N; i ++) {
		scanf("%d %d\n", &sus[i], &jos[i]);
	}

	v.insert(make_pair(0, make_pair(0, 0)));
	int nr2s, nr3s, nr5s, nr2j, nr3j, nr5j;
	int z, x, y;

	set <pair <int, pair <int, int> > >::iterator it, sf;
	vector <pair <int, pair <int, int> > >::iterator it2;

	for (i = 1; i <= N; i ++) {

		fac(sus[i], nr2s, nr3s, nr5s);
		fac(jos[i], nr2j, nr3j, nr5j);
		nr2s -= nr2j, nr3s -= nr3j, nr5s -= nr5j;
		aux.clear();

		for (it = v.begin(); it != v.end(); ++ it) {

			x = (*it).first;
			y = (*it).second.first;
			z = (*it).second.second;

			aux.push_back(make_pair(x + nr2s, make_pair(y + nr3s, z + nr5s)));
		}

		for (it2 = aux.begin(); it2 != aux.end(); ++ it2) {
			v.insert(*it2);
		}
	}

	double sum = 0, doi = log(2), trei = log(3), cinci = log(5);
	int mx = 0, my = 0, mz = 0;
	for (it = v.begin(); it != v.end(); ++ it) {

		x = (*it).first;
		y = (*it).second.first;
		z = (*it).second.second;

		if (doi * x + trei * y + cinci * z > sum && x >= 0 && y >= 0 && z >= 0) {
			sum = doi * x + trei * y + cinci * z;
			mx = x, my = y, mz = z;
		}
	}

	lg = 1;
	rez[1] = 1;

	int MIN = mx < mz ? mx : mz;

	for (i = 1; i <= mx - MIN; i ++) mul(rez, 2);
	for (i = 1; i <= my; i ++) mul(rez, 3);
	for (i = 1; i <= mz - MIN; i ++) mul(rez, 5);

	for (i = lg; i >= 1; i --) printf("%d", rez[i]);
	for (i = 1; i <= MIN; i ++) printf("0");
	printf("\n");

	return 0;
}