Problem p00282 (C++)

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

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  for (int i = 0; i < v.size(); i++)
    os << v[i] << (i + 1 < v.size() ? ", " : "");
  os << "}";
  return os;
}

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
inline bool valid(int x, int w) { return 0 <= x && x < w; }

void iostream_init() {
  ios::sync_with_stdio(false);
  cin.tie(0);
}
//}}}

const int BASE = 10000;
typedef vector<int> Num;
Num normalize(Num c) {
  int carry = 0;
  for (int i = 0; i < c.size(); i++) {
    c[i] += carry;
    carry = c[i] / BASE;
    c[i] %= BASE;
  }
  if (carry > 0)
    c.push_back(carry);
  return c;
}
Num mul(const Num &a, const Num &b) {
  int n = a.size();
  int m = b.size();
  Num c(n - 1 + m - 1 + 1);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      c[i + j] += a[i] * b[j];
    }
  }
  return normalize(c);
}
Num add(const Num &a, const Num &b) {
  Num c(max(a.size(), b.size()));
  for (int i = 0; i < c.size(); i++) {
    if (i < a.size())
      c[i] += a[i];
    if (i < b.size())
      c[i] += b[i];
  }
  return normalize(c);
}
Num pow(Num a, int b) {
  Num res = {1};
  while (b > 0) {
    if (b & 1)
      res = mul(res, a);
    a = mul(a, a);
    b /= 2;
  }
  return res;
}
void output(Num a) {
  const vector<string> unit = {"",    "Man", "Oku", "Cho", "Kei", "Gai",
                               "Jo",  "Jou", "Ko",  "Kan", "Sei", "Sai",
                               "Gok", "Ggs", "Nyt", "Fks", "Mts"};
  for (int i = a.size() - 1; i >= 0; i--) {
    if (a[i] != 0) {
      cout << a[i] << unit[i];
    }
  }
  cout << endl;
}

int main() {
  iostream_init();
  int A, B;
  while (cin >> A >> B && A > 0) {
    Num C = {A};
    output(pow(C, B));
  }
  return 0;
}

/* vim:set foldmethod=marker: */

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

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  for (int i = 0; i < v.size(); i++)
    os << v[i] << (i + 1 < v.size() ? ", " : "");
  os << "}";
  return os;
}

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
inline bool valid(int x, int w) { return 0 <= x && x < w; }

void iostream_init() {
  ios::sync_with_stdio(false);
  cin.tie(0);
}
//}}}

const int BASE = 10000;
typedef vector<int> Num;
Num normalize(Num c) {
  int carry = 0;
  for (int i = 0; i < c.size(); i++) {
    c[i] += carry;
    carry = c[i] / BASE;
    c[i] %= BASE;
  }
  if (carry > 0)
    c.push_back(carry);
  return c;
}
Num mul(const Num &a, const Num &b) {
  int n = a.size();
  int m = b.size();
  Num c(n - 1 + m - 1 + 1);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      c[i + j] += a[i] * b[j];
    }
  }
  return normalize(c);
}
Num add(const Num &a, const Num &b) {
  Num c(max(a.size(), b.size()));
  for (int i = 0; i < c.size(); i++) {
    if (i < a.size())
      c[i] += a[i];
    if (i < b.size())
      c[i] += b[i];
  }
  return normalize(c);
}
Num pow(Num a, int b) {
  Num res = {1};
  while (b > 0) {
    if (b & 1)
      res = mul(res, a);
    a = mul(a, a);
    b /= 2;
  }
  return res;
}
void output(Num a) {
  const vector<string> unit = {"",    "Man", "Oku", "Cho", "Kei", "Gai",
                               "Jo",  "Jou", "Ko",  "Kan", "Sei", "Sai",
                               "Gok", "Ggs", "Asg", "Nyt", "Fks", "Mts"};
  for (int i = a.size() - 1; i >= 0; i--) {
    if (a[i] != 0) {
      cout << a[i] << unit[i];
    }
  }
  cout << endl;
}

int main() {
  iostream_init();
  int A, B;
  while (cin >> A >> B && A > 0) {
    Num C = {A};
    output(pow(C, B));
  }
  return 0;
}

/* vim:set foldmethod=marker: */