Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define rep(i, n) for (long long int(i) = 0; (i) < (int)(n); (i)++)
#define rrep(i, a, b) for (long long int i = (a); i < (b); i++)
#define rrrep(i, a, b) for (long long int i = (a); i >= (b); i--)
#define all(v) (v).begin(), (v).end()
#define pb(q) push_back(q)
#define Size(n) (n).size()
#define Cout(x) cout << (x) << endl

typedef long long ll;

using namespace std;

const int INF = 1e9, MOD = 1e9 + 7, ohara = 1e6;
const ll LINF = 1e18;
long long int n, cnt = 0, ans = 0, a[ohara], b[ohara], c, d, cmp, cmpp, m, h, w,
                 x, y, sum = 0, pos;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
string alph("abcdefghijklmnopqrstuvwxyz"), s, t;
bool fl;
struct edge {
  int to, cost;
};

//-------------------------↓↓↓↓↓↓------------------------

long long gcd(long long aaa, long long bbb) {
  if (bbb == 0) {
    return aaa;
  }
  return gcd(bbb, aaa % bbb);
}

long long lcm(long long aaa, long long bbb) {
  long long g = gcd(aaa, bbb);
  return aaa / g * bbb;
}

int main(void) {

  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> m;
  cin >> s;
  cin >> t;
  pos = lcm(n, m);
  ll cntt = 1;
  cnt = 1;
  a[0] = 1;
  b[0] = 1;
  ll kake = 1;
  while (1) {
    a[cnt++] = kake * pos / n + 1;
    kake++;
    if (kake == n)
      break;
  }
  kake = 1;
  while (1) {
    b[cntt++] = kake * pos / m + 1;
    kake++;
    if (kake == m)
      break;
  }
  map<ll, ll> mp;
  map<ll, ll> mpp;
  rep(i, cnt) mp[a[i]] = i, mpp[a[i]]++;
  rep(i, cntt) {
    // Cout(b[i]);
    if (mpp[b[i]] > 0) {
      if (s[mp[b[i]]] != t[i]) {
        Cout(-1);
        return 0;
      }
    }
  }
  Cout(pos);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (long long int(i) = 0; (i) < (int)(n); (i)++)
#define rrep(i, a, b) for (long long int i = (a); i < (b); i++)
#define rrrep(i, a, b) for (long long int i = (a); i >= (b); i--)
#define all(v) (v).begin(), (v).end()
#define pb(q) push_back(q)
#define Size(n) (n).size()
#define Cout(x) cout << (x) << endl

typedef long long ll;

using namespace std;

const int INF = 1e9, MOD = 1e9 + 7, ohara = 1e6;
const ll LINF = 1e18;
long long int n, cnt = 0, ans = 0, a[ohara], b[ohara], c, d, cmp, cmpp, m, h, w,
                 x, y, sum = 0, pos;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
string alph("abcdefghijklmnopqrstuvwxyz"), s, t;
bool fl;
struct edge {
  int to, cost;
};

//-------------------------↓↓↓↓↓↓------------------------

long long gcd(long long aaa, long long bbb) {
  if (bbb == 0) {
    return aaa;
  }
  return gcd(bbb, aaa % bbb);
}

long long lcm(long long aaa, long long bbb) {
  long long g = gcd(aaa, bbb);
  return aaa / g * bbb;
}

int main(void) {

  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> m;
  cin >> s;
  cin >> t;
  if (n == 1 || m == 1) {
    if (s[0] == t[0])
      Cout(max(n, m));
    else
      Cout(-1);
    return 0;
  }
  pos = lcm(n, m);
  ll cntt = 1;
  cnt = 1;
  a[0] = 1;
  b[0] = 1;
  ll kake = 1;
  while (1) {
    a[cnt++] = kake * pos / n + 1;
    kake++;
    if (kake == n)
      break;
  }
  kake = 1;
  while (1) {
    b[cntt++] = kake * pos / m + 1;
    kake++;
    if (kake == m)
      break;
  }
  map<ll, ll> mp;
  map<ll, ll> mpp;
  rep(i, cnt) mp[a[i]] = i, mpp[a[i]]++;
  rep(i, cntt) {
    // Cout(b[i]);
    if (mpp[b[i]] > 0) {
      if (s[mp[b[i]]] != t[i]) {
        Cout(-1);
        return 0;
      }
    }
  }
  Cout(pos);

  return 0;
}