Problem p00005 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < int(n); ++i)

ll a, b;

ll gcd(ll a, ll b) {
  if ((0 == a) || (0 == b))
    return 0;

  while (a != b) {
    if (a > b)
      a -= b;
    else
      b -= a;
  }
  return a;
}

ll lcm(ll a, ll b) {
  if ((a == 0) || (b == 0))
    return 0;

  return ((a / gcd(a, b)) * b);
}

int main() {
  while (cin >> a >> b) {
    cout << gcd(a, b) << " " << lcm(a, b) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define ll long long
#define V 1000000
static const double PI = 6 * asin(0.5);

/*
bool operator < (ll a,ll b){
        return a>b;
}
*/

bool prime[V];
vector<ll> pri;
priority_queue<ll, vector<ll>, greater<ll>> a_pri;
priority_queue<ll, vector<ll>, greater<ll>> b_pri;
map<ll, ll> amap;
map<ll, ll> bmap;

void divisor(int n, priority_queue<ll, vector<ll>, greater<ll>> qu) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      qu.push(i);
      cout << "i = " << i << endl;
      if (i != n / i && i != n) {
        qu.push(n / i);
        cout << "n/i = " << n / i << endl;
      }
    }
  }
  cout << endl;
}

map<ll, ll> prime_factor(int n) {
  map<ll, ll> res;
  for (int i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ++res[i];
      // cout<<"res = "<<res[i]<<" "<<i<<endl;
      n /= i;
    }
  }
  // cout<<"n = "<<n<<endl;
  if (n != 1)
    res[n] = 1;
  // cout<<"map[2] = "<<res[2]<<endl;
  return res;
}

void prime_is() {
  prime[1] = true;
  // cout<<"hoge";
  for (int i = 1; i * i < V; i++) {
    if (!prime[i]) {
      pri.push_back(i);
      for (int j = i * i; j < V; j += i) {
        prime[j] = true;
      }
    }
  }
}

int main() {
  ll a, b;
  ll gcd = 1, lcm = 1;

  // rep(i,V) prime[i] = false;
  // prime_is();
  while (cin >> a >> b) {
    ll a_tmp = a, b_tmp = b;

    // a_pri.clear(); b_pri.clear();

    /*
    cout<<a_tmp<<" ";
    rep(i,10){
            cout<<pri[i]<<" ";
    }
    cout<<endl;
    */
    // cout<<a<<" "<<b<<endl;
    /*
    rep(i,V){
            if(a_tmp==1){
                    //cout<<"break\\n";
                    break;
            }
            while(a_tmp%pri[i]==0){
                    a_pri.push(pri[i]);
                    a_tmp/=pri[i];
            }
            //cout<<a_tmp<<endl;
    }
    rep(i,V){
            if(b_tmp==1){
                    //cout<<"break";
                    break;
            }
            while(b_tmp%pri[i]==0){
                    b_pri.push(pri[i]);
                    b_tmp/=pri[i];
            }
            //cout<<"b_tmp = "<<b_tmp<<endl;
            //cout<<"t = "<<t<<endl;

    }
    */
    // divisor(a,a_pri);
    // divisor(b,b_pri);

    amap = prime_factor(a);
    bmap = prime_factor(b);

    rep(i, a) {
      rep(j, amap[i]) {
        a_pri.push(i);
        // cout<<"i = "<<i<<endl;
      }
    }
    rep(i, b) {
      rep(j, bmap[i]) { b_pri.push(i); }
    }

    // cout<<"hoge\\n";
    gcd = lcm = 1;
    rep(i, V) {
      if (a_pri.empty() || b_pri.empty())
        break;
      // cout<<a_pri.front()<< " "<<b_pri.front()<<endl;
      if (a_pri.top() > b_pri.top()) {
        // cout<<"a\\n";
        lcm *= b_pri.top();
        b_pri.pop();
      } else if (a_pri.top() == b_pri.top()) {
        // cout<<"=\\n";
        gcd *= a_pri.top();
        lcm *= a_pri.top();
        a_pri.pop();
        b_pri.pop();
      } else {
        // cout<<"b\\n";
        lcm *= a_pri.top();
        a_pri.pop();
      }
    }
    while (!a_pri.empty()) {
      lcm *= a_pri.top();
      a_pri.pop();
    }
    while (!b_pri.empty()) {
      lcm *= b_pri.top();
      b_pri.pop();
    }
    cout << gcd << " " << lcm << endl;
  }

  return 0;
}