prithivMLmods/Coder-Stat huggingface dataset:
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>

using namespace std;

long long gcd(long long x, long long y) {
  while (y != 0) {
    long long tmp = x;
    x = y;
    y = tmp % y;
  }
  return x;
}

long long lcm(long long x, long long y) { return x / gcd(x, y) * y; }

char s[100001];
char t[100001];

int main() {
  long long n, m;
  scanf("%lld %lld %s %s", &n, &m, s, t);
  long long x = lcm(n, m);
  long long y = lcm(x / n, x / m);
  for (int i = 0; i < x; i += y) {
    if (s[i / (x / n)] != t[i / (x / m)]) {
      puts("-1");
      return 0;
    }
  }
  printf("%lld\n", x);
}


Fixed submission:
#include <algorithm>
#include <iostream>

using namespace std;

long long gcd(long long x, long long y) {
  while (y != 0) {
    long long tmp = x;
    x = y;
    y = tmp % y;
  }
  return x;
}

long long lcm(long long x, long long y) { return x / gcd(x, y) * y; }

char s[100001];
char t[100001];

int main() {
  long long n, m;
  scanf("%lld %lld %s %s", &n, &m, s, t);
  long long x = lcm(n, m);
  long long y = lcm(x / n, x / m);
  for (long long i = 0; i < x; i += y) {
    if (s[i / (x / n)] != t[i / (x / m)]) {
      puts("-1");
      return 0;
    }
  }
  printf("%lld\n", x);
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < n; i++)
#include <bits/stdc++.h>
#include <numeric>
using namespace std;
typedef long long ll;
const int mod = 1e+9 + 7;

