Problem p03241 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef map<ll, ll> M;

const ll INF = 1e9 + 100;
const ll MOD = 1e9 + 7;

ll n, m, m_;
V ya, pr;
vector<P> ps;

vector<ll> prime(ll n) {
  vector<bool> num;
  vector<ll> p;

  for (ll i = 0; i <= n + 10; i++)
    num.push_back(true);

  FOR(i, 2, n) {
    if (!num[i])
      continue;

    p.push_back(i);

    for (ll j = i * 2; j <= n; j += i) {
      num[j] = false;
    }
  }

  return p;
}

ll solve(ll pos, ll val) {
  ll hoge = m_;

  if (pos == ps.size() - 1) {
    REP(i, ps[pos].first + 1) {
      ll hoge_ = val * pow(ps[pos].second, i);

      if (hoge_ >= n) {
        hoge = min(hoge_, hoge);
      }
    }
    return hoge;
  }

  REP(i, ps[pos].first + 1) {
    ll hoge_ = solve(pos + 1, val * pow(ps[pos].second, i));

    if (hoge_ >= n)
      hoge = min(hoge_, hoge);
  }

  return hoge;
}

int main() {
  pr = prime(1000000);
  cin >> n >> m;
  m_ = m;

  ll ind = 0;
  while (m > 1) {
    if (m % pr[ind] != 0) {
      ind++;
      if (ind >= pr.size()) {
        ya.push_back(m);
        ps.push_back(P(1, m));
        m = 1;
      }
    } else {
      m /= pr[ind];
      ya.push_back(pr[ind]);

      if (ps.size() == 0 || ps[ps.size() - 1].second != pr[ind]) {
        ps.push_back(P(0, pr[ind]));
      }

      ps[ps.size() - 1].first++;
    }
  }

  cout << m_ / solve(0, 1) << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef map<ll, ll> M;

const ll INF = 1e9 + 100;
const ll MOD = 1e9 + 7;

ll n, m, m_;
V ya, pr;
vector<P> ps;

vector<ll> prime(ll n) {
  vector<bool> num;
  vector<ll> p;

  for (ll i = 0; i <= n + 10; i++)
    num.push_back(true);

  FOR(i, 2, n) {
    if (!num[i])
      continue;

    p.push_back(i);

    for (ll j = i * 2; j <= n; j += i) {
      num[j] = false;
    }
  }

  return p;
}

ll solve(ll pos, ll val) {
  ll hoge = m_;

  if (pos == ps.size() - 1) {
    REP(i, ps[pos].first + 1) {
      ll hoge_ = val * pow(ps[pos].second, i);

      if (hoge_ >= n) {
        hoge = min(hoge_, hoge);
      }
    }
    return hoge;
  }

  REP(i, ps[pos].first + 1) {
    ll hoge_ = solve(pos + 1, val * pow(ps[pos].second, i));

    if (hoge_ >= n)
      hoge = min(hoge_, hoge);
  }

  return hoge;
}

int main() {
  pr = prime(1000000);
  cin >> n >> m;
  m_ = m;

  if (m == 1) {
    cout << 1 << endl;
    return 0;
  }

  ll ind = 0;
  while (m > 1) {
    if (m % pr[ind] != 0) {
      ind++;
      if (ind >= pr.size()) {
        ya.push_back(m);
        ps.push_back(P(1, m));
        m = 1;
      }
    } else {
      m /= pr[ind];
      ya.push_back(pr[ind]);

      if (ps.size() == 0 || ps[ps.size() - 1].second != pr[ind]) {
        ps.push_back(P(0, pr[ind]));
      }

      ps[ps.size() - 1].first++;
    }
  }

  cout << m_ / solve(0, 1) << endl;

  return 0;
}