Problem p03231 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

static const double EPS = 1e-9;
template <class T> bool INRANGE(T x, T a, T b) { return a <= x && x <= b; }
template <class T> void amin(T &a, T v) {
  if (a > v)
    a = v;
}
template <class T> void amax(T &a, T v) {
  if (a < v)
    a = v;
}
int ROUND(double x) { return (int)(x + 0.5); }
bool ISINT(double x) { return fabs(ROUND(x) - x) <= EPS; }
bool ISEQUAL(double x, double y) {
  return fabs(x - y) <= EPS * max(1.0, max(fabs(x), fabs(y)));
}
double SQSUM(double x, double y) { return x * x + y * y; }
#define PI (acos(-1))
#define ARRAY_NUM(a) (sizeof(a) / sizeof(a[0]))
#define NG (-1)
#define BIG ((int)1e9 + 10)
#define BIGLL ((ll)4e18)
#define SZ(a) ((int)(a).size())
#define SQ(a) ((a) * (a))
typedef unsigned long long ull;
typedef long long ll;

ll gcd(ll x, ll y) {
  if (x == 0)
    return y;
  if (y == 0)
    return x;

  ll t;

  while (y != 0) {
    t = x % y;
    x = y;
    y = t;
  }
  return x;
}

// 最小公倍数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

int main() {
  int n, m;
  string s, t;
  cin >> n >> m >> s >> t;

  ll gc = gcd(n, m);
  ll l = lcm(n, m);

  ll ans = l;
  for (int i = 0; i < l; ++i) {
    if (i % (l / n) == 0 && i % (l / m) == 0) {
      if (s[i / (l / n)] != t[i / (l / m)]) {
        ans = -1;
      }
    }
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

static const double EPS = 1e-9;
template <class T> bool INRANGE(T x, T a, T b) { return a <= x && x <= b; }
template <class T> void amin(T &a, T v) {
  if (a > v)
    a = v;
}
template <class T> void amax(T &a, T v) {
  if (a < v)
    a = v;
}
int ROUND(double x) { return (int)(x + 0.5); }
bool ISINT(double x) { return fabs(ROUND(x) - x) <= EPS; }
bool ISEQUAL(double x, double y) {
  return fabs(x - y) <= EPS * max(1.0, max(fabs(x), fabs(y)));
}
double SQSUM(double x, double y) { return x * x + y * y; }
#define PI (acos(-1))
#define ARRAY_NUM(a) (sizeof(a) / sizeof(a[0]))
#define NG (-1)
#define BIG ((int)1e9 + 10)
#define BIGLL ((ll)4e18)
#define SZ(a) ((int)(a).size())
#define SQ(a) ((a) * (a))
typedef unsigned long long ull;
typedef long long ll;

ll gcd(ll x, ll y) {
  if (x == 0)
    return y;
  if (y == 0)
    return x;

  ll t;

  while (y != 0) {
    t = x % y;
    x = y;
    y = t;
  }
  return x;
}

// 最小公倍数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

int main() {
  int n, m;
  string s, t;
  cin >> n >> m >> s >> t;

  ll gc = gcd(n, m);
  ll l = lcm(n, m);

  ll ans = l;
  for (ll i = 0; i < l; i += l / gc) {
    if (i % (l / n) == 0 && i % (l / m) == 0) {
      if (s[i / (l / n)] != t[i / (l / m)]) {
        ans = -1;
      }
    }
  }

  cout << ans << endl;
  return 0;
}