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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
typedef long long int ll;
typedef pair<int, int> pi;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n, m;
  cin >> n >> m;
  string s, t;
  cin >> s;
  cin >> t;
  if (s[0] != t[0]) {
    cout << -1;
  } else {
    ll lo = 1, hi = mod, mi;
    ll l = lcm(n, m);
    while (lo < hi) {
      mi = (lo + hi) / 2;
      ll ln = mi * l;
      ll fn = lcm(ln / n, ln / m);
      int x = (int)(fn / (ln / n));
      int y = (int)(fn / (ln / m));
      bool ch = true;
      int i = x, j = y;
      while (i < n && j < m) {
        if (s[i] != t[j]) {
          ch = false;
          break;
        }
        i += x, j += y;
      }
      if (ch)
        hi = mi;
      else
        lo = mi + 1;
    }
    mi = lo;
    ll ln = mi * l;
    ll fn = lcm(ln / n, ln / m);
    int x = (int)(fn / (ln / n));
    int y = (int)(fn / (ln / m));
    bool ch = true;
    int i = x, j = y;
    while (i < n && j < m) {
      if (s[i] != t[j]) {
        ch = false;
        break;
      }
      i += x, j += y;
    }
    if (ch)
      cout << (lo * l);
    else
      cout << -1;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define mod 10007
using namespace std;
typedef long long int ll;
typedef pair<int, int> pi;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n, m;
  cin >> n >> m;
  string s, t;
  cin >> s;
  cin >> t;
  if (s[0] != t[0]) {
    cout << -1;
  } else {
    ll lo = 1, hi = mod, mi;
    ll l = lcm(n, m);
    while (lo < hi) {
      mi = (lo + hi) / 2;
      ll ln = mi * l;
      ll fn = lcm(ln / n, ln / m);
      int x = (int)(fn / (ln / n));
      int y = (int)(fn / (ln / m));
      bool ch = true;
      int i = x, j = y;
      while (i < n && j < m) {
        if (s[i] != t[j]) {
          ch = false;
          break;
        }
        i += x, j += y;
      }
      if (ch)
        hi = mi;
      else
        lo = mi + 1;
    }
    mi = lo;
    ll ln = mi * l;
    ll fn = lcm(ln / n, ln / m);
    int x = (int)(fn / (ln / n));
    int y = (int)(fn / (ln / m));
    bool ch = true;
    int i = x, j = y;
    while (i < n && j < m) {
      if (s[i] != t[j]) {
        ch = false;
        break;
      }
      i += x, j += y;
    }
    if (ch)
      cout << (lo * l);
    else
      cout << -1;
  }
}
Problem p03231 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const long long LINF = 8931145141919364364, LMOD = 998244353;
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

int gcd(int x, int y) {
  if (x < y)
    swap(x, y); // 常にx>=y
  // ユークリッドの互除法
  if (!(x % y))
    return y;
  else
    return gcd(y, x % y);
}

