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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
ll gcd(int n, int m) { return (n % m) ? gcd(m, n % m) : m; }
ll lcm(int n, int m) { return (ll)n * m / gcd(n, m); }
int toint(string unit) {
  int n;
  sscanf(unit.c_str(), "%x", &n);
  return n;
}
string tostr(vector<int> v) {
  string res;
  REP(i, v.size()) {
    int n = v[i];
    char s[100];
    sprintf(s, "%02X", n);
    res += string(s);
  }
  return res;
}

int main() {
  int T;
  cin >> T;
  while (T--) {
    int N;
    cin >> N;
    string s[8];
    ll len = 1;
    REP(i, N) {
      cin >> s[i];
      REP(j, s[i].size() / 2) if (s[i].substr(j * 2, 2) != "00") {
        // printf("%d/%d ", j, s[i].size() /2);
        if (len <= 1024)
          len = lcm(len, (s[i].size() / 2 / gcd(j, s[i].size() / 2)));
      }
    }
    if (len > 1024) {
      cout << "Too complex." << endl;
      continue;
    }
    vector<int> data(len);
    REP(i, len) {
      REP(j, N) {
        int l = s[j].size() / 2;
        if (i * l % len == 0)
          data[i] |= toint(s[j].substr(i * l / len * 2, 2));
      }
    }
    cout << tostr(data) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int gcd(int n, int m) { return (n % m) ? gcd(m, n % m) : m; }
int lcm(int n, int m) { return (ll)n * m / gcd(n, m); }
int toint(string unit) {
  int n;
  sscanf(unit.c_str(), "%x", &n);
  return n;
}
string tostr(vector<int> v) {
  string res;
  REP(i, v.size()) {
    int n = v[i];
    char s[100];
    sprintf(s, "%02X", n);
    res += string(s);
  }
  return res;
}

int main() {
  int T;
  cin >> T;
  while (T--) {
    int N;
    cin >> N;
    string s[8];
    int len = 1;
    REP(i, N) {
      cin >> s[i];
      REP(j, s[i].size() / 2) if (s[i].substr(j * 2, 2) != "00") {
        // printf("%d/%d ", j, s[i].size() /2);
        len = lcm(len, (s[i].size() / 2 / gcd(j, s[i].size() / 2)));
      }
    }
    if (len > 1024) {
      cout << "Too complex." << endl;
      continue;
    }
    vector<int> data(len);
    REP(i, len) {
      REP(j, N) {
        int l = s[j].size() / 2;
        if (i * l % len == 0)
          data[i] |= toint(s[j].substr(i * l / len * 2, 2));
      }
    }
    cout << tostr(data) << endl;
  }
  return 0;
}