Problem p03241 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for (ll i = 0; i < (ll)(n); ++i)

vector<pair<ll, ll>> prime_factor(int64_t n) {
  vector<pair<ll, ll>> ret;
  for (int64_t i = 2; i * i <= n; i++) {
    if (n % i == 0)
      ret.push_back(pair<ll, ll>(i, 0));
    while (n % i == 0) {
      ret.back().second++;
      n /= i;
    }
  }
  if (n != 1)
    ret.push_back(pair<ll, ll>(n, 1));
  return ret;
}

int main() {
  ll n, m;
  cin >> n >> m;
  auto primes = prime_factor(m);
  vector<vector<ll>> dp(primes.size() + 1, vector<ll>(n + 1, -1));
  dp[0][1] = 0;
  REP(i, primes.size()) {
    REP(j, n) if (dp[i][j] != -1) dp[i + 1][j] = 0;
    dp[i + 1][n] = dp[i][n];

    REP(j, n) if (dp[i][j] != -1) {
      if (j * primes[i].first >= n) {
        if (dp[i + 1][n] == -1) {
          dp[i + 1][n] = j * primes[i].first;
        } else {
          dp[i + 1][n] = min(dp[i + 1][n], j * primes[i].first);
        }
      } else if (dp[i + 1][j * primes[i].first] != 0) {
        dp[i + 1][j * primes[i].first] = 1;
      }
    }
    REP(j, n) if (dp[i + 1][j] != -1) {
      if (dp[i + 1][j] >= primes[i].second)
        continue;
      if (j * primes[i].first >= n) {
        if (dp[i + 1][n] == -1) {
          dp[i + 1][n] = j * primes[i].first;
        } else {
          dp[i + 1][n] = min(dp[i + 1][n], j * primes[i].first);
        }
      } else if (dp[i + 1][j] + 1 < dp[i + 1][j * primes[i].first]) {
        dp[i + 1][j * primes[i].first] = dp[i + 1][j] + 1;
      } else if (dp[i + 1][j * primes[i].first] == -1) {
        dp[i + 1][j * primes[i].first] = dp[i + 1][j] + 1;
      }
    }
  }
  cout << m / dp[primes.size()][n] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for (ll i = 0; i < (ll)(n); ++i)

vector<pair<ll, ll>> prime_factor(int64_t n) {
  vector<pair<ll, ll>> ret;
  for (int64_t i = 2; i * i <= n; i++) {
    if (n % i == 0)
      ret.push_back(pair<ll, ll>(i, 0));
    while (n % i == 0) {
      ret.back().second++;
      n /= i;
    }
  }
  if (n != 1)
    ret.push_back(pair<ll, ll>(n, 1));
  return ret;
}

int main() {
  ll n, m;
  cin >> n >> m;
  auto primes = prime_factor(m);
  vector<vector<ll>> dp(primes.size() + 1, vector<ll>(n + 1, -1));
  dp[0][1] = 1;
  REP(i, primes.size()) {
    REP(j, n) if (dp[i][j] != -1) dp[i + 1][j] = 0;
    dp[i + 1][n] = dp[i][n];

    REP(j, n) if (dp[i][j] != -1) {
      if (j * primes[i].first >= n) {
        if (dp[i + 1][n] == -1) {
          dp[i + 1][n] = j * primes[i].first;
        } else {
          dp[i + 1][n] = min(dp[i + 1][n], j * primes[i].first);
        }
      } else if (dp[i + 1][j * primes[i].first] != 0) {
        dp[i + 1][j * primes[i].first] = 1;
      }
    }
    REP(j, n) if (dp[i + 1][j] != -1) {
      if (dp[i + 1][j] >= primes[i].second)
        continue;
      if (j * primes[i].first >= n) {
        if (dp[i + 1][n] == -1) {
          dp[i + 1][n] = j * primes[i].first;
        } else {
          dp[i + 1][n] = min(dp[i + 1][n], j * primes[i].first);
        }
      } else if (dp[i + 1][j] + 1 < dp[i + 1][j * primes[i].first]) {
        dp[i + 1][j * primes[i].first] = dp[i + 1][j] + 1;
      } else if (dp[i + 1][j * primes[i].first] == -1) {
        dp[i + 1][j * primes[i].first] = dp[i + 1][j] + 1;
      }
    }
  }
  cout << m / dp[primes.size()][n] << endl;
}