Problem p03231 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
#define int long long
#define end                                                                    \
  getchar();                                                                   \
  getchar();                                                                   \
  return 0
int inf = 1000000000;
using namespace std;
int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
int mypow(int x, int y) {
  if (y == 0)
    return 1;
  if (y % 2)
    return mypow(x, y - 1) * x;
  else
    return mypow(x, y / 2) * mypow(x, y / 2);
}
int Max(int x, int y) {
  if (x > y)
    return x;
  else
    return y;
}
int Min(int x, int y) {
  if (x < y)
    return x;
  else
    return y;
}
int kai(int x) {
  int cnt = 1;
  if (x == 0)
    return 1;
  cnt = kai(x - 1);
  return x * cnt;
}
int prime(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return n != 1;
}
void prime_factor(int n) {
  int cnt = 1;
  map<int, int> res;
  int m = n;
  for (int i = 2; i <= n; i++) {
    if (prime(i)) {
      while (m % i == 0) {
        ++res[i];
        m /= i;
      }
    }
  }
  cout << n << ":" << endl;
  for (int i = 2; i <= n; i++) {
    if (res[i]) {
      cout << i << " " << res[i] << endl;
    }
  }
  cout << endl;
}
/*Library End!*/
int n, m;
string s, t;
signed main() {
  cin >> n >> m >> s >> t;
  int siz = lcm(n, m);
  for (int i = 0; i < siz; i++) {
    if (i % (siz / n) == 0 && i % (siz / m) == 0) {
      if (s[i / (siz / n)] != t[i / (siz / m)]) {
        cout << -1 << endl;
        end;
      }
    }
  }
  cout << siz << endl;
  end;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
#define int long long
#define end                                                                    \
  getchar();                                                                   \
  getchar();                                                                   \
  return 0
int inf = 1000000000;
using namespace std;
int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
int mypow(int x, int y) {
  if (y == 0)
    return 1;
  if (y % 2)
    return mypow(x, y - 1) * x;
  else
    return mypow(x, y / 2) * mypow(x, y / 2);
}
int Max(int x, int y) {
  if (x > y)
    return x;
  else
    return y;
}
int Min(int x, int y) {
  if (x < y)
    return x;
  else
    return y;
}
int kai(int x) {
  int cnt = 1;
  if (x == 0)
    return 1;
  cnt = kai(x - 1);
  return x * cnt;
}
int prime(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return n != 1;
}
void prime_factor(int n) {
  int cnt = 1;
  map<int, int> res;
  int m = n;
  for (int i = 2; i <= n; i++) {
    if (prime(i)) {
      while (m % i == 0) {
        ++res[i];
        m /= i;
      }
    }
  }
  cout << n << ":" << endl;
  for (int i = 2; i <= n; i++) {
    if (res[i]) {
      cout << i << " " << res[i] << endl;
    }
  }
  cout << endl;
}
/*Library End!*/
int n, m;
string s, t;
signed main() {
  cin >> n >> m >> s >> t;
  int siz = lcm(n, m);
  for (int i = 0; i < siz; i += Min(siz / n, siz / m)) {
    if (i % (siz / n) == 0 && i % (siz / m) == 0) {
      if (s[i / (siz / n)] != t[i / (siz / m)]) {
        cout << -1 << endl;
        end;
      }
    }
  }
  cout << siz << endl;
  end;
}