int main() {
  long long n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;
  // if(max(n,m)%min(n,m)==0){
  //     cout<<-1<<endl;
  //     return 0;
  // }
  long long cnt = 0;
  for (long long i = n * m / gcd(n, m);; i += n * m / gcd(n, m), cnt++) {
    if (i < 0 || i > LINF || cnt > 1e5)
      break;
    // cout<<i<<endl;
    long long ss = 0, tt = 0;
    bool flg = 1;
    while (ss < n && tt < m) {
      // cout<<ss<<" "<<tt<<endl;
      if (ss * i / n == tt * i / m) {
        if (s[ss] != t[tt]) {
          flg = 0;
          break;
        }
      }
      if (ss * i / n > tt * i / m)
        tt++;
      else
        ss++;
    }
    if (flg) {
      cout << i << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const long long LINF = 8931145141919364364, LMOD = 998244353;
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

int gcd(int x, int y) {
  if (x < y)
    swap(x, y); // 常にx>=y
  // ユークリッドの互除法
  if (!(x % y))
    return y;
  else
    return gcd(y, x % y);
}

int main() {
  long long n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;
  // if(max(n,m)%min(n,m)==0){
  //     cout<<-1<<endl;
  //     return 0;
  // }
  long long cnt = 0;
  for (long long i = n * m / gcd(n, m);; i += n * m / gcd(n, m), cnt++) {
    if (i < 0 || i > LINF || cnt > 1e3)
      break;
    // cout<<i<<endl;
    long long ss = 0, tt = 0;
    bool flg = 1;
    while (ss < n && tt < m) {
      // cout<<ss<<" "<<tt<<endl;
      if (ss * i / n == tt * i / m) {
        if (s[ss] != t[tt]) {
          flg = 0;
          break;
        }
      }
      if (ss * i / n > tt * i / m)
        tt++;
      else
        ss++;
    }
    if (flg) {
      cout << i << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;
// 最大公約数を求める関数
long gcd(int x, int y) {
  long r;
  // ユーグリッドの互除法
  while ((r = x % y) != 0) // yで割り切れるまでループ
  {
    x = y;
    y = r;
  }
  return y;
}
long lcm(long x, long y) { return (x * y / gcd(x, y)); }

int main(void) {
  long N, M;
  char S[100001];
  char T[100001];
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }
  for (int i = 0; i < M; i++) {
    cin >> T[i];
  }
  if (S[0] != T[0]) {
    cout << -1 << endl;
    return 0;
  }
  long L = lcm(N, M);
  long NN = L / N;
  long MM = L / M;
  for (long i = 1; i <= L; i++) {
    if (i % NN == 0 && i % MM == 0) {
      if (i / NN < N && i / MM < M) {
        if (S[i / NN] != T[i / MM]) {
          cout << -1 << endl;
          return 0;
        }
      }
    }
  }
  cout << L << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
// 最大公約数を求める関数
long gcd(int x, int y) {
  long r;
  // ユーグリッドの互除法
  while ((r = x % y) != 0) // yで割り切れるまでループ
  {
    x = y;
    y = r;
  }
  return y;
}
long lcm(long x, long y) { return (x * y / gcd(x, y)); }

int main(void) {
  long N, M;
  char S[100001];
  char T[100001];
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }
  for (int i = 0; i < M; i++) {
    cin >> T[i];
  }
  if (S[0] != T[0]) {
    cout << -1 << endl;
    return 0;
  }
  long L = lcm(N, M);
  long NN = L / N;
  long MM = L / M;
  long LL = lcm(NN, MM);
  for (long i = 1; i <= L / LL; i++) {
    if (i * LL / NN < N && i * LL / MM < M) {
      if (S[i * LL / NN] != T[i * LL / MM]) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  cout << L << endl;
  return 0;
}
Problem p03231 (C++)

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

#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define fi first
#define se second
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef long double ld;
typedef string st;
const double PI =
    3.14159265358979323846264338327950288419716939937510582097494459230;
const ll MOD = 1e9 + 7;
const ll INF = 1e14;
ll mpow(ll a, ll b, ll p = MOD) {
  a = a % p;
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % p;
    a = (a * a) % p;
    b = b >> 1;
  }
  return res % p;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ll n, m;
  cin >> n >> m;
  st a, b;
  cin >> a >> b;
  ll lcm = (n * m) / (__gcd(n, m));
  ll k1 = lcm / n;
  ll k2 = lcm / m;
  forn(i, n) {
    ll o = i * m;
    if (o % n == 0) {
      if (a[i] != b[o / n]) {
        cout << "-1";
        return 0;
      }
    }
  }
  forn(i, m) {
    ll o = i * n;
    if (o % m == 0) {
      if (b[i] != a[o / m]) {
        cout << "-1";
        return 0;
      }
    }
  }
  cout << lcm;
  return 0;
}

Fixed submission:
/* nothing is impossible */
#include <bits/stdc++.h>

#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define fi first
#define se second
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef long double ld;
typedef string st;
const double PI =
    3.14159265358979323846264338327950288419716939937510582097494459230;
const ll MOD = 1e9 + 7;
const ll INF = 1e14;
ll mpow(ll a, ll b, ll p = MOD) {
  a = a % p;
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % p;
    a = (a * a) % p;
    b = b >> 1;
  }
  return res % p;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, m;
  cin >> n >> m;
  st a, b;
  cin >> a >> b;
  ll lcm = (n * m) / (__gcd(n, m));
  ll k1 = lcm / n;
  ll k2 = lcm / m;
  forn(i, n) {
    ll o = i * m;
    if (o % n == 0) {
      if (a[i] != b[o / n]) {
        cout << "-1";
        return 0;
      }
    }
  }
  forn(i, m) {
    ll o = i * n;
    if (o % m == 0) {
      if (b[i] != a[o / m]) {
        cout << "-1";
        return 0;
      }
    }
  }
  cout << lcm;
  return 0;
}
Problem p03231 (Python)

Buggy submission (status: Runtime Error):
import math

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

L = N * M // math.gcd(N, M)

d = dict()

for i in range(N):
    index = 1 + (L // N) * i
    d[index] = S[i]

for j in range(M):
    index = 1 + (L // M) * j
    if index in d:
        if d[index] != T[j]:
            print(-1)
            exit()
print(L)


Fixed submission:
import math

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


def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    c = a % b
    return gcd(b, c)


L = N * M // gcd(N, M)

d = dict()

for i in range(N):
    index = 1 + (L // N) * i
    d[index] = S[i]

for j in range(M):
    index = 1 + (L // M) * j
    if index in d:
        if d[index] != T[j]:
            print(-1)
            exit()
print(L)
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
const int N = 1e5 + 1e2;
int n, m;
char s1[N], s2[N], a[N];
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
int main() {
  scanf("%d %d", &n, &m);
  scanf(" %s", s1 + 1);
  scanf(" %s", s2 + 1);
  if (s1[1] != s2[1]) {
    puts("-1");
    return 0;
  }
  a[1] = s1[1];
  ll L = 1ll * n * m / (gcd(n, m));
  for (int i = 2; i <= n; ++i) {
    a[(i - 1) * (L / n) + 1] = s1[i];
  }
  for (int i = 2; i <= m; ++i) {
    if ('a' <= a[(i - 1) * (L / m) + 1] && 'z' >= a[(i - 1) * (L / m) + 1]) {
      if (a[(i - 1) * (L / m) + 1] != s2[i]) {
        puts("-1");
        return 0;
      }
    } else {
      a[(i - 1) * (L / m) + 1] = s2[i];
    }
  }
  printf("%lld", L);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
const int N = 1e5 + 1e2;
int n, m;
char s1[N], s2[N];
std::map<int, char> a;
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
int main() {
  scanf("%d %d", &n, &m);
  scanf(" %s", s1 + 1);
  scanf(" %s", s2 + 1);
  if (s1[1] != s2[1]) {
    puts("-1");
    return 0;
  }
  a[1] = s1[1];
  ll L = 1ll * n * m / (gcd(n, m));
  for (int i = 2; i <= n; ++i) {
    a[(i - 1) * (L / n) + 1] = s1[i];
  }
  for (int i = 2; i <= m; ++i) {
    if ('a' <= a[(i - 1) * (L / m) + 1] && 'z' >= a[(i - 1) * (L / m) + 1]) {
      if (a[(i - 1) * (L / m) + 1] != s2[i]) {
        puts("-1");
        return 0;
      }
    } else {
      a[(i - 1) * (L / m) + 1] = s2[i];
    }
  }
  printf("%lld", L);
  return 0;
}
Problem p03231 (C++)

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

int main() {
  int64_t N, M, l, lcm, dn, dm;
  string S, T;
  cin >> N >> M >> S >> T;
  l = (N * M) / __gcd(N, M);
  dn = l / N;
  dm = l / M;
  lcm = (dn * dm) / __gcd(dn, dm);
  for (int i = 0; i < l; i += lcm) {
    if (S.at(i / dn) != T.at(i / dm)) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << l << endl;
}


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

int main() {
  int64_t N, M, l, lcm, dn, dm;
  string S, T;
  cin >> N >> M >> S >> T;
  l = (N * M) / __gcd(N, M);
  dn = l / N;
  dm = l / M;
  lcm = (dn * dm) / __gcd(dn, dm);
  for (int64_t i = 0; i < l; i += lcm) {
    if (S.at(i / dn) != T.at(i / dm)) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << l << endl;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int gcd(int a, int b) {
  if (a == b)
    return a;
  if (a > b) {
    int c = a;
    a = b;
    b = c;
  }
  if (b % a == 0)
    return a;
  else
    return gcd(a, b % a);
}
int main(void) {
  long long n, m;
  scanf("%d%d", &n, &m);
  char s[n + 5];
  char t[m + 5];
  scanf(" %s", s);
  scanf(" %s", t);
  long long x = gcd(n, m);
  for (int i = 0; i < x; i++) {
    if (s[(n / x) * i] != t[(m / x) * i]) {
      printf("-1\n");
      return 0;
    }
  }
  printf("%lld\n", (n / x) * m);
}


Fixed submission:
#include <stdio.h>
int gcd(int a, int b) {
  if (a == b)
    return a;
  if (a > b) {
    int c = a;
    a = b;
    b = c;
  }
  if (b % a == 0)
    return a;
  else
    return gcd(a, b % a);
}
int main(void) {
  long long n, m;
  scanf("%lld%lld", &n, &m);
  char s[n + 5];
  char t[m + 5];
  scanf(" %s", s);
  scanf(" %s", t);
  long long x = gcd(n, m);
  for (int i = 0; i < x; i++) {
    if (s[(n / x) * i] != t[(m / x) * i]) {
      printf("-1\n");
      return 0;
    }
  }
  printf("%lld\n", (n / x) * m);
}
Problem p03231 (C++)

Buggy submission (status: Time Limit Exceeded):
/*Lucky_Glass*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll lena, lenb;
string A, B;
inline ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
int main() {
  cin >> lena >> lenb >> A >> B;
  ll len = lena * lenb / GCD(lena, lenb);
  ll fA = len / lena, fB = len / lenb;
  ll add = fA * fB / GCD(fA, fB);
  ll xA = 0, xB = 0;
  for (int i = 0; i < len; i += add) {
    if (A[xA] != B[xB]) {
      printf("-1\n");
      return 0;
    }
    xA = (xA + add / fA) % lena;
    xB = (xB + add / fB) % lenb;
  }
  printf("%lld\n", len);
  return 0;
}

Fixed submission:
/*Lucky_Glass*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll lena, lenb;
string A, B;
inline ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
int main() {
  cin >> lena >> lenb >> A >> B;
  ll len = lena * lenb / GCD(lena, lenb);
  ll fA = len / lena, fB = len / lenb;
  ll pos = 0;
  for (int i = 0; i < lena; i++, pos += fA) {
    if (pos % fB == 0)
      if (B[pos / fB] != A[i]) {
        printf("-1\n");
        return 0;
      }
  }
  printf("%lld\n", len);
  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}

int main() {
  ll n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;
  ll lon = n * (m / gcd(n, m));
  vector<char> tmp(lon, '.');
  for (ll i = 0; i < n; i++)
    tmp[i * (lon / n)] = s[i];
  for (ll i = 0; i < m; i++) {
    if (tmp[i * (lon / m)] == '.' || tmp[i * (lon / m)] == t[i]) {
    } else {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << lon << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}

int main() {
  ll n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;
  ll kou = gcd(n, m), lon = n * (m / kou);
  // vector<char> tmp (lon,'.');
  // for(ll i=0;i<n;i++)tmp[i*(lon/n)]=s[i];
  ll dn = lon / n, dm = lon / m;
  for (ll i = 0; i * dm < n && i * dn < m; i++) {
    if (s[i * dm] == t[i * dn]) {
    } else {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << lon << endl;
}
Problem p03231 (C++)

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

using namespace std;

long long LCM(long long a, long long b) {
  int r, x, tmp, lcm;

  x = a * b;

  /* 自然数 a > b を確認・入替 */
  if (a < b) {
    tmp = a;
    a = b;
    b = tmp;
  }

  /* ユークリッドの互除法 */
  r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }

  /* 最小公倍数を出力 */
  lcm = x / b;

  return lcm;
}

int main(void) {
  // 入力部
  // 文字数の入力
  long long N, M;
  cin >> N >> M;

  // 文字の入力
  string S, T;
  cin >> S;
  cin >> T;

  // 最小公倍数計算部
  long long lc = LCM(N, M);

  long long nd = lc / N;
  long long md = lc / M;

  // 計算部
  bool judge = true;
  for (long long i = 0, j = 0; i < N || j < M; i += md, j += nd) {
    if (S[i] != T[j])
      judge = false;
  }

  if (judge)
    cout << lc << endl;
  else
    cout << "-1" << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <iostream>
#include <numeric>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;

long long LCM(long long a, long long b) {
  long long r, x, tmp, lcm;

  x = a * b;

  /* 自然数 a > b を確認・入替 */
  if (a < b) {
    tmp = a;
    a = b;
    b = tmp;
  }

  /* ユークリッドの互除法 */
  r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }

  /* 最小公倍数を出力 */
  lcm = x / b;

  return lcm;
}

int main(void) {
  // 入力部
  // 文字数の入力
  long long N, M;
  cin >> N >> M;

  // 文字の入力
  string S, T;
  cin >> S;
  cin >> T;

  // 最小公倍数計算部
  long long lc = LCM(N, M);

  long long nd = lc / N;
  long long md = lc / M;

  // 計算部
  bool judge = true;
  for (long long i = 0, j = 0; i < N || j < M; i += md, j += nd) {
    if (S[i] != T[j])
      judge = false;
  }

  if (judge)
    cout << lc << endl;
  else
    cout << "-1" << endl;

  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define inf LLONG_MAX / 10000000
#define INF LLONG_MAX / 3
#define P pair<int, int>
#define EPS DBL_EPSILON
using namespace std;
int n, m;
string s, t;
int gcd(int a, int b) {
  while (max(a, b) % min(a, b)) {
    if (a < b)
      b %= a;
    else
      a %= b;
  }
  return min(a, b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
signed main() {
  cin >> n >> m >> s >> t;
  int l = lcm(n, m);
  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)]) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  cout << lcm(n, m) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define inf LLONG_MAX / 10000000
#define INF LLONG_MAX / 3
#define P pair<int, int>
#define EPS DBL_EPSILON
using namespace std;
int n, m;
string s, t;
int gcd(int a, int b) {
  while (max(a, b) % min(a, b)) {
    if (a < b)
      b %= a;
    else
      a %= b;
  }
  return min(a, b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
signed main() {
  cin >> n >> m >> s >> t;
  int l = lcm(n, m);
  for (int i = 0; i < l; i += l / n) {
    if (i % (l / m) == 0) {
      if (s[i / (l / n)] != t[i / (l / m)]) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  cout << lcm(n, m) << endl;
  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
int N, M;
char S[100010], T[100010];
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int main() {
  scanf("%d%d%s%s", &N, &M, S, T);
  for (int i = 0; i < N; i++) {
    if (1ll * i * M % N == 0 && S[i] != T[i * M / N])
      return puts("-1"), 0;
  }
  printf("%lld\n", 1ll * N * M / gcd(N, M));
}

Fixed submission:
#include <cstdio>
int N, M;
char S[100010], T[100010];
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int main() {
  scanf("%d%d%s%s", &N, &M, S, T);
  for (int i = 0; i < N; i++) {
    if (1ll * i * M % N == 0 && S[i] != T[1ll * i * M / N])
      return puts("-1"), 0;
  }
  printf("%lld\n", 1ll * N * M / gcd(N, M));
}
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) {
  while (1) {
    if (a < b)
      swap(a, b);
    if (!b)
      break;
    a %= b;
  }
  return a;
}

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

#define ll long long
#define rep(i, x, y) for (int i = x; i < y; i++)
#define rel(i, x, y) for (int i = x - 1; i >= y; i--)
#define all(x) x.begin(), x.end()

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  ll n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;
  bool ok = true;
  ll i = 0;
  ll ans = lcm(n, m);
  ll sn = ans / n;
  ll mn = ans / m;
  rep(i, 0, ans) {
    // if(i%n==0 && i%m==0) cout << s[i/n] << " " <<  t[i/m] << endl;
    if (i % sn == 0 && i % mn == 0 && s[i / sn] != t[i / mn]) {
      cout << "-1" << endl;
      return 0;
    }
  }
  cout << ans << endl;
}

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

long long gcd(long long a, long long b) {
  while (1) {
    if (a < b)
      swap(a, b);
    if (!b)
      break;
    a %= b;
  }
  return a;
}

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

#define ll long long
#define rep(i, x, y) for (int i = x; i < y; i++)
#define rel(i, x, y) for (int i = x - 1; i >= y; i--)
#define all(x) x.begin(), x.end()

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  ll n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;
  bool ok = true;
  ll i = 0;
  ll ans = lcm(n, m);
  ll sn = ans / n;
  ll mn = ans / m;
  for (ll i = 0; i < ans; i += lcm(sn, mn)) {
    if (s[i / sn] != t[i / mn]) {
      cout << "-1" << endl;
      return 0;
    }
  }
  cout << ans << endl;
}
Problem p03231 (C++)

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

typedef long long ll;
const ll INF = (ll)1e18 + 1;
const ll MOD = 1e9 + 7;

void print() { std::cout << std::endl; }
template <typename Head> void print(Head &&head) {
  std::cout << head << std::endl;
}
template <typename Head, typename... Tail>
void print(Head &&head, Tail &&...tail) {
  std::cout << head << " ";
  print(std::forward<Tail>(tail)...); // or print(tail...);
}
template <typename Iterator> void printVec(Iterator begin, Iterator end) {
  for (Iterator it = begin; it != end; it++) {
    std::cout << "[" << std::distance(begin, it) << "] " << *it << " ";
    if (std::distance(begin, it) % 20 == 19) {
      std::cout << std::endl;
    }
  }
  std::cout << std::endl;
}

ll gcd(ll a, ll b) {
  while (b > 0) {
    a %= b;
    std::swap(a, b);
  }
  return a;
}

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

int main(int argc, char *argv[]) {
  ll N, M;
  std::cin >> N >> M;

  std::string s, t;
  std::cin >> s >> t;

  ll L = lcm(N, M);
  for (ll i = 0; i < L; i++) {
    if (i % (L / N) == 0 && i % (L / M) == 0 &&
        s[i / (L / N)] != t[i / (L / M)]) {
      L = -1;
      break;
    }
  }

  print(L);
  return 0;
}


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

typedef long long ll;
const ll INF = (ll)1e18 + 1;
const ll MOD = 1e9 + 7;

void print() { std::cout << std::endl; }
template <typename Head> void print(Head &&head) {
  std::cout << head << std::endl;
}
template <typename Head, typename... Tail>
void print(Head &&head, Tail &&...tail) {
  std::cout << head << " ";
  print(std::forward<Tail>(tail)...); // or print(tail...);
}
template <typename Iterator> void printVec(Iterator begin, Iterator end) {
  for (Iterator it = begin; it != end; it++) {
    std::cout << "[" << std::distance(begin, it) << "] " << *it << " ";
    if (std::distance(begin, it) % 20 == 19) {
      std::cout << std::endl;
    }
  }
  std::cout << std::endl;
}

ll gcd(ll a, ll b) {
  while (b > 0) {
    a %= b;
    std::swap(a, b);
  }
  return a;
}

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

int main(int argc, char *argv[]) {
  ll N, M;
  std::cin >> N >> M;

  std::string s, t;
  std::cin >> s >> t;

  ll L = lcm(N, M);
  ll pitch = L / gcd(N, M);
  for (ll i = 0; i < L; i += pitch) {
    if (i % (L / N) == 0 && i % (L / M) == 0 &&
        s[i / (L / N)] != t[i / (L / M)]) {
      L = -1;
      break;
    }
  }

  print(L);
  return 0;
}
Problem p03231 (C++)

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

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

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

int main() {
  int N, M;
  cin >> N >> M;
  string S, T;
  cin >> S >> T;

  ll L = N * M / gcd(N, M);
  bool e = false;
  REP(i, std::min(N, M)) {
    if (N >= M) {
      if ((i * L / M) % (L / N) != 0)
        continue;
      int j = (i * L / M) / (L / N);
      if (S[j] == T[i])
        continue;
    } else {
      if ((i * L / N) % (L / M) != 0)
        continue;
      int j = (i * L / N) / (L / M);
      if (S[i] == T[j])
        continue;
    }
    e = true;
    break;
  }

  if (e)
    cout << -1 << endl;
  else
    cout << L << endl;
  return 0;
}

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

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

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

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

  ll L = N * M / gcd(N, M);
  bool e = false;
  REP(i, std::min(N, M)) {
    if (N >= M) {
      if ((i * L / M) % (L / N) != 0)
        continue;
      int j = (i * L / M) / (L / N);
      if (S[j] == T[i])
        continue;
    } else {
      if ((i * L / N) % (L / M) != 0)
        continue;
      int j = (i * L / N) / (L / M);
      if (S[i] == T[j])
        continue;
    }
    e = true;
    break;
  }

  if (e)
    cout << -1 << endl;
  else
    cout << L << endl;
  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fo(i, a, b) for (int i = a; i <= b; i++)
long long N, M, L, D, l, ans;
char s[110000], t[110000];
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
inline void chec(long long d) {
  if (d <= ans)
    return;
  l = L / d;
  long long pos, p, q, b1 = l / N, b2 = l / M, te = b1 / gcd(b1, b2) * b2,
                       num = L / te;
  fo(i, 0, num - 1) {
    pos = te * i;
    p = pos / b1;
    q = pos / b2;
    if (s[p + 1] != t[q + 1])
      return;
  }
  ans = d;
}
int main() {
  scanf("%d%d%s%s", &N, &M, s + 1, t + 1);
  D = gcd(N, M);
  L = N * M;
  ans = -1;
  for (long long i = 1; i * i <= D; i++) {
    chec(i);
    if (i * i < D)
      chec(D / i);
  }
  if (ans > 0)
    ans = L / ans;
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define fo(i, a, b) for (int i = a; i <= b; i++)
long long N, M, L, D, l, ans;
char s[110000], t[110000];
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
inline void chec(long long d) {
  if (d <= ans)
    return;
  l = L / d;
  long long pos, p, q, b1 = l / N, b2 = l / M, te = b1 / gcd(b1, b2) * b2,
                       num = l / te; // num=L/te!!!!
  fo(i, 0, num - 1) {
    pos = te * i;
    p = pos / b1;
    q = pos / b2;
    if (s[p + 1] != t[q + 1])
      return;
  }
  ans = d;
}
int main() {
  scanf("%d%d%s%s", &N, &M, s + 1, t + 1);
  D = gcd(N, M);
  L = N * M;
  ans = -1;
  for (long long i = 1; i * i <= D; i++) {
    chec(i);
    if (i * i < D)
      chec(D / i);
  }
  if (ans > 0)
    ans = L / ans;
  printf("%lld\n", ans);
  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
typedef long long ll;
using namespace std;
const int MOD = 1000000007;
const int INF = 1010000000;
const double EPS = 1e-10;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

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

int main() {
  ll n, m;
  string s, t;
  cin >> n >> m >> s >> t;
  ll l = lcm(n, m);
  // bool can = true;
  // int a = max(n,m), b = min(n,m);
  // string c = (n==a?s:t), d = (m==b?t:s);
  /*vector<char> x(l);
  rep(i,b){
    x[l/b*i] = d[i];
  }*/
  ll lim = n * m;
  for (int i = 0; i < gcd(n, m); i++) {
    /*  if(x[lim*i] && (x[lim*i] != c[i*lim*a/l])){
        can = false;
        break;
      }*/
    if (s[i * n] != t[i * m]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << l << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
typedef long long ll;
using namespace std;
const int MOD = 1000000007;
const int INF = 1010000000;
const double EPS = 1e-10;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

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

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

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

int64_t gcd(int64_t a, int64_t b) { return b == 0 ? a : gcd(b, a % b); }
int64_t lcm(int64_t a, int64_t b) { return a * b / gcd(a, b); }

int main() {
  int64_t i, j, k;
  int64_t N, M;
  string S, T;
  cin >> N >> M >> S >> T;
  int64_t L = lcm(N, M);
  int64_t l = lcm(L / N, L / M);
  for (i = 0; i < L; i += l) {
    if (S[l / (L / N) * i] != T[l / (L / M) * i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << L << endl;
  return 0;
}

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

int64_t gcd(int64_t a, int64_t b) { return b == 0 ? a : gcd(b, a % b); }
int64_t lcm(int64_t a, int64_t b) { return a * b / gcd(a, b); }

int main() {
  int64_t i, j, k;
  int64_t N, M;
  string S, T;
  cin >> N >> M >> S >> T;
  int64_t L = lcm(N, M);
  int64_t l = lcm(L / N, L / M);
  for (i = 0; i < L; i += l) {
    if (S[i / (L / N)] != T[i / (L / M)]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << L << endl;
  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <math.h>
#include <memory.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <utility>
#include <vector>
using namespace std;

long long gcd(long long a, long long b) {
  long long c;
  while (b) {
    c = a;
    a = b;
    b = c % b;
  }
  return a;
}

char s[100001], t[100001];

int main() {
  long long N, M;
  scanf("%lld %lld %s %s", &N, &M, s, t);
  long long g = gcd(N, M);
  long long L = (N * M) / g;
  long long ni = 0, mi = 0, nd = L / N, md = L / M;
  while (ni < L && mi < L) {
    if (ni < mi)
      ni += nd;
    else if (mi < ni)
      mi += md;
    else {
      if (s[ni] != t[mi])
        return !printf("-1\n");
      ni += nd;
      mi += md;
    }
  }
  printf("%lld\n", L);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <functional>
#include <math.h>
#include <memory.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <utility>
#include <vector>
using namespace std;

long long gcd(long long a, long long b) {
  long long c;
  while (b) {
    c = a;
    a = b;
    b = c % b;
  }
  return a;
}

char s[100001], t[100001];

int main() {
  long long N, M;
  scanf("%lld %lld %s %s", &N, &M, s, t);
  long long g = gcd(N, M);
  long long L = (N * M) / g;
  long long ni = 0, mi = 0, nd = L / N, md = L / M;
  while (ni < L && mi < L) {
    if (ni < mi)
      ni += nd;
    else if (mi < ni)
      mi += md;
    else {
      if (s[ni / nd] != t[mi / md])
        return !printf("-1\n");
      ni += nd;
      mi += md;
    }
  }
  printf("%lld\n", L);
  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#include <algorithm>
#include <array>
#include <cstring>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

int gcd(int x, int y) {
  x = std::abs(x);
  y = std::abs(y);
  int a = std::max(x, y), b = std::min(x, y);
  int r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}

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

int main(void) {
  int N, M;
  std::string S, T;
  scanf("%d %d", &N, &M);
  std::cin >> S >> T;
  long long l = lcm(N, M);
  int g = gcd(N, M);
  int t;
  for (int i = 0; (t = i * lcm(l / N, l / M)) < l; ++i) {
    if (S[i * (l / M)] != T[i * (l / N)]) {
      puts("-1");
      return 0;
    }
  }
  printf("%lld\n", l);
  return 0;
}

Fixed submission:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#include <algorithm>
#include <array>
#include <cstring>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

int gcd(int x, int y) {
  x = std::abs(x);
  y = std::abs(y);
  int a = std::max(x, y), b = std::min(x, y);
  int r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}

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

int main(void) {
  int N, M;
  std::string S, T;
  scanf("%d %d", &N, &M);
  std::cin >> S >> T;
  long long l = lcm(N, M);
  for (int i = 0; i * lcm(l / N, l / M) < l; ++i) {
    if (S[i * (l / M)] != T[i * (l / N)]) {
      puts("-1");
      return 0;
    }
  }
  printf("%lld\n", l);
  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define forn(i, n) for (ll i = 0; i < (n); i++)
#define fori(i, b, e) for (ll i = (b); i < (e); i++)
#define forr(i, b, e) for (ll i = (b); i >= (e); i--)
#define allele(v) v.begin(), v.end()
#define fi first
#define se second

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;

  ll ans = n * m / __gcd(n, m);
  string str;
  str.resize(ans, '*');
  forn(i, n) str[i * (ans / n)] = s[i];
  forn(i, m) {
    char c = str[i * (ans / m)];
    if (c != '*' && c != t[i])
      return cout << -1, 0;
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define forn(i, n) for (ll i = 0; i < (n); i++)
#define fori(i, b, e) for (ll i = (b); i < (e); i++)
#define forr(i, b, e) for (ll i = (b); i >= (e); i--)
#define allele(v) v.begin(), v.end()
#define fi first
#define se second

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;

  ll ans = n * m / __gcd(n, m);
  ll a = ans / m, b = ans / n;
  forn(i, n / a) {
    if (s[i * a] != t[i * b])
      return cout << -1, 0;
  }
  cout << ans;
  return 0;
}
Problem p03231 (C++)

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

long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }

int main() {
  long long N, M;
  string S, T;
  cin >> N >> M;
  cin >> S;
  cin >> T;
  long long L = N * M / gcd(N, M);

  int p = L / N;
  int q = L / M;
  int r = p * q / gcd(p, q);
  for (int i = 0; i < L; i = i + r) {
    if (!(S.at(i / p) == T.at(i / q))) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << setprecision(20) << L << endl;
  return 0;
}


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

long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }

int main() {
  long long N, M;
  string S, T;
  cin >> N >> M;
  cin >> S;
  cin >> T;
  long long L = N * M / gcd(N, M);

  long long p = L / N;
  long long q = L / M;
  long long r = p * q / gcd(p, q);
  for (long long i = 0; i < L; i = i + r) {
    if (!(S.at(i / p) == T.at(i / q))) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << setprecision(20) << L << endl;
  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int n, m;
string s, t;

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

  return gcd(b, a % b);
}

int main() {

  cin >> n >> m;
  cin >> s >> t;

  long long len = n;
  len = len * m;

  int g = gcd(n, m);

  len /= g;

  int a = len / n, b = len / m;

  bool flag = true;
  int idx1 = 0, idx2 = 0, count1 = 0, count2 = 0;
  while (idx1 < len && idx2 < len) {
    if (idx1 == idx2) {
      if (s[count1] != t[count2]) {
        flag = false;
        break;
      }

      idx1 += a;
      idx2 += b;

      ++count1;
      ++count2;
    } else if (idx1 < idx2) {
      idx1 += a;
      ++count1;
    } else {
      idx2 += b;
      ++count2;
    }
  }

  if (flag)
    cout << len << endl;
  else
    cout << -1 << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int n, m;
string s, t;

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

  return gcd(b, a % b);
}

int main() {

  cin >> n >> m;
  cin >> s >> t;

  long long len = n;
  len = len * m;

  int g = gcd(n, m);

  len /= g;

  int a = len / n, b = len / m;

  bool flag = true;
  int idx1 = 0, idx2 = 0, count1 = 0, count2 = 0;
  while (count1 < n && count2 < m) {
    if (idx1 == idx2) {
      if (s[count1] != t[count2]) {
        flag = false;
        break;
      }

      idx1 += a;
      idx2 += b;

      ++count1;
      ++count2;
    } else if (idx1 < idx2) {
      idx1 += a;
      ++count1;
    } else {
      idx2 += b;
      ++count2;
    }
  }

  if (flag)
    cout << len << endl;
  else
    cout << -1 << endl;

  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vt<T>>;
template <class T> using ttt = tuple<T, T>;
using tii = tuple<int, int>;
using tiii = tuple<int, int, int>;
using vi = vector<int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define mt make_tuple
#define ALL(a) (a).begin(), (a).end()
#define FST first
#define SEC second
#define DEB cerr << "!" << endl
#define SHOW(a, b) cerr << (a) << " " << (b) << endl
#define DIV int(1e9 + 7)
const int INF = (INT_MAX / 2);
const ll LLINF = (LLONG_MAX / 2);
const double eps = 1e-8;
// const double PI = M_PI;
inline ll pow(ll x, ll n, ll m) {
  ll r = 1;
  while (n > 0) {
    if ((n & 1) == 1)
      r = r * x % m;
    x = x * x % m;
    n >>= 1;
  }
  return r % m;
}
inline ll lcm(ll d1, ll d2) { return d1 / __gcd(d1, d2) * d2; }

/*Coding Space*/
char ans[200000] = {};
int main() {
  int n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;
  ll g = lcm(n, m);

  rep(i, n) {
    int index = g * i / n;
    // cerr << index <<" " <<  s[i]<<endl;
    ans[index] = s[i];
  }
  rep(i, m) {
    int index = g * i / m;
    // cerr << index <<" " <<  t[i]<<endl;
    if (ans[index] != 0 && ans[index] != t[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << g << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vt<T>>;
template <class T> using ttt = tuple<T, T>;
using tii = tuple<int, int>;
using tiii = tuple<int, int, int>;
using vi = vector<int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define mt make_tuple
#define ALL(a) (a).begin(), (a).end()
#define FST first
#define SEC second
#define DEB cerr << "!" << endl
#define SHOW(a, b) cerr << (a) << " " << (b) << endl
#define DIV int(1e9 + 7)
const int INF = (INT_MAX / 2);
const ll LLINF = (LLONG_MAX / 2);
const double eps = 1e-8;
// const double PI = M_PI;
inline ll pow(ll x, ll n, ll m) {
  ll r = 1;
  while (n > 0) {
    if ((n & 1) == 1)
      r = r * x % m;
    x = x * x % m;
    n >>= 1;
  }
  return r % m;
}
inline ll lcm(ll d1, ll d2) { return d1 / __gcd(d1, d2) * d2; }

/*Coding Space*/
unordered_map<ll, char> ans;
int main() {
  int n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;
  ll g = lcm(n, m);

  rep(i, n) {
    int index = g * i / n;
    // cerr << index <<" " <<  s[i]<<endl;
    ans[index] = s[i];
  }
  rep(i, m) {
    int index = g * i / m;
    // cerr << index <<" " <<  t[i]<<endl;
    if (ans[index] != 0 && ans[index] != t[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << g << endl;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
char a[10005], b[10005];
map<long long, char> vis;
long long gcd(long long x, long long y) {
  while (y) {
    x %= y;
    swap(x, y);
  }
  return x;
}
int main() {
  long long n, m;
  long long orz;
  cin >> n >> m;
  scanf("%s%s", a + 1, b + 1);
  orz = n * m / gcd(n, m);
  for (long long i = 1; i <= n; i++) {
    vis[(i - 1) * orz / n + 1] = a[i];
    // cout<<(i-1)*(orz/n)+1<<" ";
  }
  // cout<<endl;
  // cout<<orz/m<<endl;
  for (long long i = 1; i <= m; i++) {
    // cout<<(i-1)*(orz/m)+1<<" ";
    if (vis[(i - 1) * orz / m + 1]) {
      if (vis[(i - 1) * orz / m + 1] != b[i]) {
        printf("-1");
        return 0;
      }
    }
  }
  cout << orz << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
char a[100005], b[100005];
map<long long, char> vis;
long long gcd(long long x, long long y) {
  while (y) {
    x %= y;
    swap(x, y);
  }
  return x;
}
int main() {
  long long n, m;
  long long orz;
  cin >> n >> m;
  scanf("%s%s", a + 1, b + 1);
  orz = n * m / gcd(n, m);
  for (long long i = 1; i <= n; i++) {
    vis[(i - 1) * orz / n + 1] = a[i];
    // cout<<(i-1)*(orz/n)+1<<" ";
  }
  // cout<<endl;
  // cout<<orz/m<<endl;
  for (long long i = 1; i <= m; i++) {
    // cout<<(i-1)*(orz/m)+1<<" ";
    if (vis[(i - 1) * orz / m + 1]) {
      if (vis[(i - 1) * orz / m + 1] != b[i]) {
        printf("-1");
        return 0;
      }
    }
  }
  cout << orz << endl;
  return 0;
}
Problem p03231 (C++)

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

using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(i) (i).begin(), (i).end()
#define rall(i) (i).begin(), (i).end(), greater<int>()
#define int long long
#define elif else if
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fst first
#define sec second
#define mod 1000000007
#define INF 10e10
template <typename T>

void print(T x) {
  std::cout << x << " ";
}
template <typename T>

void println(T x) {
  std::cout << x << "\n";
}

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

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

signed main(void) {
  int n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;
  int xsize = lcm(n, m);
  vector<char> x(xsize, '0');
  rep(i, n) { x[i * xsize / n] = s[i]; }
  rep(i, m) {
    if (x[i * xsize / m] != '0' && x[i * xsize / m] != t[i]) {
      print("-1");
      return 0;
    }
  }
  print(xsize);
}

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

using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(i) (i).begin(), (i).end()
#define rall(i) (i).begin(), (i).end(), greater<int>()
#define int long long
#define elif else if
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fst first
#define sec second
#define mod 1000000007
#define INF 10e10
template <typename T>

void print(T x) {
  std::cout << x << " ";
}
template <typename T>

void println(T x) {
  std::cout << x << "\n";
}

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

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

signed main(void) {
  int n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;
  int xsize = lcm(n, m);
  int a = xsize / n;
  int b = xsize / m;
  for (int i = 0; i * a < xsize; i++) {
    if (i * a % b == 0) {
      if (s[i] != t[i * a / b]) {
        print("-1");
        return 0;
      }
    }
  }
  print(xsize);
}
Problem p03232 (Python)

Buggy submission (status: Runtime Error):
import sys

fin = sys.stdin.readline


def factorial(n, mod):
    if n == 0:
        return 1
    else:
        return n * factorial(n - 1, mod) % mod


MOD = 10**9 + 7
N = int(fin())
A_list = [int(elem) for elem in fin().split()]

fac_N = factorial(N, MOD)
inv_nums = [fac_N * pow(i, MOD - 2, MOD) % MOD for i in range(1, N + 1)]
cuml_inv_nums = [inv_nums[0]]
for inv_num in inv_nums[1:]:
    cuml_inv_nums.append((cuml_inv_nums[-1] + inv_num) % MOD)

ans = 0
for i, A in enumerate(A_list):
    ans += A * (cuml_inv_nums[i] + cuml_inv_nums[N - 1 - i] - cuml_inv_nums[0]) % MOD
    ans %= MOD

print(ans)


Fixed submission:
import sys

fin = sys.stdin.readline
sys.setrecursionlimit(200000)


def factorial(n, mod):
    if n == 0:
        return 1
    else:
        return n * factorial(n - 1, mod) % mod


MOD = 10**9 + 7
N = int(fin())
A_list = [int(elem) for elem in fin().split()]

fac_N = factorial(N, MOD)
inv_nums = [fac_N * pow(i, MOD - 2, MOD) % MOD for i in range(1, N + 1)]
cuml_inv_nums = [inv_nums[0]]
for inv_num in inv_nums[1:]:
    cuml_inv_nums.append((cuml_inv_nums[-1] + inv_num) % MOD)

ans = 0
for i, A in enumerate(A_list):
    ans += A * (cuml_inv_nums[i] + cuml_inv_nums[N - 1 - i] - cuml_inv_nums[0]) % MOD
    ans %= MOD

print(ans)