Problem p03231 (python)

Fixed submission:
import platform
ver = platform.python_version_tuple()
if int(ver[1]) < 5:
    from fractions import gcd
else:
    from math import gcd

from sys import stdin
from bisect import bisect

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

    L = lcm(N, M)
    s = [i * (L // N) + 1 for i in range(N)]
    t = [i * (L // M) + 1 for i in range(M)]
    for i, x in enumerate(s):
        n = bisect(t, x)
        if t[n-1] == x:
            if T[n-1] != S[i]:
                print(-1)
                return
    print(L)

input = lambda: stdin.readline().rstrip()
lcm = lambda x, y: x * y // gcd(x, y)
main()

Buggy submission (verdict: Runtime Error):
import platform
ver = platform.python_version_tuple()
if int(ver[1]) < 5:
    from fractions import gcd
else:
    from math import gcd
from sys import stdin

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

    L = lcm(N, M)
    ans = [None] * 10000000000
    for i in range(N):
        j = i * (L // N)
        if ans[j] is None or ans[j] == S[i]:
            ans[j] = S[i]
        else:
            print(-1)
            return
    for i in range(M):
        j = i * (L // M)
        if ans[j] is None or ans[j] == T[i]:
            ans[j] = T[i]
        else:
            print(-1)
            return
    print(L)

input = lambda: stdin.readline().rstrip()
lcm = lambda x, y: x * y // gcd(x, y)
main()

Buggy submission (verdict: Runtime Error):
import platform
ver = platform.python_version_tuple()
if int(ver[1]) < 5:
    from fractions import gcd
else:
    from math import gcd
from sys import stdin

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

    L = lcm(N, M)
    ans = [None] * 100000
    for i in range(N):
        j = i * (L // N)
        if ans[j] is None or ans[j] == S[i]:
            ans[j] = S[i]
        else:
            print(-1)
            return
    for i in range(M):
        j = i * (L // M)
        if ans[j] is None or ans[j] == T[i]:
            ans[j] = T[i]
        else:
            print(-1)
            return
    print(L)

input = lambda: stdin.readline().rstrip()
lcm = lambda x, y: x * y // gcd(x, y)
main()

Buggy submission (verdict: Runtime Error):
import platform
ver = platform.python_version_tuple()
if int(ver[1]) < 5:
    from fractions import gcd
else:
    from math import gcd
from sys import stdin

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

    L = lcm(N, M)
    ans = [None] * L
    for i in range(N):
        j = i * (L // N)
        if ans[j] is None or ans[j] == S[i]:
            ans[j] = S[i]
        else:
            print(-1)
            return
    for i in range(M):
        j = i * (L // M)
        if ans[j] is None or ans[j] == T[i]:
            ans[j] = T[i]
        else:
            print(-1)
            return
    print(L)

input = lambda: stdin.readline().rstrip()
lcm = lambda x, y: x * y // gcd(x, y)
main()

Buggy submission (verdict: Runtime Error):
import platform
ver = platform.python_version_tuple()
if int(ver[1]) < 5:
    from fractions import gcd
else:
    from math import gcd
from sys import stdin

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

    L = lcm(N, M)
    ans = [None] * L
    for j in range(N):
        k = j * (L // N)
        if ans[k] is None or ans[k] in S[j]:
            ans[k] = S[j]
        else:
            print(-1)
            return
    for j in range(M):
        k = j * (L // M)
        if ans[k] is None or ans[k] in T[j]:
            ans[k] = T[j]
        else:
            print(-1)
            return
    print(L)

input = lambda: stdin.readline().rstrip()
lcm = lambda x, y: x * y // gcd(x, y)
main()