Problem p03231 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, j, n) for (ll i = j; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define INF (1000000000)
#define MOD (1000000007)
#define MAX (100000)
#define pii pair<int, int>

/////////////////////////////////////////////////////////

class XY {
public:
  ll x, y;
  XY() { x = y = 0; }
  XY(ll u, ll v) {
    x = u;
    y = v;
  }
};

template <typename T1, typename T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

template <typename T1, typename T2> ll mypow(T1 a, T2 n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return a;
  if (n % 2)
    return a * mypow(a, n - 1);
  ll tmp = mypow(a, n / 2);
  return tmp * tmp;
}

template <typename T> int BS(vector<T> V, int left, int right, T key) {
  int mid = (left + right) / 2;
  if (V[mid] <= key)
    left = mid;
  else
    right = mid;
  if (right - mid == 1)
    return left;
  else
    return BS(V, left, right, key);
}

ll comb(ll n, ll r) {
  ll res = 1;
  rep(i, 0, r) {
    res *= n - i;
    res /= i + 1;
  }
  return res;
}

template <typename T> T euclid(T a, T b) {
  if (b == 0)
    return a;
  else
    return euclid(b, a % b);
}

/////////////////////////////////////////////////////////

void Main() {
  ll N, M;
  cin >> N >> M;
  string S, T;
  cin >> S >> T;
  ll L = (N * M) / euclid(N, M);
  string X = "";
  int a = 0, b = 0;
  rep(i, 1, L + 1) {
    char tmp;
    if (i == (L / N) * a + 1 && i == (L / M) * b + 1) {
      if (S[a] == T[b])
        tmp = S[i / (L / N)];
      else {
        cout << -1 << endl;
        return;
      }
      a++;
      b++;
    } else if (i == (L / N) * a + 1) {
      tmp = S[i / (L / N)];
      a++;
    } else if (i == (L / M) * b + 1) {
      tmp = T[i / (L / M)];
      b++;
    } else
      tmp = 'a';
    X.push_back(tmp);
  }
  cout << L << endl;
}

/////////////////////////////////////////////////////////

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << std::fixed << std::setprecision(15);

  Main();
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, j, n) for (ll i = j; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define INF (1000000000)
#define MOD (1000000007)
#define MAX (100000)
#define pii pair<int, int>

/////////////////////////////////////////////////////////

class XY {
public:
  ll x, y;
  XY() { x = y = 0; }
  XY(ll u, ll v) {
    x = u;
    y = v;
  }
};

template <typename T1, typename T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

template <typename T1, typename T2> ll mypow(T1 a, T2 n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return a;
  if (n % 2)
    return a * mypow(a, n - 1);
  ll tmp = mypow(a, n / 2);
  return tmp * tmp;
}

template <typename T> int BS(vector<T> V, int left, int right, T key) {
  int mid = (left + right) / 2;
  if (V[mid] <= key)
    left = mid;
  else
    right = mid;
  if (right - mid == 1)
    return left;
  else
    return BS(V, left, right, key);
}

ll comb(ll n, ll r) {
  ll res = 1;
  rep(i, 0, r) {
    res *= n - i;
    res /= i + 1;
  }
  return res;
}

template <typename T> T euclid(T a, T b) {
  if (b == 0)
    return a;
  else
    return euclid(b, a % b);
}

/////////////////////////////////////////////////////////

void Main() {
  ll N, M;
  cin >> N >> M;
  string S, T;
  cin >> S >> T;
  ll L = (N * M) / euclid(N, M);
  ll l = ((L / N) * (L / M)) / euclid((L / N), (L / M));
  rep(i, 0, L / l) {
    if (S[(l * i) / (L / N)] != T[(l * i) / (L / M)])
      L = -1;
  }
  cout << L << endl;
}

/////////////////////////////////////////////////////////

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << std::fixed << std::setprecision(15);

  Main();
}