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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-5;
const int inf = 1e8;
int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    vvi in(n);
    vi tmp;
    rep(i, n) {
      string s;
      cin >> s;
      int N = s.size();
      tmp = vi(N / 2);
      int gc = N / 2;
      rep(j, N / 2) {
        tmp[j] = (s[2 * j] - '0') * 16 + (s[2 * j + 1] - '0');
        if (j && tmp[j])
          gc = gcd(gc, j);
      }
      for (int j = 0; j < N / 2; j += gc)
        in[i].pb(tmp[j]);
    }
    int lc = 1;
    rep(i, n) {
      lc = lcm(lc, in[i].size());
      if (lc > 1024)
        break;
    }
    if (lc > 1024)
      cout << "Too complex." << endl;
    else {
      vi out(lc);
      rep(i, n) {
        int t = lc / in[i].size();
        for (int j = 0; j < lc; j += t)
          out[j] |= in[i][j / t];
      }
      rep(i, lc) printf("%02X", out[i]);
      cout << endl;
    }
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-5;
const int inf = 1e8;
int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    vvi in(n);
    vi tmp;
    rep(i, n) {
      string s;
      cin >> s;
      int N = s.size();
      tmp = vi(N / 2);
      int gc = N / 2;
      rep(j, N / 2) {
        tmp[j] = (s[2 * j] - '0') * 16 + (s[2 * j + 1] - '0');
        if (j && tmp[j])
          gc = gcd(gc, j);
      }
      for (int j = 0; j < N / 2; j += gc)
        in[i].pb(tmp[j]);
    }
    int lc = 1;
    rep(i, n) lc = lcm(lc, in[i].size());
    if (lc > 1024)
      cout << "Too complex" << endl;
    else {
      vi out(lc);
      rep(i, n) {
        int t = lc / in[i].size();
        for (int j = 0; j < lc; j += t)
          out[j] |= in[i][j / t];
      }
      rep(i, lc) printf("%02X", out[i]);
      cout << endl;
    }
  }
}