Problem:


Score : 300 points

Problem Statement

You are given a string S of length N and another string T of length M.
These strings consist of lowercase English letters.

A string X is called a good string when the following conditions are all met:

Let L be the length of X. L is divisible by both N and M.

Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.

Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.

Determine if there exists a good string. If it exists, find the length of the shortest such string.

Constraints

1 \leq N,M \leq 10^5

S and T consist of lowercase English letters.

|S|=N

|T|=M

Input

Input is given from Standard Input in the following format:

N M
S
T

Output

If a good string does not exist, print -1; if it exists, print the length of the shortest such string.

Sample Input 1

3 2
acp
ae

Sample Output 1

6

For example, the string accept is a good string.
There is no good string shorter than this, so the answer is 6.

Sample Input 2

6 3
abcdef
abc

Sample Output 2

-1

Sample Input 3

15 9
dnsusrayukuaiia
dujrunuma

Sample Output 3

45



Sample input:
3 2
acp
ae


Sample output:
6

Submission status: Wrong Answer

Solution code:
import itertools
import numpy as np
import sys
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()
def gcd(a,b):
    while b % a:
        a,b = b, a % b
    return a

N,M = IL()
s = S()
t = S()

agcd = gcd(N,M)
bgcd = N//agcd

if agcd == 1:
    print(N*M//agcd)
else:
    if N > M:
        if t[::agcd] == s[::bgcd]:
            print(N*M//agcd)
        else:
            print(-1)
    else:
        if s[::agcd] == t[::bgcd]:
            print(N*M//agcd)
        else:
            print(-1)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
from collections import defaultdict
def lcm(n,m): return n*m//gcd(n,m)
def func(i,l,n): return i*l//n + 1
N,M = map(int,input().split())
S = input()
T = input()
l = lcm(N,M)
dicS = defaultdict(int)
for i in range(1,len(S)):
    dicS[i+1] = func(i,l,N)
dicT = defaultdict(int)
for i in range(1,len(T)):
    dicT[func(i,l,M)] = i+1

flag = True
for i in range(1,N+1):
    if dicS[i] in dicT.keys():
        if S[i-1] != T[dicT[dicS[i]]-1]:
            flag = False
            break
if flag: print(l)
else: print(-1)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
def lcm(n,m): return n*m//gcd(n,m)
N,M = map(int,input().split())
S = input()
T = input()
l = lcm(N,M)
if max(N,M) < l: print(l)
else: print(-1)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
n,m = map(int, input().split())
s = input()
t = input()
# len(s) < len(t)
if m < n:
    tmp=s
    s=t
    t=tmp
    tmp=m
    m=n
    n=tmp

def lcm(a,b):
    return a*b/gcd(a,b)
# print(n,m,s,t)
if m % n ==0:
    for i,x in enumerate(t[0::n]):
        # print(x, s[i])
        if s[i] != x:
            print(-1)
            break
    else:
        print(int(lcm(n,m)))
else:
    print(int(lcm(n,m)))

Submission status: Wrong Answer

Solution code:
from fractions import gcd
n,m = map(int, input().split())
s = input()
t = input()
# len(s) < len(t)
if m < n:
    tmp=s
    s=t
    t=tmp
    tmp=m
    m=n
    n=tmp

def lcm(a,b):
    return a*b/gcd(a,b)

if m % n ==0:
    for i,x in enumerate(s[0::n]):
        if s[i] != x:
            print(-1) 
            break   
    print(int(lcm(n,m)))
else:
    print(int(lcm(n,m)))

Submission status: Wrong Answer

Solution code:
from fractions import gcd
n,m = map(int, input().split())
s = input()
t = input()

def lcm(a,b):
    return a*b/gcd(a,b)

if n % m==0 or m % n ==0:
    print(-1)
else:
    print(int(lcm(n,m)))

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
if N>=M:
    A=input()
    B=input()
else:
    B=input()
    A=input()

def gcd(A,B):
    while A%B!=0:
        A,B=B,A%B
    return B
    
if N==M:
    if A==B:
        print(N)
    else:
        print(-1)
elif len(A)%len(B)==0:
    if A==B*(len(A)//len(B)):
        print(len(A))
    else:
        print(-1)
else:
    if A[0]==B[0]:
        print(len(A)*len(B)//gcd(len(A),len(B)))
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())

a=max(N,M)
b=min(N,M)
S=input()
T=input()

if a%b==0:
    if len(S)>=len(T):
        if S==T*(len(S)//len(T)):
            print(len(S))
        else:
            print(-1)
    else:
        if T==S*(len(T)//len(S)):
            print(len(T))
        else:
            print(-1)
else:
    while b!=0:
        c=a%b
        a=max(c,b)
        b=min(c,b)
    if S[0]==T[0]:
        print(len(S)*len(T)//a)
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split(" "))
ar = list(input())
br = list(input())
i = max(n,m)
j = min(n,m)
if ar[0] != br[0]:
    print(-1)
    exit()
while True:
    if i % j == 0:
        break
    else:
        i += n
if max(i // n,i // m) % min(i // n,i // m) != 0:
    print(i)
else:
    if min(n,m) == n:
        for r in ar:
            for i in range(0,m,i // n):
                if br[i] != r:
                    print(-1)
                    exit()
    else:
        for r in br:
            for i in range(0,n,i // m):
                if br[i] != r:
                    print(-1)
                    exit()
    print(i)

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


N, M = map(int, input().split())
if N >= M:
    s1 = input()
    s2 = input()
else:
    N, M = M, N
    s2 = input()
    s1 = input()


g = gcd(N, M)
lcm = N*M//g
ans = lcm
if N == M:
    if s1 == s2:
        ans = lcm
    else:
        ans = -1
elif N % M == 0:
    if any(i != j for i, j in zip(s1[::N//g], s2[::M//g])):
        ans = -1
    else:
        ans = lcm
else:
    if s1[0] == s2[0]:
        ans = lcm
    else:
        ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


N, M = map(int, input().split())
if N >= M:
    s1 = input()
    s2 = input()
else:
    N, M = M, N
    s2 = input()
    s1 = input()


lcm = N*M//gcd(N, M)
ans = lcm
if N == M:
    if s1 == s2:
        ans = lcm
    else:
        ans = -1
elif N % M == 0:
    if any(i != j for i, j in zip(s1[::lcm//M], s2[::lcm//N])):
        ans = -1
    else:
        ans = lcm
else:
    if s1[0] == s2[0]:
        ans = lcm
    else:
        ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


N, M = map(int, input().split())
if N >= M:
    s1 = input()
    s2 = input()
else:
    N, M = M, N
    s2 = input()
    s1 = input()


lcm = N*M//gcd(N, M)
ans = lcm
if N == M:
    if s1 == s2:
        ans = lcm
    else:
        ans = -1
elif N % M == 0:
    if any(i != j for i, j in zip(s1[::lcm//N], s2[::lcm//M])):
        ans = -1
    else:
        ans = lcm
else:
    if s1[0] == s2[0]:
        ans = lcm
    else:
        ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


N, M = map(int, input().split())
if N >= M:
    s1 = input()
    s2 = input()
else:
    N, M = M, N
    s2 = input()
    s1 = input()


lcm = N*M//gcd(N, M)
ans = lcm
if N == M:
    if s1 == s2:
        ans = lcm
    else:
        ans = -1
elif N % M == 0:
    if any(i != j for i, j in zip(s1[::lcm//N], s2[::lcm//M])):
        ans = -1
    else:
        ans = lcm
else:
    ans = lcm
print(ans)

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


N, M = map(int, input().split())
if N >= M:
    s1 = input()
    s2 = input()
else:
    N, M = M, N
    s2 = input()
    s1 = input()


lcm = N*M//gcd(N, M)
ans = lcm
if N == M and s1 == s2:
    ans = lcm
elif N % M == 0:
    if any(i != j for i, j in zip(s1[::lcm//N], s2[::lcm//M])):
        ans = -1
    else:
        ans = lcm
else:
    ans = lcm
print(ans)

Submission status: Wrong Answer

Solution code:

def gcd(a, b):
    while b:
            a,b=b,a%b
    return a
def lcm(x,y):
    return (x * y) // gcd(x, y)

def atwo(n,m,s,t):
    l=lcm(n,m)
    gc=gcd(n,m)
    if s[0::n//gc]==t[0::m//gc]:
        print(l)
        exit()
    else:
        print(-1)
        exit()

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


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

def lcm(a,b):
    return (a // gcd(a,b)) * b

n,m = li()
s = ns()
t = ns()


if n%m != 0 and m%n != 0:
    if s[0] == t[0]:
        print(lcm(n,m))
    else:
        print(-1)

else:
    if n%m == 0:
        ans = n
        for i in range(m):
            if s[(n//m)*i] != t[i]:
                ans = -1
                
    elif m%n == 0:
        ans = m
        for i in range(n):
            if t[(m//n)*i] != s[i]:
                ans = -1
                
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


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

def lcm(a,b):
    return (a // gcd(a,b)) * b

n,m = li()
s = ns()
t = ns()


if n%m != 0 and m%n != 0:
    if s[0] == t[0]:
        print(lcm(n,m))
    else:
        print(-1)

else:
    if n%m == 0:
        ans = n
        for i in range(m):
            if s[(n//m)*i] != t[i]:
                ans = -1
                
    elif m%n == 0:
        ans = m
        for i in range(n):
            if t[(m//n)*i] != s[i]:
                ans = -1
                
    print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys, re
from collections import deque, defaultdict, Counter
from math import sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappop, heappush, heapify, heappushpop
from bisect import bisect_left, bisect_right
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from functools import reduce, partial
from fractions import Fraction
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def ceil(a, b=1): return int(-(-a // b))
def round(x): return int((x*2+1) // 2)
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

def gcd(a, b):
    while b > 0:
        a, b = b, a%b
    return a
def lcm_base(x, y):
    return (x * y) // gcd(x, y)

N, M = MAP()
S = input()
T = input()

lcm = lcm_base(N, M)
N_gap = lcm // N
M_gap = lcm // M
for i in range(1, lcm):
    if i % N_gap == 0 and i % M_gap == 0:
        if S[i//N_gap] != T[i//M_gap]:
            print(-1)
            exit()
print(lcm_base(N, M))

Submission status: Wrong Answer

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

X=max(N,M)
Y=min(N,M)

while X%Y!=0:
    X,Y=max(X%Y,Y),min(X%Y,Y)
    
lcm=N*M//Y

if S[0]!=T[0]:
    print(-1)
elif max(N,M)%min(N,M)!=0:
    print(lcm)
else:
    f=0
    if N>M and N%M==0:
        for i in range(N//M):
            if S[i*(N//M)]==T[i]:
                pass
            else:
                f+=1
        if f==0:
            print(N)
        else:
            print(-1)
    elif M>N and M%N==0:
        for i in range(M//N):
            if T[i*(M//N)]==S[i]:
                pass
            else:
                f+=1
        if f==0:
            print(M)
        else:
            prnt(-1)
    elif N==M:
        if S==T:
            print(N)
        else:
            print(-1)

Submission status: Wrong Answer

Solution code:
import fractions

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

g = fractions.gcd(N, M)

lcm = N * M // g

ln = lcm // N
lm = lcm // M

count_n = 0
count_m = 0

while count_n != (N - 1) or count_m != (M - 1):

    temp_n = count_n * ln
    temp_m = count_m * lm

    if temp_n == temp_m:
        if S[count_n] != T[count_m]:
            print(-1)
            break
    
    if count_n == (N - 1):
        count_m += 1
    elif count_m == (M - 1):
        count_n += 1
    elif temp_n <= temp_m:
        count_n += 1
    else:
        count_m += 1
else:
    print(lcm)

Submission status: Wrong Answer

Solution code:
def gcd(a,b):
    if a==b:
        return b
    elif a%b == 0:
        return b
    else:
        return gcd(b,a%b)

n,m = map(int,input().split())
s = input()
t = input()
if n<m:
    tmp = m
    m = n
    n = m
    tmp = s
    s = t
    t = tmp
lcm = n*m//gcd(n,m)

ok = True
if s[0] != t[0]:
    ok = False

for i in range(1,gcd(n,m)-1):
    if s[n//gcd(n,m)*i] != t[m//gcd(n,m)*i]:
        ok = False
        break
print(lcm if ok else -1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
if N < M:
  tmp = N
  N = M
  M = tmp

a = N
b = M

r = a % b

while r > 0:
  a = b
  b = r
  r = a % b

L = N * M / b

S = input()
T = input()
i = 0
j = 0

while j != M and i != N:
  if i * L / N + 1 == j * L / M + 1:
    if S[i] != T[j]:
      L = -1
      break
    i += 1
    j += 1
  elif i * L / N + 1 >= j * L / M + 1:
    j += 1
  else:
    i += 1

print('%d' % L)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
S = input()
T = input()
ans = 0
a = 2

if S[0] != T[0]:ans = -1

elif N % M != 0 and M % N != 0:
    if N >= M:
        while True:
            if N * a % M == 0:
                ans = N * a
                break
            else:
                a += 1
    elif N < M:
        while True:
            if M * a % N == 0:
                ans = M * a
                break
            else:
                a += 1

elif N % M == 0:
    b = int(N/M)
    ans = N
    for i in range(M):
        if T[i] != S[b*i]:
            ans = -1
            break   

elif M % N == 0:
    b = int(M/N)
    ans = M
    for i in range(N):
        if S[i] != T[b*i]:
            ans = -1
            break   

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np# your code goes here
def eucrid(a, b):
	if (a > b):
		buf = a
		a = b
		b = buf
	if (b % a == 0):
		return a
	else:
		return eucrid(b%a, a)
		
def main():
	buf = input().split()
	s = input()
	t = input()
	n = len(s)
	m = len(t)
	
	big = n*m//eucrid(n, m)
	flag = big
	for i in range(big):
		if (i%n == 0 and i%m == 0):
			#print(i)
			if(s[i//n] != t[i//m]):
				flag = -1
				
	print(flag)
	#print(s, t, n, m)
	
	
main()

Submission status: Wrong Answer

Solution code:
import math
ls, lt = map(int, input().split())
s = list(input())
t = list(input())
def gcd(m,n):
    if n == 0:
        return m
    else:
        return gcd(n,m%n)
g = gcd(ls,lt)
if (s[0]!=t[0]):
    print(-1)
    exit()
elif (g == 1):
    if (ls == 1):
        for i in range(lt):
            if(s[0]!=t[i]):
                print(-1)
                exit()
    elif (lt == 1):
        for i in range(ls):
            if(t[0]!=s[i]):
                print(-1)
                exit()
elif (g == ls):
    d = lt//g
    for i in range(ls):
        if (s[i]!=t[i*d]):
            print(-1)
            exit()
elif (g == lt):
    d = ls//g
    for i in range(lt):
        if (t[i]!=s[i*d]):
            print(-1)
            exit()
print(ls*lt//g)

Submission status: Wrong Answer

Solution code:
import math
ls, lt = map(int, input().split())
s = list(input())
t = list(input())
def gcd(m,n):
    if n == 0:
        return m
    else:
        return gcd(n,m%n)
g = gcd(ls,lt)
if (s[0]!=t[0]):
    print(-1)
    exit()
elif (g == ls):
    d = lt//g
    for i in range(ls):
        if (s[i]!=t[i*d]):
            print(-1)
            exit()
elif (g == lt):
    d = ls//g
    for i in range(lt):
        if (t[i]!=s[i*d]):
            print(-1)
            exit()
print(ls*lt//g)

Submission status: Wrong Answer

Solution code:
import math
ls, lt = map(int, input().split())
s = list(input())
t = list(input())
def gcd(m,n):
    if n == 0:
        return m
    else:
        return gcd(n,m%n)
g = gcd(ls,lt)
if (g == ls):
    d = lt//g
    for i in range(ls):
        if (s[i]!=t[i*d]):
            print(-1)
            exit()
elif (g == lt):
    d = ls//g
    for i in range(lt):
        if (t[i]!=s[i*d]):
            print(-1)
            exit()
print(ls*lt//g)

Submission status: Wrong Answer

Solution code:
# time complexity: O(lg(max{a, b}))
def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)


def lcm(a, b):
    g = gcd(a, b)
    return a // g * b // g * g


N, M = [int(elem) for elem in input().split()]
S = input()
T = input()

L = lcm(N, M)
if L != N and L != M:
    print(L)
elif L == N:
    m = L // M
    for i in range(M):
        idx = m * i
        if S[idx] != T[i]:
            print(-1)
            break
    else:
        print(L)
elif L == M:
    n = L // M
    for i in range(N):
        idx = n * i
        if T[idx] != S[i]:
            print(-1)
            break
    else:
        print(L)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
s=input()
t=input()
#l=n*m*k
import fractions

def agc(n,m,s,t):
    if s[0]!=t[0]:
        return -1
    g=fractions.gcd(n,m)
    if g==1:
        return  n*m
    l=n*m//g

    if s[int(l//m)]!=t[int(l//n)]:
        return -1
    return  int(l)
print(agc(n,m,s,t))

Submission status: Wrong Answer

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

def gcd(a, b):
    while b!= 0:
        a, b = b, a%b
    return a

if N == M:
    if S == T:
        print('N')
    else:
        print(-1)
    
else:
    if S[0] == T[0]:
        lcm = N*M//(gcd(N, M))
        print(lcm)
        
        
        
        
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
N, M = [int(_) for _ in input().split()]
S = input()
T = input()


def gcd(x, y):
    if x < y:
        return gcd(y, x)
    elif y == 0:
        return x
    else:
        return gcd(y, x % y)


g = gcd(N, M)
if g == 1 or S[::N // g] == T[::M // g]:
    print(N * M // g)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
def gcd(a,b): return a if b==0 else gcd(b,a%b)
def lcm(a,b): return a*b//gcd(a,b)
n,m=map(int,input().split())
s=input()
t=input()
l=lcm(n,m)
d={}
f=l
for i in range(max(n,m)):
    o=i*(l//n)
    p=i*(l//m)
    if i<n and o not in d:
        d[o]=s[i]
    elif i<n and o in d and d[o]!=s[i]:
        f=-1
    if i<m and p not in d:
        d[p]=t[i]
    elif i<n and p in d and d[p]!=t[i]:
        f=-1
print(f)

Submission status: Wrong Answer

Solution code:
def gcd(a,b): return a if b==0 else gcd(b,a%b)
def lcm(a,b): return a*b//gcd(a,b)
n,m=map(int,input().split())
s=input()
t=input()
l=lcm(n,m)
d={}
f=l
for i in range(max(n,m)):
    o=i*(l//n)
    p=i*(l//m)
    if i<n and o not in d:
        d[o]=s[i]
    elif i<n and o in d and d[o]!=s[i]:
        f=-1
    if i<m and p not in d:
        d[p]=t[i]
    elif i<n and p in d and d[p]!=s[i]:
        f=-1
print(f)

Submission status: Wrong Answer

Solution code:
N, M = [int(_) for _ in input().split()]
S = input()
T = input()


def gcd(x, y):
    if x % y:
        return gcd(y, x % y)
    else:
        return y


if N < M:
    N, M, S, T = M, N, T, S

G = gcd(N, M)

if G == M and S[::N//M] == T or S[0] == T[0]:
    print(N*M//G)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N, M = [int(_) for _ in input().split()]
S = input()
T = input()


def gcd(x, y):
    if x % y:
        return gcd(y, x % y)
    else:
        return y


if N < M:
    N, M, S, T = M, N, T, S

G = gcd(N, M)

if G == M:
    if sorted(S[:M]) != sorted(T):
        print(-1)
    for i in range(N // G):
        if S[(N//G)*i] != T[i]:
            print(-1)
            exit()
else:
    if S[0] == T[0]:
        print(N*M//G)
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
N, M = [int(_) for _ in input().split()]
S = input()
T = input()


def gcd(x, y):
    if x % y:
        return gcd(y, x % y)
    else:
        return y


if N < M:
    N, M, S, T = M, N, T, S

G = gcd(N, M)

if G == M:
    if S[:M] != T:
        print(-1)
    for i in range(N // G):
        if S[(N//G)*i] != T[i]:
            print(-1)
            exit()
else:
    if S[0] == T[0]:
        print(N*M//G)
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
# https://img.atcoder.jp/agc028/editorial.pdf
N, M = map(int,input().split())
S = input()
T = input()

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

gcded = gcd(N,M)
n = N//gcded
m = M//gcded
for k1 in range(1, gcded):
  for k2 in range(1, gcded):
    if (k1 * n == k2 * m):
      print(-1)
      exit()

print(L)

Submission status: Wrong Answer

Solution code:
import sys
from fractions import gcd

N,M=map(int,input().split())
S=input()
T=input()
L=N * M // gcd(N, M)
X=["" for _ in range(L)]

if S[0] != T[0]:
    print(-1)
else:
    X[0]=S[0]
    
for i in range(1,N-1):
    x=((N-1)*L//N)+1
    if x < L:
        X[x]=S[i]
    else:
        print(-1)
        sys.exit()

for i in range(1,M-1):
    x=((M-1)*L//M)+1
    if x < L:
        if X[x] != "":
            print(-1)
            sys.exit()
    else:
        print(-1)
        sys.exit()

print(L)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
S=input()
T=input()
list_S=list(S)
list_T=list(T)
Number_i=[i for i in range(N-1)]
Number_j=[j for j in range(M-1)]
Kaburi_i=[]
Kaburi_j=[]
counter=0
Flag=0
for i in Number_i:
  if i*M/N in Number_j:
    Kaburi_i.append(i)
    Kaburi_j.append(int(i*M/N))
Kaburi_Size=len(Kaburi_i)
while counter<=Kaburi_Size-1:
  if list_S[Kaburi_i[counter]]==list_T[Kaburi_j[counter]]:
    pass
  else:
    Flag=1
    break
  counter+=1
def gcd(a,b):
  while b>0:
    a,b=b,a%b
  return a
if Flag==1:
  print(-1)
else:
  print(int(N*M/gcd(N,M)))

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
S=input()
T=input()
list_S=list(S)
list_T=list(T)
Number_i=[i for i in range(M-1)]
Number_j=[j for j in range(N-1)]
Kaburi_i=[]
Kaburi_j=[]
counter=0
Flag=0
for i in Number_i:
  if i*N/M in Number_j:
    Kaburi_i.append(i)
    Kaburi_j.append(int(i*N/M))
Kaburi_Size=len(Kaburi_i)
while counter<=Kaburi_Size-1:
  if list_S[Kaburi_i[counter]]==list_T[Kaburi_j[counter]]:
    pass
  else:
    Flag=1
    break
  counter+=1
def gcd(a,b):
  while b>0:
    a,b=b,a%b
  return a
if Flag==1:
  print(-1)
else:
  print(int(N*M/gcd(N,M)))

Submission status: Wrong Answer

Solution code:
def gcd(a, b):
    if a < b:
        return gcd(b, a)
    if b == 0:
        return a
    else:
        return gcd(b, a % b)


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

    g = gcd(N, M)
    for i in range(g):
        if S[i * N // g] != T[i * M // g]:
            print("-1")
            exit()
    print(N * M / g)

main()

Submission status: Wrong Answer

Solution code:
import fractions
n, m = map(int,input().split())
s = input()
t = input()
l =  n * m // fractions.gcd(n, m)
flag = True

for i in range(1, n + m):
    if i % n == 0 and i % m == 0:
        a = l // n
        b = l // m
        if s[a-1] != t[b-1]:
            flag = False

if flag:
    print(l)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import fractions
n, m = map(int,input().split())
s = input()
t = input()

if s in t or t in s:
    print(-1)
    exit()

else:
    print(n * m // fractions.gcd(n, m))

Submission status: Wrong Answer

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


def gcd(a, b):
    a, b = (a, b) if a >= b else (b, a)
    while b > 0:
        a, b = b, a % b
        return a

g = gcd(N, M)
if S[0::N//g] == T[0::M//g]:
    print(N*M//g)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import math

 

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

pair = {N:S,M:T}


def lcd(n,m):
    return int(n * m / gcd(n,m))
def gcd(n,m):
    if n % m == 0:
        return m
    else:
        return gcd(m,n % m)
#    return int(m*n/math.gcd(n,m))
    
def prove(n,m):
    if gcd(n,m)==1:
        return lcd(n,m)
    global pair
    n_i = int(n/gcd(n,m))
    m_i = int(m/gcd(n,m))
    n_list = pair[n][::n_i]
    m_list = pair[m][::m_i]
#    print(c)
#    print(pair[m])
    if n_list[0:len(m_list)]==m_list:
        return lcd(n,m)
    else:
        return -1
n = max(pair.keys())
m = min(pair.keys())
if pair[N][0]!=pair[M][0]:
    print(-1)
else:
    print(prove(n,m))

Submission status: Wrong Answer

Solution code:
import math

 

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

pair = {N:S,M:T}


def lcd(n,m):
    return int(n * m / gcd(n,m))
def gcd(n,m):
    if n % m == 0:
        return m
    else:
        return gcd(m,n % m)
#    return int(m*n/math.gcd(n,m))
    
def prove(n,m):
    if gcd(n,m)==1:
        return lcd(n,m)
    if m==1:
        return lcd(n,m)
    global pair
    n_i = int(n/gcd(n,m))
    m_i = int(m/gcd(n,m))
    n_list = pair[n][::n_i]
    m_list = pair[m][::m_i]
#    print(c)
#    print(pair[m])
    if n_list==m_list:
        return lcd(n,m)
    else:
        return -1
n = max(pair.keys())
m = min(pair.keys())
if pair[N][0]!=pair[M][0]:
    print(-1)
else:
    print(prove(n,m))

Submission status: Wrong Answer

Solution code:
import math

 

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

pair = {N:S,M:T}


def lcd(n,m):
    return int(n * m / gcd(n,m))
def gcd(n,m):
    if n % m == 0:
        return m
    else:
        return gcd(m,n % m)
#    return int(m*n/math.gcd(n,m))
    
def prove(n,m):
    if gcd(n,m)==1:
        return lcd(n,m)
    global pair
    n_i = int(n/gcd(n,m))
    m_i = int(m/gcd(n,m))
    n_list = pair[n][::n_i]
    m_list = pair[m][::m_i]
#    print(c)
#    print(pair[m])
    if n_list==m_list:
        return lcd(n,m)
    else:
        return -1
n = max(pair.keys())
m = min(pair.keys())
if pair[N][0]!=pair[M][0]:
    print(-1)
else:
    print(prove(n,m))

Submission status: Wrong Answer

Solution code:
import math

 

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

pair = {N:S,M:T}


def lcd(n,m):
    return int(n * m / gcd(n,m))
def gcd(n,m):
    if n % m == 0:
        return m
    else:
        return gcd(m,n % m)
#    return int(m*n/math.gcd(n,m))
    
def prove(n,m):
    if gcd(n,m)==1:
        return lcd(n,m)
    global pair
    n_i = int(n/gcd(n,m))
    m_i = int(m/gcd(n,m))
    n_list = pair[n][::n_i]
    m_list = pair[m][::m_i]
#    print(c)
#    print(pair[m])
    if n_list==m_list:
        return lcd(n,m)
    else:
        return -1
n = max(pair.keys())
m = min(pair.keys())
print(prove(n,m))

Submission status: Wrong Answer

Solution code:
import math

 

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

pair = {N:S,M:T}


def lcd(n,m):
    return int(n * m / gcd(n,m))
def gcd(n,m):
    if n % m == 0:
        return m
    else:
        return gcd(m,n % m)
#    return int(m*n/math.gcd(n,m))
    
def prove(n,m):
    global pair
    mul = int(n/m)
    c = "".join([pair[n][mul*i] for i in range(len(pair[m]))])
#    print(c)
#    print(pair[m])
    if c== pair[m]:
        print (n)
    else:
        print (-1)
if max(N,M) % min(N,M) == 0:
    n = max(pair.keys())
    m = min(pair.keys())
    prove(n,m)
else:
    print(lcd(max(N,M),min(N,M)))

Submission status: Wrong Answer

Solution code:
import math

 

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

pair = {N:S,M:T}


def lcd(n,m):
    return int(n * m / gcd(n,m))
def gcd(n,m):
    if n % m == 0:
        return m
    else:
        return gcd(m,n % m)
#    return int(m*n/math.gcd(n,m))
    
def prove(n,m):
    global pair
    mul = int(n/m)
    c = "".join([pair[n][mul*i] for i in range(len(pair[m]))])
#    print(c)
#    print(pair[m])
    if c== pair[m]:
        print (m)
    else:
        print (-1)
if max(N,M) % min(N,M) == 0:
    n = max(pair.keys())
    m = min(pair.keys())
    prove(n,m)
else:
    print(lcd(max(N,M),min(N,M)))

Submission status: Wrong Answer

Solution code:
n,m = (int(i) for i in input().split())
s = input()
t = input()
def gcd(a,b):
	if a%b: return gcd(b,a%b)
	else: return b
g = gcd(n,m)
print(n*m//g)

Submission status: Wrong Answer

Solution code:
n,m = (int(i) for i in input().split())
s = input()
t = input()
def gcd(a,b):
	if a%b: return gcd(b,a%b)
	else: return b
g = gcd(n,m)
print(g)

Submission status: Wrong Answer

Solution code:
0
N, M = map(int, input().split())
S = input()
T = input()

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

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

print ('gcd {}'.format(L))
i = 1
while True:
    exist = True
    for j in range(L*i):
        if j % int(L/N) == 0 and j % int(L/M) == 0 and S[int(j*N/L)] != T[int(j*M/L)]:
            exist = False
            break
    if exist == True:
        print (L*i)
        exit()
    elif L*1 > 10 ** 5:
       print (-1)
       exit()
    else:
        i = i + 1

Submission status: Wrong Answer

Solution code:
N, M = [int(i) for i in input().split()]
S = input()
T = input()

ans = -1

largeNum, smallNum = max(N, M), min(N, M)
longStr = S if N > M else T
shortStr = T if N > M else S
step = largeNum // smallNum

if largeNum % smallNum == 0:
    for si, li in enumerate(range(0, largeNum, step)):
        if not shortStr[si] == longStr[li]:
            break
    else:
        ans = largeNum
else:
    devide = 1
    a = largeNum
    b = smallNum
    while True:
        a -= b
        if a == b:
            devide = a
            break
        else:
            c = a
            a = max(a, b)
            b = min(b, c)

    ans = largeNum * smallNum // devide
print(ans)

Submission status: Wrong Answer

Solution code:
def gcd(a,b):
    """Compute the greatest common divisor of a and b"""
    while b > 0:
        a, b = b, a % b
    return a
    
def lcm(a, b):
    """Compute the lowest common multiple of a and b"""
    return a * b / gcd(a, b)
a,b=map(int, input().split())
s=input()
t=input()
if lcm(a,b)>a+b:
    print(int(lcm(a,b)))
else:
    print("-1")