Advertisement
Gustavo_Inzunza

Mint

Jul 30th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <vector>
  5. #include <stdio.h>
  6. #include <string.h>
  7. using namespace std;
  8. int n,t;
  9. vector<int> patas,alturas;
  10. int GCD(int a,int b)
  11. {
  12.     return (!a)? b:GCD(b%a,a);
  13. }
  14. int LCM(int a,int b)
  15. {
  16.     return ((a*b)/GCD(a,b));
  17. }
  18. pair< int, int> solucion(int mask,int profundidad,int &valor_esperado,int pos,int MCM)//first max second min
  19. {
  20.     if(profundidad==4)
  21.         return make_pair((valor_esperado/MCM)*MCM,((valor_esperado/MCM)+1)*MCM);
  22.     int maximo=0,minimo=2147483647;
  23.     for (int i = pos; i < n; ++i)
  24.         if (!(mask&(1<<i)))
  25.             for (int j = i+1; j < n; ++j)
  26.                 if (!(mask&(1<<j)))
  27.                 {
  28.                     pair<int,int> sol;
  29.                     if (!profundidad)
  30.                         sol=solucion(mask|(1<<i)|(1<<j),profundidad+2,valor_esperado,j+1,LCM(patas[i],patas[j]));
  31.                     else if(profundidad==2)
  32.                             sol=solucion(mask|(1<<i)|(1<<j),profundidad+2,valor_esperado,j+1,LCM(MCM,LCM(patas[i],patas[j])));
  33.                     maximo=max(maximo,sol.first);
  34.                     minimo=min(minimo,sol.second);
  35.                 }
  36.     return make_pair(maximo,minimo);
  37. }
  38. int main()
  39. {
  40.     while(scanf("%d %d",&n,&t)!=EOF)
  41.     {
  42.         if(!n && !t)
  43.             break;
  44.         patas.resize(n);alturas.resize(t);
  45.         for (int i = 0; i < n; ++i)
  46.             scanf("%d",&patas[i]);
  47.         for (int i = 0; i < t; ++i)
  48.             scanf("%d",&alturas[i]);
  49.         for (int i = 0; i < t; ++i)
  50.         {
  51.             int candidatos=0;
  52.             vector< pair<int,int> > resto;
  53.             for (int j = 0; j < n; ++j)
  54.             {
  55.                 if (!(alturas[i]%patas[j]))//si el modulo es cero es candidato
  56.                     candidatos++;
  57.                 if (candidatos==4)
  58.                 {
  59.                     printf("%d %d\n",alturas[i],alturas[i]);
  60.                     break;
  61.                 }
  62.             }
  63.             if (candidatos!=4)
  64.             {
  65.                 int mascara=1<<n;
  66.                 pair<int,int> valor=solucion(mascara,0,alturas[i],0,0);
  67.                 printf("%d %d\n",valor.first,valor.second);
  68.             }
  69.         }
  70.         patas.clear();alturas.clear();
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement