Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <cmath>
- #include <vector>
- #include <stdio.h>
- #include <string.h>
- using namespace std;
- int n,t;
- vector<int> patas,alturas;
- int GCD(int a,int b)
- {
- return (!a)? b:GCD(b%a,a);
- }
- int LCM(int a,int b)
- {
- return ((a*b)/GCD(a,b));
- }
- pair< int, int> solucion(int mask,int profundidad,int &valor_esperado,int pos,int MCM)//first max second min
- {
- if(profundidad==4)
- return make_pair((valor_esperado/MCM)*MCM,((valor_esperado/MCM)+1)*MCM);
- int maximo=0,minimo=2147483647;
- for (int i = pos; i < n; ++i)
- if (!(mask&(1<<i)))
- for (int j = i+1; j < n; ++j)
- if (!(mask&(1<<j)))
- {
- pair<int,int> sol;
- if (!profundidad)
- sol=solucion(mask|(1<<i)|(1<<j),profundidad+2,valor_esperado,j+1,LCM(patas[i],patas[j]));
- else if(profundidad==2)
- sol=solucion(mask|(1<<i)|(1<<j),profundidad+2,valor_esperado,j+1,LCM(MCM,LCM(patas[i],patas[j])));
- maximo=max(maximo,sol.first);
- minimo=min(minimo,sol.second);
- }
- return make_pair(maximo,minimo);
- }
- int main()
- {
- while(scanf("%d %d",&n,&t)!=EOF)
- {
- if(!n && !t)
- break;
- patas.resize(n);alturas.resize(t);
- for (int i = 0; i < n; ++i)
- scanf("%d",&patas[i]);
- for (int i = 0; i < t; ++i)
- scanf("%d",&alturas[i]);
- for (int i = 0; i < t; ++i)
- {
- int candidatos=0;
- vector< pair<int,int> > resto;
- for (int j = 0; j < n; ++j)
- {
- if (!(alturas[i]%patas[j]))//si el modulo es cero es candidato
- candidatos++;
- if (candidatos==4)
- {
- printf("%d %d\n",alturas[i],alturas[i]);
- break;
- }
- }
- if (candidatos!=4)
- {
- int mascara=1<<n;
- pair<int,int> valor=solucion(mascara,0,alturas[i],0,0);
- printf("%d %d\n",valor.first,valor.second);
- }
- }
- patas.clear();alturas.clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement