Problem p00646 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
ll b_pow(ll x, ll n) {
  return n ? n == 1 ? x : b_pow(x, n >> 1) * b_pow(x, (n >> 1) + (n & 1)) : 1;
}

set<pair<ll, ll>> c;

void func(int pa, int pb, vector<pair<long long, int>> &a,
          vector<pair<long long, int>> &b, long long A, long long B) {
  if (a.size() == pa && b.size() == pb) {
    c.insert(make_pair(min(A, B), max(A, B)));
    return;
  }
  // cout << pb << "(" << a.size() << ")" << pb << "(" << b.size() << ")" << A
  // << " " << B << endl;

  if (b.size() == pb || (a.size() != pa && pa <= pb)) {
    for (int i = 0; i <= a[pa].second; i++) {
      // cout <<"[" << A << "]" << endl;
      func(pa + 1, pb, a, b, A, B);
      A *= a[pa].first;
    }
  } else {
    for (int i = 0; i <= b[pb].second; i++) {
      // cout <<"[" << B << "]" << endl;
      func(pa, pb + 1, a, b, A, B);
      B *= b[pb].first;
    }
  }
}
int f(long long n) {
  c.clear();
  map<long long, int> v;
  for (long long i = 2; i * i <= n; i++)
    while (n % i == 0)
      v[i]++, n /= i;
  if (n != 1)
    v[n]++;
  vector<pair<long long, int>> q;
  for (map<long long, int>::iterator it = v.begin(); it != v.end(); ++it)
    q.push_back(*it);
  long long ans = 0;
  for (int i = 0; i < (1 << q.size()); i++) {
    long long a = 1, b = 1;
    vector<pair<long long, int>> x, y;
    for (int j = 0; j < q.size(); j++) {
      if (i >> j & 1)
        a *= b_pow(q[j].first, q[j].second), y.push_back(q[j]);
      else
        b *= b_pow(q[j].first, q[j].second), x.push_back(q[j]);
    }
    func(0, 0, x, y, a, b);
  }
  return c.size();
}
// 2 3
// 6 __ 1 [1 , 2 , 3 , 2*3]
// 2*[1,3] __ 3*[1,2]
int main() {
  long long n;
  while (cin >> n && n) {
    cout << f(n) << endl;
  }
}

Error: -7

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (long long i = 0; i < n; i++)
#define INF (1ll << 32)

long long land[200][200], ship[200][200];

int main() {
  long long n, m;
  while (cin >> n >> m, n) {
    rep(i, 200) rep(j, 200) land[i][j] = ship[i][j] = INF;
    rep(i, 200) land[i][i] = ship[i][i] = 0;

    rep(i, m) {
      string l;
      long long a, b, c;
      cin >> a >> b >> c >> l;
      a--, b--;
      if (l == "L")
        land[a][b] = land[b][a] = min(c, land[a][b]);
      else
        ship[a][b] = ship[b][a] = min(c, ship[a][b]);
    }
    rep(k, n) rep(i, n) rep(j, n) land[i][j] =
        min(land[i][j], land[i][k] + land[k][j]);
    rep(k, n) rep(i, n) rep(j, n) ship[i][j] =
        min(ship[i][j], ship[i][k] + ship[k][j]);

    long long r;
    cin >> r;
    long long R[1000];
    rep(i, r) cin >> R[i], R[i]--;

    long long dp[2][200];
    rep(j, 2) rep(k, 200) dp[j][k] = INF;
    dp[0][R[0]] = 0;

    rep(bit, r - 1) {
      rep(cur, n) {
        rep(next, n) {
          long long newCost = dp[bit % 2][cur] + land[R[bit]][cur] +
                              ship[cur][next] + land[next][R[bit + 1]];
          dp[(bit + 1) % 2][next] = min(dp[(bit + 1) % 2][next], newCost);
        }
      }
      rep(i, 2) rep(j, 200) dp[bit % 2][j] = INF;
    }
    long long ret = INF;
    rep(i, n) { ret = min(dp[(r - 1) % 2][i], ret); }
    cout << ret << endl;
  }
}