Problem p03231 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------
ll gcd(ll m, ll n) {
  ll l;
  // 引数に０がある場合は０を返す
  if ((0 == m) || (0 == n))
    return 0;

  // ユークリッドの方法
  while (1) {
    l = m % n;
    if (l == 0)
      break;
    m = n;
    n = l;
  }
  return n;
}
ll lcm(ll m, ll n) {
  ll g = gcd(m, n);
  if (g == 0) {
    return 0;
  }
  return m * n / g;
}

void solve() {
  ll i, j, k, r, x, y;
  string S, T;
  bool exit = false;
  ll N, M;
  cin >> N >> M;
  cin >> S >> T;
  ll g = gcd(M, N);
  ll L = lcm(M, N);
  ll l = L / g;
  FOR(i, g) {
    if (S[i * l] != T[i * l]) {
      cout << "-1" << endl;
      return;
    }
  }
  cout << L << endl;
  /*cin>>N;
  vector<ll> V((N+1)*(N+1));
  FOR(x,N+1) {
          FOR(y,N+1) {
                  ll ret=1;
                  FOR(i,x) ret*=2;
                  FOR(i,y) ret*=5;
                  V.push_back(ret);
          }
  }
  sort(ALL(V));
  FORR(v,V) cout<<v<<endl;*/
}

int main(int argc, char **argv) {
  string s;
  int i;
  if (argc == 1)
    ios::sync_with_stdio(false), cin.tie(0);
  FOR(i, argc - 1) s += argv[i + 1], s += '\n';
  FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------
ll gcd(ll m, ll n) {
  ll l;
  // 引数に０がある場合は０を返す
  if ((0 == m) || (0 == n))
    return 0;

  // ユークリッドの方法
  while (1) {
    l = m % n;
    if (l == 0)
      break;
    m = n;
    n = l;
  }
  return n;
}
ll lcm(ll m, ll n) {
  ll g = gcd(m, n);
  if (g == 0) {
    return 0;
  }
  return m * n / g;
}

void solve() {
  ll i, j, k, r, x, y;
  string S, T;
  bool exit = false;
  ll N, M;
  cin >> N >> M;
  cin >> S >> T;
  ll g = gcd(M, N);
  ll L = lcm(M, N);
  ll l = L / g;
  FOR(i, g) {
    if (S[i * (N / g)] != T[i * (M / g)]) {
      cout << "-1" << endl;
      return;
    }
  }
  cout << L << endl;
  /*cin>>N;
  vector<ll> V((N+1)*(N+1));
  FOR(x,N+1) {
          FOR(y,N+1) {
                  ll ret=1;
                  FOR(i,x) ret*=2;
                  FOR(i,y) ret*=5;
                  V.push_back(ret);
          }
  }
  sort(ALL(V));
  FORR(v,V) cout<<v<<endl;*/
}

int main(int argc, char **argv) {
  string s;
  int i;
  if (argc == 1)
    ios::sync_with_stdio(false), cin.tie(0);
  FOR(i, argc - 1) s += argv[i + 1], s += '\n';
  FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}