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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef vector<int> vi;

inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b) { return a / gcd(a, b) * b; }
inline int hex(char a) { return isdigit(a) ? (a - '0') : (a - 'A'); }
inline char inv_hex(int a) { return (a < 10) ? (a + '0') : (a - 10 + 'A'); }

inline vi reduce(vi a) {
  int n = a.size();
  for (int i = n; i >= 2; i--) {
    if (n % i)
      continue;
    bool f = true;
    rep(j, n) {
      if (j % i && a[j]) {
        f = false;
        break;
      }
    }
    if (f) {
      int k = n / i;
      vi res(k);
      for (int j = 0; j < k; j++)
        res[j] = a[j * i];
      return res;
    }
  }
  return a;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int casenum;
  cin >> casenum;
  while (casenum--) {
    int n;
    cin >> n;
    int len = 1;
    vector<vi> rhythm(n);
    rep(i, n) {
      string s;
      cin >> s;
      if (len <= 1024) {
        vi tmp(s.size() / 2);
        rep(j, s.size() / 2) {
          tmp[j] = hex(s[2 * j]) * 16 + hex(s[2 * j + 1]);
        }
        rhythm[i] = reduce(tmp);
        len = lcm(len, rhythm[i].size());
      }
    }

    if (len > 1024)
      cout << "Too complex." << endl;
    else {
      vi res(len, 0);
      rep(i, n) {
        int k = len / rhythm[i].size();
        rep(j, rhythm[i].size()) res[j * k] |= rhythm[i][j];
      }
      rep(i, len) { cout << inv_hex(res[i] / 16) << inv_hex(res[i] % 16); }
      cout << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef vector<int> vi;

inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b) { return a / gcd(a, b) * b; }
inline int hex(char a) { return isdigit(a) ? (a - '0') : (a - 'A'); }
inline char inv_hex(int a) { return (a < 10) ? (a + '0') : (a - 10 + 'A'); }

inline vi reduce(vi a) {
  int n = a.size();
  for (int i = n; i >= 1; i--) {
    if (n % i)
      continue;
    bool f = true;
    rep(j, n) {
      if (j % i && a[j]) {
        f = false;
        break;
      }
    }
    if (f) {
      int k = n / i;
      vi res(k);
      for (int j = 0; j < k; j++)
        res[j] = a[j * i];
      return res;
    }
  }
  return a;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int casenum;
  cin >> casenum;
  while (casenum--) {
    int n;
    cin >> n;
    int len = 1;
    vector<vi> rhythm(n);
    rep(i, n) {
      string s;
      cin >> s;
      if (len < 1024) {
        vi tmp(s.size() / 2);
        rep(j, s.size() / 2) {
          tmp[j] = hex(s[2 * j]) * 16 + hex(s[2 * j + 1]);
        }
        rhythm[i] = reduce(tmp);
        len = lcm(len, rhythm[i].size());
      }
    }

    if (len > 1024)
      cout << "Too complex." << endl;
    else {
      vi res(len, 0);
      rep(i, n) {
        int k = len / rhythm[i].size();
        rep(j, rhythm[i].size()) res[j * k] |= rhythm[i][j];
      }
      rep(i, len) { cout << inv_hex(res[i] / 16) << inv_hex(res[i] % 16); }
      cout << endl;
    }
  }
}