int main() {
  ll N, M;
  cin >> N >> M;
  string S, T;
  cin >> S >> T;
  ll lcm = N / __gcd(N, M) * M;
  ll g = __gcd(N, M);

  /*vector<char>s(lcm,' ');
  vector<char>t(lcm,' ');
  ll cnts=0,cntt=0;
  rep(i,lcm){
      if(i%(lcm/N)==0&&cnts<=N-1){
          s[i]=S[cnts];
          cnts++;
      }
      if(i%(lcm/M)==0&&cntt<=M-1){
          t[i]=T[cntt];
          cntt++;
      }
       if(s[i]!=' '&&t[i]!=' '){
          if(s[i]!=t[i]){
              cout<<-1<<endl;
              return 0;
          }
      }
      if(cnts>=N&&cntt>=M)break;

  }
  cout<<lcm<<endl;*/
  for (int i = 0, j = 0; i < max(N, M); i += N / g, j += M / g) {
    if (S[i] != T[j]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << lcm << endl;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < n; i++)
#include <bits/stdc++.h>
#include <numeric>
using namespace std;
typedef long long ll;
const int mod = 1e+9 + 7;

int main() {
  ll N, M;
  cin >> N >> M;
  string S, T;
  cin >> S >> T;
  ll lcm = N / __gcd(N, M) * M;
  ll g = __gcd(N, M);

  /*vector<char>s(lcm,' ');
  vector<char>t(lcm,' ');
  ll cnts=0,cntt=0;
  rep(i,lcm){
      if(i%(lcm/N)==0&&cnts<=N-1){
          s[i]=S[cnts];
          cnts++;
      }
      if(i%(lcm/M)==0&&cntt<=M-1){
          t[i]=T[cntt];
          cntt++;
      }
       if(s[i]!=' '&&t[i]!=' '){
          if(s[i]!=t[i]){
              cout<<-1<<endl;
              return 0;
          }
      }
      if(cnts>=N&&cntt>=M)break;

  }
  cout<<lcm<<endl;*/
  for (int i = 0, j = 0; j < M; i += N / g, j += M / g) {
    if (S[i] != T[j]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << lcm << endl;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long N, M;
  string S, T;
  cin >> N >> M >> S >> T;
  long L = (N * M) / __gcd(N, M);
  vector<char> data(L, '0');
  for (int i = 0; i < N; i++) {
    data.at(i * L / N) = S.at(i);
  }
  int flag = 1;
  for (int i = 0; i < M; i++) {
    if (data.at(i * L / M) != T.at(i) && data.at(i * L / M) != '0') {
      flag = 0;
      break;
    }
  }
  cout << (flag ? L : -1) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long N, M;
  string S, T;
  cin >> N >> M >> S >> T;
  long L = (N * M) / __gcd(N, M);
  long npl = 0, mpl = 0, flag = 1;
  while (npl != N && mpl != M) {
    if (npl * L / N == mpl * L / M) {
      if (S.at(npl) != T.at(mpl)) {
        flag = 0;
        break;
      }
      npl++;
      mpl++;
    } else if (npl * L / N > mpl * L / M) {
      mpl++;
    } else {
      npl++;
    }
  }
  cout << (flag ? L : -1) << endl;
  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <math.h>
using namespace std;

const int MAXN = 1e7 + 10;
int N, M;
char S[MAXN], T[MAXN];

inline bool check() {
  register int i, j;
  int K = 1ll * N * M / __gcd(N, M);
  int n = K / N, m = K / M;
  swap(n, m);
  for (i = 1, j = 1; i <= N && j <= M; i += n, j += m)
    if (S[i] != T[j])
      return false;
  return true;
}

int main() {
  scanf("%d%d", &N, &M);
  scanf("%s%s", S + 1, T + 1);
  printf("%lld\n",
         N != 0 && M != 0 && !check() ? -1ll : 1ll * N * M / __gcd(N, M));
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <math.h>
using namespace std;

const int MAXN = 1e7 + 10;
int N, M;
char S[MAXN], T[MAXN];

inline bool check() {
  register int i, j;
  long long K = 1ll * N * M / __gcd(N, M);
  long long n = K / N, m = K / M;
  swap(n, m);
  for (i = 1, j = 1; i <= N && j <= M; i += n, j += m)
    if (S[i] != T[j])
      return false;
  return true;
}

int main() {
  scanf("%d%d", &N, &M);
  scanf("%s%s", S + 1, T + 1);
  printf("%lld\n",
         N != 0 && M != 0 && !check() ? -1ll : 1ll * N * M / __gcd(N, M));
  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
using namespace std;
long long gcd(long long a, long long b) {
  long long t = 1;
  while (b) {
    t = b;
    b = a % b;
    a = t;
  }
  return t;
}
int main() {
  long long n, m;
  string s, t;
  while (scanf("%lld%lld", &n, &m) != EOF) {
    cin >> s >> t;
    long long L = n * m / gcd(n, m);
    if (n >= m)
      for (long long i = 1; i <= m; i++)
        if ((i - 1) * (L / m) % (L / n) == 0 &&
            s[(i - 1) * (L / m) / (L / n)] != t[i - 1]) {
          printf("-1\n");
          return 0;
        }
    if (n < m)
      for (long long i = 1; i <= n; i++)
        if ((i - 1) * (L / n) % (L % m) == 0 &&
            s[i - 1] != t[(i - 1) * (L / n) / (L / m)]) {
          printf("-1\n");
          return 0;
        }
    printf("%lld\n", L);
  }
  return 0;
}


Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;
long long gcd(long long a, long long b) {
  long long t = 1;
  while (b) {
    t = b;
    b = a % b;
    a = t;
  }
  return t;
}
int main() {
  long long n, m;
  string s, t;
  while (scanf("%lld%lld", &n, &m) != EOF) {
    cin >> s >> t;
    long long L = n * m / gcd(n, m);
    if (n >= m)
      for (long long i = 1; i <= m; i++)
        if ((i - 1) * (L / m) % (L / n) == 0 &&
            s[(i - 1) * (L / m) / (L / n)] != t[i - 1]) {
          printf("-1\n");
          return 0;
        }
    if (n < m)
      for (long long i = 1; i <= n; i++)
        if ((i - 1) * (L / n) % (L / m) == 0 &&
            s[i - 1] != t[(i - 1) * (L / n) / (L / m)]) {
          printf("-1\n");
          return 0;
        }
    printf("%lld\n", L);
  }
  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <map>
#define int long long
#define RI register int
#define CI const int &
using namespace std;
const int N = 100005;
int n, m, l;
char s[N], t[N];
map<int, char> res;
inline int gcd(CI n, CI m) { return m ? gcd(m, n % m) : n; }
signed main() {
  RI i;
  scanf("%lld%lld%s%s", &n, &m, s + 1, t + 1);
  l = n * m / gcd(n, m);
  for (i = 1; i <= l; i += l / n)
    res[i] = s[i];
  for (i = 1; i <= l; i += l / m)
    if (res.count(i) && res[i] != t[i])
      return puts("-1"), 0;
  return printf("%lld", l), 0;
}

Fixed submission:
#include <cstdio>
#include <map>
#define int long long
#define RI register int
#define CI const int &
using namespace std;
const int N = 100005;
int n, m, l;
char s[N], t[N];
map<int, char> res;
inline int gcd(CI n, CI m) { return m ? gcd(m, n % m) : n; }
signed main() {
  RI i;
  scanf("%lld%lld%s%s", &n, &m, s + 1, t + 1);
  l = n * m / gcd(n, m);
  for (i = 1; i <= n; ++i)
    res[(i - 1) * (l / n) + 1] = s[i];
  for (i = 1; i <= m; ++i)
    if (res.count((i - 1) * (l / m) + 1) && res[(i - 1) * (l / m) + 1] != t[i])
      return puts("-1"), 0;
  return printf("%lld", l), 0;
}
Problem p03231 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define INF 1000000000000000
using namespace std;
typedef long long ll;
typedef double db;
typedef string str;

ll lcm(ll a, ll b) {
  ll gcd = 1;
  for (ll i = 1; i <= min(a, b); i++) {
    if (a % i == 0 && b % i == 0) {
      gcd = i;
    }
  }
  return a * b / gcd;
}

int main() {
  ll n, m;
  cin >> n >> m;
  str s, t;
  cin >> s >> t;
  ll l = lcm(n, m);
  REP(i, l) {
    if (i % (l / n) == 0 && i % (l / m) == 0) {
      if (s[i / (l / n)] != t[i / (l / m)]) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  cout << l << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define INF 1000000000000000
using namespace std;
typedef long long ll;
typedef double db;
typedef string str;

ll lcm(ll a, ll b) {
  ll gcd = 1;
  for (ll i = 1; i <= min(a, b); i++) {
    if (a % i == 0 && b % i == 0) {
      gcd = i;
    }
  }
  return a * b / gcd;
}

int main() {
  ll n, m;
  cin >> n >> m;
  str s, t;
  cin >> s >> t;
  ll l = lcm(n, m);
  ll l_step = lcm(l / n, l / m);
  REP(i, l / l_step) {
    if (s[i * l_step / (l / n)] != t[i * l_step / (l / m)]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << l << endl;
  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <numeric>

typedef long long int ll;
typedef unsigned long long int ull;
#define rep(i, n) for (auto i = 0; i < (n); i++)

using namespace std;

ll gcd(ll m, ll n) {
  if (m == 0)
    return 0;
  if (n == 0)
    return 0;

  while (m != n) {
    if (m > n)
      m = m - n;
    else
      n = n - m;
  }
  return m;
}

ll lcm(ll m, ll n) {
  if (m == 0)
    return 0;
  if (n == 0)
    return 0;

  return ((m / gcd(m, n)) * n);
}

bool solve(string s, string t, ll l) {
  ll n = s.size();
  ll m = t.size();

  ll nd = (l / n);
  ll md = (l / m);

  ll nx = 0;
  ll mx = 0;
  while (nx < l - nd && mx < l - md) {
    if (nx == mx) {
      if (s[nx / nd] != t[mx / md])
        return false;
      nx += nd;
      mx += md;
      continue;
    }
    if (nx < mx)
      nx += nd;
    else
      mx += md;
  }

  return true;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, m;
  string s, t;
  cin >> n >> m;
  cin >> s >> t;

  if (s[0] != t[0]) {
    cout << -1 << endl;
    return 0;
  }

  if (n % m == 0 || m % n == 0) {
    int minn = max(n, m);
    bool res = solve(s, t, minn);
    if (res)
      cout << minn << endl;
    else
      cout << -1 << endl;
    return 0;
  }

  ll minn = lcm(n, m);
  int INF = max(n, m);
  for (int i = 1; i <= INF; i++) {
    bool res = solve(s, t, i * minn);
    if (res) {
      cout << i * minn << endl;
      return 0;
    }
  }

  cout << -1 << endl;

  return 0;
}


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

typedef long long int ll;
typedef unsigned long long int ull;
#define rep(i, n) for (auto i = 0; i < (n); i++)

using namespace std;

ll gcd(ll m, ll n) {
  if (m == 0)
    return 0;
  if (n == 0)
    return 0;

  while (m != n) {
    if (m > n)
      m = m - n;
    else
      n = n - m;
  }
  return m;
}

ll lcm(ll m, ll n) {
  if (m == 0)
    return 0;
  if (n == 0)
    return 0;

  return ((m / gcd(m, n)) * n);
}

bool solve(string s, string t, ll l) {
  ll n = s.size();
  ll m = t.size();

  ll nd = (l / n);
  ll md = (l / m);

  ll nx = 0;
  ll mx = 0;
  while (nx < l - nd && mx < l - md) {
    if (nx == mx) {
      if (s[nx / nd] != t[mx / md])
        return false;
      nx += nd;
      mx += md;
      continue;
    }
    if (nx < mx)
      nx += nd;
    else
      mx += md;
  }

  return true;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, m;
  string s, t;
  cin >> n >> m;
  cin >> s >> t;

  if (s[0] != t[0]) {
    cout << -1 << endl;
    return 0;
  }

  if (n % m == 0 || m % n == 0) {
    int minn = max(n, m);
    bool res = solve(s, t, minn);
    if (res)
      cout << minn << endl;
    else
      cout << -1 << endl;
    return 0;
  }

  ll minn = lcm(n, m);
  bool res = solve(s, t, minn);
  if (res) {
    cout << minn << endl;
    return 0;
  }

  cout << -1 << endl;

  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
#define rep(i, a, b) for (long long i = a; i < b; ++i)
#define rrep(i, a, b) for (int i = a; i >= b; --i)
#define yesno(flag)                                                            \
  if (flag)                                                                    \
    cout << "Yes" << endl;                                                     \
  else                                                                         \
    cout << "No" << endl;
using namespace std;
const int INF = 1000000000LL;
const ll r197 = 1000000007;
typedef pair<int, int> P;
typedef pair<int, P> Pa;

int main() {
  ll n, m;
  string s, t;
  cin >> n >> m;
  cin >> s >> t;
  ll a = n, b = m;
  while (1) {
    if (a < b) {
      ll d = a;
      a = b;
      b = d;
    }
    if (b == 0)
      break;
    a %= b;
  }
  bool ans = false;
  ll x = 0, y = 0;
  while (1) {
    if (x >= n || y >= m)
      break;
    if (x * (m / a) == y * (n / a)) {
      if (s[x * (m / a)] != t[y * (n / a)])
        ans = true;
      if (x < n - 1)
        x++;
      else
        y++;
    } else {
      if (x * (m / a) > y * (n / a))
        y++;
      else if (x * (m / a) < y * (n / a))
        x++;
    }
  }
  if (!ans)
    cout << (n / a) * m << endl;
  else
    cout << -1 << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
#define rep(i, a, b) for (long long i = a; i < b; ++i)
#define rrep(i, a, b) for (int i = a; i >= b; --i)
#define yesno(flag)                                                            \
  if (flag)                                                                    \
    cout << "Yes" << endl;                                                     \
  else                                                                         \
    cout << "No" << endl;
using namespace std;
const int INF = 1000000000LL;
const ll r197 = 1000000007;
typedef pair<int, int> P;
typedef pair<int, P> Pa;

int main() {
  ll n, m;
  string s, t;
  cin >> n >> m;
  cin >> s >> t;
  ll a = n, b = m;
  while (1) {
    if (a < b) {
      ll d = a;
      a = b;
      b = d;
    }
    if (b == 0)
      break;
    a %= b;
  }
  bool ans = false;
  ll x = 0, y = 0;
  while (1) {
    if (x >= n || y >= m)
      break;
    if (x * (m / a) == y * (n / a)) {
      if (s[x] != t[y])
        ans = true;
      if (x < n - 1)
        x++;
      else
        y++;
    } else {
      if (x * (m / a) > y * (n / a))
        y++;
      else if (x * (m / a) < y * (n / a))
        x++;
    }
  }
  if (!ans)
    cout << (n / a) * m << endl;
  else
    cout << -1 << endl;
  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cout << '>' << #x << ':' << x << endl;
#define ll long long

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;
  ll lcm = (n * m) / __gcd(n, m);
  string ans;
  ans.resize(lcm + 1);
  ans[1] = s[0];
  for (int i = 1; i <= n; i++) {
    // cout << (i * lcm / n) + 1 << " " << s[i] << endl;
    // if ((i * lcm / m) + 1 <= lcm)
    ans[(i * lcm / n) + 1] = s[i];
  }
  for (int i = 1; i <= m; i++) {
    //	cout << (i * lcm / m) + 1 << " " << t[i] << endl;
    // if ((i * lcm / m) + 1 <= lcm)
    ans[(i * lcm / m) + 1] = t[i];
  }
  // cout << ans << endl;
  for (int i = 1; i <= n; i++) {
    if (ans[(i * lcm / n) + 1] != s[i]) {
      return cout << "-1", 0;
    }
  }
  cout << lcm << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cout << '>' << #x << ':' << x << endl;
#define ll long long

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;
  ll gcd = __gcd(n, m);
  ll lcm = (n * m) / gcd;
  n /= gcd;
  m /= gcd;
  for (int i = 0; i < gcd; i++) {
    if (s[i * n] != t[i * m]) {
      return cout << "-1", 0;
    }
  }
  cout << lcm << endl;
  return 0;
}
Problem p03231 (C++)

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

typedef long long ll;
ll gcd(ll x, ll y) {
  ll p = max(x, y);
  ll q = min(x, y);
  while (p % q != 0) {
    p %= q;
    ll temp = q;
    q = p;
    p = temp;
  }
  return q;
}
int main() {
  bool ans = true;
  ll n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;
  int gd = gcd(n, m);
  ll xlen = (n / gd) * (m / gd) * gd;
  for (int i = 0; i < (gd); i++) {
    if (s[(n / gd) * (m / gd) * i] != t[(n / gd) * (m / gd) * i]) {
      ans = false;
    }
  }
xxx:
  if (!ans) {
    cout << -1 << endl;
  } else {
    cout << xlen << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
ll gcd(ll x, ll y) {
  ll p = max(x, y);
  ll q = min(x, y);
  while (p % q != 0) {
    p %= q;
    ll temp = q;
    q = p;
    p = temp;
  }
  return q;
}
int main() {
  bool ans = true;
  ll n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;
  int gd = gcd(n, m);
  ll xlen = (n / gd) * (m / gd) * gd;
  for (int i = 0; i < (gd); i++) {
    if (s[(n / gd) * i] != t[(m / gd) * i]) {
      ans = false;
    }
  }
xxx:
  if (!ans) {
    cout << -1 << endl;
  } else {
    cout << xlen << endl;
  }
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <cstdint>
#include <vector>
#define FOR(i, l, r) for (int i = (l); i < (r); ++i)
#define RFOR(i, l, r) for (int i = (l); i >= (int)(r); i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n - 1, 0)

#define int long long
using namespace std;
const int MX = 1e6;
const int inf = 1e13;
const int mod = 1e9 + 7;
#define ll long long

signed main() {
  int n, m;
  string s, t;
  cin >> n >> m >> s >> t;
  int l = n * m / __gcd(n, m);
  vector<char> c(l + 1, '.');
  rep(i, n) {
    int x = l / n * (i) + 1;
    c[x] = s[i];
  }
  rep(i, m) {
    int x = l / m * (i) + 1;
    if (c[x] != t[i] && c[x] != '.') {
      // cout << c[x]<<" "<<t[i] << endl;
      cout << -1 << endl;
      return 0;
    }
  }
  cout << l << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <cstdint>
#include <vector>
#define FOR(i, l, r) for (int i = (l); i < (r); ++i)
#define RFOR(i, l, r) for (int i = (l); i >= (int)(r); i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n - 1, 0)

#define int long long
using namespace std;
const int MX = 1e6;
const int inf = 1e13;
const int mod = 1e9 + 7;
#define ll long long

signed main() {
  int n, m;
  string s, t;
  cin >> n >> m >> s >> t;
  int l = n * m / __gcd(n, m);
  int a = l / m;
  int b = l / n;
  rep(i, n / a + 1) {
    if (s[i * a] != t[i * b]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << l << endl;

  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;

int main() {
  int n, m;
  string s, t;
  cin >> n >> m >> s >> t;
  ll ans = n / __gcd(n, m) * m;
  ll a = ans / n, b = ans / m;
  for (ll i = 0, j = 0; i < n; i += b, j += a) {
    if (s[i] != t[j])
      ans = -1;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;

int main() {
  int n, m;
  string s, t;
  cin >> n >> m >> s >> t;
  ll g = __gcd(n, m);
  ll ans = n / g * m;
  for (int i = 0, j = 0; i < n; i += n / g, j += m / g) {
    if (s[i] != t[j])
      ans = -1;
  }
  cout << ans << endl;
  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m;
  string s, t;
  cin >> n >> m >> s >> t;
  long long l = n * m / (__gcd(n, m));
  string ans(l, '1');
  for (int i = 0; i < n; i++) {
    int j = i * (l / n) + 1;
    ans[j - 1] = s[i];
  }
  for (int i = 0; i < m; i++) {
    int j = i * (l / m) + 1;
    if (ans[j - 1] == '1') {
      ans[j - 1] = t[i];
    } else if (ans[j - 1] != t[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << l << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m;
  string s, t;
  cin >> n >> m >> s >> t;
  long long l = n * m / __gcd(n, m);
  for (long long i = 0; i < m; i++) {
    long long j = i * (l / m);
    if (j % (l / n) == 0 and s[j / (l / n)] != t[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << l << endl;
  return 0;
}
Problem p03231 (C++)

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

typedef long long ll;

int main() {
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;
  int an = n * m / __gcd(n, m);
  if (n > m)
    swap(n, m), swap(s, t);
  for (int i = 0; i < n; i++)
    if (i * (an / n) % (an / m) == 0)
      if (s[i] != t[i * (an / n) / (an / m)]) {
        cout << -1 << "\n";
        return 0;
      }
  cout << an << "\n";

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

#define int long long

int32_t main() {
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;
  int an = n * m / __gcd(n, m);
  if (n > m)
    swap(n, m), swap(s, t);
  for (int i = 0; i < n; i++)
    if (i * (an / n) % (an / m) == 0)
      if (s[i] != t[i * (an / n) / (an / m)]) {
        cout << -1 << "\n";
        return 0;
      }
  cout << an << "\n";

  return 0;
}
Problem p03231 (C++)

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

using namespace std;

const int maxn = 2000100;
typedef long long int ll;

ll N, M;
char S[maxn], T[maxn];

ll gcd(ll m, ll n) { return (0 == n) ? m : gcd(n, m % n); }

int main(void) {
  scanf("%lld%lld", &N, &M);
  scanf("%s", S);
  scanf("%s", T);
  ll g = gcd(N, M);
  ll l = N * M / g;
  ll a = M / g;
  ll b = N / g;
  for (int i = 0; i < N; i++) {
    int pos_in_x = a * i;
    if (0 == (pos_in_x % b)) {
      ll pos_in_t = pos_in_x / b;
      if (pos_in_t >= M) {
        puts("-1");
        return 0;
      }
      if (S[i] != T[pos_in_t]) {
        puts("-1");
        return 0;
      }
    }
  }
  printf("%lld\n", l);
  return 0;
}


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

using namespace std;

const int maxn = 2000100;
typedef long long int ll;

ll N, M;
char S[maxn], T[maxn];

ll gcd(ll m, ll n) { return (0 == n) ? m : gcd(n, m % n); }

int main(void) {
  scanf("%lld%lld", &N, &M);
  scanf("%s", S);
  scanf("%s", T);
  ll g = gcd(N, M);
  ll l = N * M / g;
  ll a = M / g;
  ll b = N / g;
  for (int i = 0; i < N; i++) {
    ll pos_in_x = a * i;
    if (0 == (pos_in_x % b)) {
      ll pos_in_t = pos_in_x / b;
      if (pos_in_t >= M) {
        puts("-1");
        return 0;
      }
      if (S[i] != T[pos_in_t]) {
        puts("-1");
        return 0;
      }
    }
  }
  printf("%lld\n", l);
  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
long long lcm(long long x, long long y) { return x * y / gcd(x, y); }
long long N, M;
string S, T;
int main() {
  cin >> N >> M >> S >> T;
  long long L = lcm(N, M);
  string X(L, '*');
  for (int i = 0; i < N; i++)
    X[(L / N) * i] = S[i];
  for (int i = 0; i < M; i++)
    if (X[(L / M) * i] != '*' && X[(L / M) * i] != T[i]) {
      cout << -1 << endl;
      return 0;
    }
  cout << L << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
long long lcm(long long x, long long y) { return x * y / gcd(x, y); }
long long N, M;
string S, T;
int main() {
  cin >> N >> M >> S >> T;
  long long L = lcm(N, M), n = N / gcd(N, M), m = M / gcd(N, M);
  for (int i = 0; i < gcd(N, M); i++)
    if (S[n * i] != T[m * i]) {
      cout << -1 << endl;
      return 0;
    }
  cout << L << endl;
  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <functional>
#include <iostream>
#include <numeric>
#include <stdlib.h>
#include <string>

using namespace std;
#define pi 3.14159265

int gcd(int m, int n) {
  // 引数に０がある場合は０を返す
  if ((0 == m) || (0 == n))
    return 0;

  // ユークリッドの方法
  while (m != n) {
    if (m > n)
      m = m - n;
    else
      n = n - m;
  }
  return m;
}

int main() {
  long long ans = 0, i = 0, j = 0, k = 0, l = 0;
  long long N, M, n, m;
  string S, T;
  cin >> N >> M;
  cin >> S >> T;

  long long L = 0;
  L = N * M / gcd(N, M);
  n = L / N;
  m = L / M;
  while (true) {
    j++;
    if ((j % n == 0) && (j % m == 0))
      break;
  }

  for (i = 0; i < L / j; i++) {
    if (S[i * m])
      if ((S[i * m] != T[i * n]) && T[i * n]) {
        cout << -1 << endl;
        return 0;
      }
  }

  cout << L << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <numeric>
#include <stdlib.h>
#include <string>

using namespace std;
#define pi 3.14159265

int gcd(int m, int n) {
  // 引数に０がある場合は０を返す
  if ((0 == m) || (0 == n))
    return 0;

  // ユークリッドの方法
  while (m != n) {
    if (m > n)
      m = m - n;
    else
      n = n - m;
  }
  return m;
}

int main() {
  long long ans = 0, i = 0, j = 0, k = 0, l = 0;
  long long N, M, n, m;
  string S, T;
  cin >> N >> M;
  cin >> S >> T;

  long long L = 0;
  L = N * M / gcd(N, M);
  n = L / N;
  m = L / M;
  j = n * m / gcd(n, m);

  for (i = 0; i < L / j; i++) {
    if (S[i * m])
      if ((S[i * m] != T[i * n]) && T[i * n]) {
        cout << -1 << endl;
        return 0;
      }
  }

  cout << L << endl;
  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vll = vector<vl>;
using vb = vector<bool>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using psi = pair<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define range(i, s, n) for (int i = s; i < n; i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define fs first
#define sc second
#define mp make_pair
#define INF 1E9
#define LINF 1E18 * 5
#define EPS 1E-9
#define MOD 1000000007
#define PI 3.1415926535897932384

template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
  os << "[" << p.first << ", " << p.second << "]";
  return os;
};
template <class S> auto &operator<<(ostream &os, vector<S> t) {
  bool a = 1;
  for (auto s : t) {
    os << (a ? "" : " ") << s;
    a = 0;
  }
  return os;
}

// int dx[]={1,1,1,0,-1,-1,-1,0},dy[8]={1,0,-1,-1,-1,0,1,1,1};
//
constexpr ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

class compare {
public:
  bool operator()(tuple<int, int> a, tuple<int, int> b) {
    return (get<1>(a) < get<1>(b));
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  string a, b;
  cin >> a >> b;
  ll g = gcd(n, m);
  ll l = n * m / g;
  int ra = n / l, rb = m / l;
  bool ans = true;
  rep(i, g) {
    if (a[ra * i] != b[rb * i])
      ans = false;
  }
  cout << (ans ? l : -1);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vll = vector<vl>;
using vb = vector<bool>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using psi = pair<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define range(i, s, n) for (int i = s; i < n; i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define fs first
#define sc second
#define mp make_pair
#define INF 1E9
#define LINF 1E18 * 5
#define EPS 1E-9
#define MOD 1000000007
#define PI 3.1415926535897932384

template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
  os << "[" << p.first << ", " << p.second << "]";
  return os;
};
template <class S> auto &operator<<(ostream &os, vector<S> t) {
  bool a = 1;
  for (auto s : t) {
    os << (a ? "" : " ") << s;
    a = 0;
  }
  return os;
}

// int dx[]={1,1,1,0,-1,-1,-1,0},dy[8]={1,0,-1,-1,-1,0,1,1,1};
//
constexpr ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

class compare {
public:
  bool operator()(tuple<int, int> a, tuple<int, int> b) {
    return (get<1>(a) < get<1>(b));
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  string a, b;
  cin >> a >> b;
  ll g = gcd(n, m);
  ll l = 1ll * n * m / g;
  int ra = n / g, rb = m / g;
  bool ans = true;
  rep(i, g) {
    if (a[ra * i] != b[rb * i])
      ans = false;
  }
  cout << (ans ? l : -1);
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define int long long
#define P pair<long, long>
#define all(a) a.begin(), a.end()
using namespace std;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
signed main() {
  int n, m;
  string s, t;
  cin >> n >> m >> s >> t;
  int l = n * m / gcd(n, m);
  vector<char> vec(l, 'A');
  rep(i, n) { vec.at(i * l / n) = s.at(i); }
  rep(i, m) {
    if (vec.at(i * l / m) == 'A' or vec.at(i * l / m) == t.at(i)) {
      vec.at(i * l / m) = t.at(i);
    } else {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << l << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define int long long
#define P pair<long, long>
#define all(a) a.begin(), a.end()
using namespace std;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
signed main() {
  int n, m;
  string s, t;
  cin >> n >> m >> s >> t;
  int l = n * m / gcd(n, m);
  int a = l / n, b = l / m;
  int c = a * b / gcd(a, b);
  rep(i, l / c) {
    if (s.at(i * c / a) != t.at(i * c / b)) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << l << endl;
}
Problem p03231 (C++)

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

using namespace std;

long long n, m, l;
string s1, s2;
char x[999800010];

int gcd(int a, int b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}

long long lcm(long a, long b) { return a / gcd(a, b) * b; }

int main() {

  cin >> n >> m >> s1 >> s2;

  l = lcm(n, m);

  for (int i = 0; i < s1.size(); ++i) {
    x[i * l / n + 1] = s1[i];
  }

  for (int i = 0; i < s2.size(); ++i) {
    if (x[i * l / m + 1] != 0 && x[i * l / m + 1] != s2[i]) {
      cout << -1;
      return 0;
    }
  }

  cout << l;

  return 0;
}

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

using namespace std;

long long n, m, l;
string s1, s2;
map<int, char> x;

int gcd(int a, int b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}

long long lcm(long a, long b) { return a / gcd(a, b) * b; }

int main() {

  cin >> n >> m >> s1 >> s2;

  l = lcm(n, m);

  for (int i = 0; i < s1.size(); ++i) {
    x[i * l / n + 1] = s1[i];
  }

  for (int i = 0; i < s2.size(); ++i) {
    if (x[i * l / m + 1] != 0 && x[i * l / m + 1] != s2[i]) {
      cout << -1;
      return 0;
    }
  }

  cout << l;

  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <math.h>
#include <numeric>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

long long int A, a, n, m, y, s;
string str_1, str_2, str_3;
long long int x[2][30000];

int gcd(int m, int n) {
  if (m % n == 0)
    return n;
  m %= n;
  if (n % m == 0)
    return m;
  n %= m;
}

int main() {
  cin >> n >> m;
  cin >> str_1 >> str_2;

  for (int i = 0; i < gcd(n, m); i++) {
    if (str_1[i * n / gcd(n, m)] != str_2[i * m / gcd(n, m)]) {
      cout << -1 << '\n';
      return 0;
    }
  }
  cout << n * m / gcd(n, m) << '\n';
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <math.h>
#include <numeric>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

long long int A, a, n, m, y, s;
string str_1, str_2, str_3;
long long int x[2][30000];

int gcd(int m, int n) {
  while (true) {
    if (m % n == 0)
      return n;
    m %= n;
    if (n % m == 0)
      return m;
    n %= m;
  }
}

int main() {
  cin >> n >> m;
  cin >> str_1 >> str_2;

  for (int i = 0; i < gcd(n, m); i++) {
    if (str_1[i * n / gcd(n, m)] != str_2[i * m / gcd(n, m)]) {
      cout << -1 << '\n';
      return 0;
    }
  }
  cout << n * m / gcd(n, m) << '\n';
  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll lcm(ll m, ll n) { // 最小公倍数
  if (0 == m || 0 == n)
    return 0;
  return ((m / __gcd(m, n)) * n);
}

int main() {
  ll N, M, L, a, b;
  cin >> N >> M;
  string S, T;
  cin >> S >> T;

  L = lcm(N, M);
  a = L / N;
  b = L / M;
  // SはX[0], X[a], X[2a], ...,X[(N-1)*a]を連結したもの
  // TはX[0], X[b], X[2b], ...,X[(M-1)*b]を連結したもの
  ll l = min((N - 1) * a + 1, (M - 1) * b + 1), c;
  if (N > M)
    c = b;
  else
    c = a;
  for (int i = 0; i <= l; i += c) {
    // a, bの公倍数となっている箇所をチェック
    if (i % a == 0 && i % b == 0) {
      if (S[i / a] != T[i / b]) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  cout << L << endl;
  return 0;
}

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

ll lcm(ll m, ll n) { // 最小公倍数
  if (0 == m || 0 == n)
    return 0;
  return ((m / __gcd(m, n)) * n);
}

int main() {
  ll N, M, L, a, b;
  cin >> N >> M;
  string S, T;
  cin >> S >> T;

  L = lcm(N, M);
  a = L / N;
  b = L / M;
  // SはX[0], X[a], X[2a], ...,X[(N-1)*a]を連結したもの
  // TはX[0], X[b], X[2b], ...,X[(M-1)*b]を連結したもの
  ll l = min((N - 1) * a + 1, (M - 1) * b + 1), c;
  if (N > M)
    c = b;
  else
    c = a;
  for (ll i = 0; i <= l; i += c) {
    // a, bの公倍数となっている箇所をチェック
    if (i % a == 0 && i % b == 0) {
      if (S[i / a] != T[i / b]) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  cout << L << endl;
  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
  long long N, M;
  string S, T;
  cin >> N >> M >> S >> T;
  long long i, j;
  long long k = 1;
  for (i = 0; i < N && k == 1; i++) {
    for (j = 0; j < M; j++) {
      if (i * M == j * N && S[i] != T[j]) {
        k = 0;
      }
    }
  }

  if (k == 0) {
    cout << -1 << endl;
  } else {
    long long n, m, gcd;
    n = N;
    m = M;
    while (n * m != 0) {
      if (n > m) {
        n = n % m;
      } else {
        m = m % n;
      }
    }
    if (n > m) {
      gcd = n;
    } else {
      gcd = m;
    }
    cout << N * M / gcd << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
  long long N, M;
  string S, T;
  cin >> N >> M >> S >> T;
  long long i, j;
  long long k = 1;
  for (i = 0; i < N && k == 1; i++) {
    j = i * M / N;
    if (i * M == j * N && S[i] != T[j]) {
      k = 0;
    }
  }

  if (k == 0) {
    cout << -1 << endl;
  } else {
    long long n, m, gcd;
    n = N;
    m = M;
    while (n * m != 0) {
      if (n > m) {
        n = n % m;
      } else {
        m = m % n;
      }
    }
    if (n > m) {
      gcd = n;
    } else {
      gcd = m;
    }
    cout << N * M / gcd << endl;
  }
}
Problem p03231 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
typedef vector<int> vint;
typedef vector<ll> vll;
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec)
    is >> v;
  return is;
}
template <typename A, typename B> inline bool chmin(A &a, const B &b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename A, typename B> inline bool chmax(A &a, const B &b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

int main() {
  ll N, M;
  cin >> N >> M;
  string S, T;
  cin >> S >> T;
  ll L = N * M / __gcd(N, M);
  ll stepN, stepM;
  stepN = L / N;
  stepM = L / M;
  for (int i = 1; i <= L; i++) {
    if ((0 == (i - 1) % stepN) && (0 == (i - 1) % stepM)) {
      int idxN = (i - 1) / stepN;
      int idxM = (i - 1) / stepM;
      if (S[idxN] != T[idxM]) {
        L = -1;
        break;
      }
    }
  }
  cout << L << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
typedef vector<int> vint;
typedef vector<ll> vll;
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec)
    is >> v;
  return is;
}
template <typename A, typename B> inline bool chmin(A &a, const B &b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename A, typename B> inline bool chmax(A &a, const B &b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

int main() {
  ll N, M;
  cin >> N >> M;
  string S, T;
  cin >> S >> T;
  ll L = N * M / __gcd(N, M);
  ll stepN, stepM;
  stepN = L / N;
  stepM = L / M;
  for (int i = 0; i < M; i++) {
    if (0 == (i * stepM) % stepN) {
      int idxN = (i * stepM) / stepN;
      if (idxN >= N) {
        break;
      }
      if (S[idxN] != T[i]) {
        L = -1;
        break;
      }
    }
  }
  cout << L << endl;
}
Problem p03231 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int main() {
  long long n, m;
  string s, t;
  cin >> n >> m;
  cin >> s;
  cin >> t;
  //	string ans;
  long long G;
  if (n > m) {
    G = n * m / gcd(n, m);
  } else {
    G = n * m / gcd(m, n);
  }
  if (s[0] != t[0]) {
    cout << "-1" << endl;
  } else {
    bool flag = false;
    int aans;
    for (int i = 1; i <= 100005; i++) {
      flag = true;
      long long x = G * i / n;
      long long y = G * i / m;
      int k, j;
      k = j = 0;
      while (k != n && j != m) {
        long long m;
        if (x * k + 1 < y * j + 1) {
          m = x * k + 1;
          k++;
        } else if (x * k + 1 == y * j + 1) {
          m = x * k + 1;
          if (s[k] != t[j]) {
            flag = false;
          }
          k++;
          j++;
        } else {
          m = y * j + 1;
          j++;
        }
      }
      if (flag) {
        aans = i;
        break;
      }
    }
    if (flag) {
      long long res = G * aans;
      cout << res << endl;
    } else {
      cout << "-1" << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int main() {
  long long n, m;
  string s, t;
  cin >> n >> m;
  cin >> s;
  cin >> t;
  //	string ans;
  long long G;
  if (n > m) {
    G = n * m / gcd(n, m);
  } else {
    G = n * m / gcd(m, n);
  }
  if (s[0] != t[0]) {
    cout << "-1" << endl;
  } else {
    bool flag = false;
    int aans;
    for (int i = 1; i <= 1005; i++) {
      flag = true;
      long long x = G * i / n;
      long long y = G * i / m;
      int k, j;
      k = j = 0;
      while (k != n && j != m) {
        long long m;
        if (x * k + 1 < y * j + 1) {
          m = x * k + 1;
          k++;
        } else if (x * k + 1 == y * j + 1) {
          m = x * k + 1;
          if (s[k] != t[j]) {
            flag = false;
          }
          k++;
          j++;
        } else {
          m = y * j + 1;
          j++;
        }
      }
      if (flag) {
        aans = i;
        break;
      }
    }
    if (flag) {
      long long res = G * aans;
      cout << res << endl;
    } else {
      cout << "-1" << endl;
    }
  }
  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> P;
const int INF = 1e18;

int gcd(int a, int b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

signed main() {
  int n, m;
  string s, t;
  cin >> n >> m >> s >> t;
  int l = n / gcd(n, m) * m;
  bool f = true;
  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)])
        f = false;
    }
  }
  if (f)
    cout << l << endl;
  else
    cout << -1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> P;
const int INF = 1e18;

int gcd(int a, int b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

signed main() {
  int n, m;
  string s, t;
  cin >> n >> m >> s >> t;
  int l = n / gcd(n, m) * m;
  bool f = true;
  for (int i = 0; i < l; i += (l / n)) {
    if (i % (l / n) == 0 && i % (l / m) == 0) {
      if (s[i / (l / n)] != t[i / (l / m)])
        f = false;
    }
  }
  if (f)
    cout << l << endl;
  else
    cout << -1 << endl;
}
Problem p03231 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define MAX_S 100000
#define MAX_T 100000

int N, M;
char S[MAX_S];
char T[MAX_T];

long long gcd(long long m, long long n) {
  if (m < n) {
    swap(m, n);
  }
  long long temp;
  while (m % n != 0) {
    temp = n;
    n = m % n;
    m = temp;
  }
  return n;
}

long long lcm(long long m, long long n) { return m / gcd(m, n) * n; }

int main() {

  cin >> N >> M;
  cin >> S >> T;
  long long L = lcm(N, M);
  int ans = L;
  for (long long i = 0; i < N; i++) {
    for (long long j = 0; j < M; j++) {
      if (i * L / N == j * L / M && S[i] != T[j]) {
        ans = -1;
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define MAX_S 100000
#define MAX_T 100000

int N, M;
char S[MAX_S];
char T[MAX_T];

long long gcd(long long m, long long n) {
  if (m < n) {
    swap(m, n);
  }
  long long temp;
  while (m % n != 0) {
    temp = n;
    n = m % n;
    m = temp;
  }
  return n;
}

long long lcm(long long m, long long n) { return m / gcd(m, n) * n; }

int main() {

  cin >> N >> M;
  cin >> S >> T;
  long long L = lcm(N, M);
  long long ans = L;
  long long G = gcd(N, M);
  long long n = N / G;
  long long m = M / G;
  for (long long i = 0; i < G; i++) {
    if (S[i * n] != T[i * m]) {
      ans = -1;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p03231 (Python)

Buggy submission (status: Runtime Error):
# from math import gcd
from fraction import gcd
import sys

N, M = map(int, input().split())
S = input()
T = input()

d = gcd(N, M)
m = N * M // d

for i in range(d):
    if S[i * (N // d)] != T[i * (M // d)]:
        print(-1)
        sys.exit()

print(m)


Fixed submission:
# from math import gcd
from fractions import gcd
import sys

N, M = map(int, input().split())
S = input()
T = input()

d = gcd(N, M)
m = N * M // d

for i in range(d):
    if S[i * (N // d)] != T[i * (M // d)]:
        print(-1)
        sys.exit()

print(m)
Problem p03231 (Python)

Buggy submission (status: Runtime Error):
from math import gcd

N, M = map(int, input().split())
S = input()
T = input()

G = gcd(N, M)
L = N * M // G
n = N // G
m = M // G

ans = True
for k in range(G):
    if S[k * n] != T[k * m]:
        ans = False
        break
print(L if ans else -1)


Fixed submission:
from fractions import gcd

N, M = map(int, input().split())
S = input()
T = input()

G = gcd(N, M)
L = N * M // G
n = N // G
m = M // G

ans = True
for k in range(G):
    if S[k * n] != T[k * m]:
        ans = False
        break
print(L if ans else -1)
Problem p03231 (Python)

Buggy submission (status: Time Limit Exceeded):
#!/usr/bin/env python


def gcd(a, b):
    if a < b:
        a, b = b, a

    if b == 1:
        return 1
    m = a % b
    if m == 0:
        return b
    return gcd(b, m)


def main():
    N, M = [int(a) for a in input().split()]

    S = input()
    T = input()
    L = N * M // gcd(N, M)
    a1 = L // N
    a2 = L // M
    cc = a1 * a2 // gcd(a1, a2)
    for i in range(L):
        if i % cc != 0:
            continue
        if S[i // a1] != T[i // a2]:
            print(-1)
            return
    print(L)


if __name__ == "__main__":
    main()


Fixed submission:
#!/usr/bin/env python


def gcd(a, b):
    if a < b:
        a, b = b, a

    if b == 1:
        return 1
    m = a % b
    if m == 0:
        return b
    return gcd(b, m)


def main():
    N, M = [int(a) for a in input().split()]

    S = input()
    T = input()
    L = N * M // gcd(N, M)
    a1 = L // N
    a2 = L // M
    cc = a1 * a2 // gcd(a1, a2)
    for i in range(0, L, cc):
        if S[i // a1] != T[i // a2]:
            print(-1)
            return
    print(L)


if __name__ == "__main__":
    main()
Problem p03231 (Python)

Buggy submission (status: Runtime Error):
import fractions

n, m = map(int, input().split())
s = input()
t = input()
lcm = n * m // fractions.gcd(n, m)
sl = [""] * lcm
for i in range(len(s)):
    temp = i * lcm // n
    sl[temp] = s[i]
for i in range(len(t)):
    temp = i * lcm // m
    if sl[temp] != "" and sl[temp] != t[i]:
        print(-1)
        exit()
print(lcm)


Fixed submission:
import fractions

n, m = map(int, input().split())
s = input()
t = input()
gcd = fractions.gcd(n, m)
lcm = n * m // gcd
o = lcm // gcd
for i in range(gcd):
    if s[i * lcm // m] != t[i * lcm // n]:
        print(-1)
        exit()
print(lcm)