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:
N, M = map(int, input().split())
S = str(input())
T = str(input())


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


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


start = get_lcm(N, M, get_gcd(N, M))
ans = 0
for i in range(100):
    L = (i+1) * start
    flg = 0

    s_ind = [j * (L / N) + 1 for j in range(N)]
    for k in range(M):
        if (k * (L / M) + 1) in s_ind:
            if S[s_ind.index(k * (L / N) + 1)] != T[k]:
                flg = 1
                break

    if flg == 0:
        ans = L
        break

if ans == 0:
    print('-1')
else:
    print(ans)

Submission status: Wrong Answer

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

def get_gcd(a, b):
    # ユークリッド互除法により最大公約数を求める。
    if a < b:
        a, b = b, a

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

def get_lcm(a, b, gcd):
    # ２数を掛けあわせ、最大公約数で割れば最小公倍数が求まる。
    lcm = a * b // gcd
    return lcm

gcd = get_gcd(n,m)
x = 0
good_str_is_exist = True
for k in range(get_lcm(n, m, gcd)):
    if s[0] != t[0]:
        good_str_is_exist = False
        break
    if (k + 1) % n == 0 and (k + 1) % m == 0:
        if s[0] != t[0]:
            good_str_is_exist = False
            break
        else:
            if len(s) == 0 or len(t) == 0:
                good_str_is_exist = False
                break
                s.remove(0)
                t.remove(0)
    x += 1

if good_str_is_exist:
    print(x)
else:
    print('-1')

Submission status: Wrong Answer

Solution code:
def main():
    n, m = list(map(int, input().split(" ")))
    s = input()
    t = input()

    if n < m:
        n, m = m, n
        s, t = t, s

    for i in range(10000):
        if i == 0:
            continue
        if i % n != 0 or i % m != 0:
            continue

        s_spliters = list(range(0, i, i // n))
        t_spliters = list(range(0, i, i // m))

        flag = True
        count = 0
        for t_spliter in t_spliters:
            if t_spliter in s_spliters:
                index = s_spliters.index(t_spliter)
                if s[index] != t[count]:
                    flag = False
                    break
            count += 1

        if flag:
            print(i)
            return
    print(-1)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N, M =  map(int, input().split(' '))
S = input()
T = input()
import numpy as np
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
def lcm(x, y):
    return (x * y) // gcd(x, y)
 
L = lcm(N,M)
#max_n = max(N,M)
#x3 =[i for i in range(1, L, max_n)]
x1 = [1] + [(i *(L/N)) +1for i in range(1, N)] 
x2 = [1]+ [(i *(L/M)) +1for i in range(1, M)] 
x3 = set(x1) & set(x2) 
if len(x3) ==1:
   print(L)
else :
    if all([S[int((i-1)/(L/N))] == T[int((i -1)/(L/M))] for i in x3 if i != 1]) :
        print(L)
    else :
        print(-1)

Submission status: Wrong Answer

Solution code:
input_lines = input().split()
N = int(input_lines[0])
M = int(input_lines[1])
S = input()
T = input()

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

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

L = int(lcm(N,M))
x = 1
count = 0
if N >= M:
    big = N
    small = M
else:
    big = M
    small = N
while True:
    k = L*x
    flag = 1
    for i in range(big):
        if((i-1)*k/big+1 > (small-1)*k/small+1):
            break
        for j in range(small):
            if((i-1)*k/big+1 < (j-1)*k/small+1):
                break
            if((i-1)*k/big+1 == (j-1)*k/small+1):
                if(S[i-1] != T[j-1]):
                    flag = 0
                    count += 1
                    break
        if(flag == 0):
            break
    if(flag == 1):
        print(k)
        break
    if(count == 2):
        print(-1)
        break
    x += 1

Submission status: Wrong Answer

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

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

def enstr(A,g):
    m=''
    for i in range(len(A)//g):
        m+=A[g*i]
    return m
    
if S[0]==T[0]:
    gcd=gcd(N,M)
    if N<M:
        N,M=M,N
        S,T=T,S
    if gcd==M:
        U=enstr(S,gcd)
        check=[i==T[0] for i in U]
        if not (False in check):
            print(N)
        else:
            print(-1)
    else:
        gcm=N*M//gcd
        print(gcm)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys
n,m=map(int,input().split())
s=input()
t=input()
def g(a,b):
    while b:
        a,b=b,a%b
    return a
gcd=g(n,m)
l = n*m / gcd
slist=[1]
tlist=[1]
for i in range(1,n):
    j=i*(l/n)+1 
    jj=int(j)
    if jj > l:
        break
    if j==jj:
        slist.append(j)
for i in range(1,n):
    k=i*(l/m) + 1
    kk=int(k)
    if kk > l:
        break
    if k==kk:
        tlist.append(k)
se=[]
for i in range(int(l)):
    se.append(0)
for i in range(len(tlist)):
    se[int(tlist[i])-1] = t[i]

for i in range(len(slist)):
    if se[int(slist[i]) -1] != 0 and se[int(slist[i]) -1] != s[i]:
        print(-1)
        sys.exit()
print(int(l))

Submission status: Wrong Answer

Solution code:
N, M =  map(int, input().split(' '))
S = input()
T = input()
import numpy as np
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
def lcm(x, y):
    return (x * y) // gcd(x, y)
 
L = lcm(N,M)
#max_n = max(N,M)
#x3 =[i for i in range(1, L, max_n)]
x1 = [1] + [(i *(L/N)) +1for i in range(1, N)] 
x2 = [1]+ [(i *(L/M)) +1for i in range(1, M)] 
x3 = set(x1) & set(x2) 
if len(x3) ==1:
   print("a") 
   print(L)
else :
    if all([S[int((i-1)/(L/N))] == T[int((i -1)/(L/M))] for i in x3 if i != 1]) :
        print(L)
    else :
        print(-1)

Submission status: Wrong Answer

Solution code:
n, m = list(map(int, input().split()))
s = input()
t = input()
flag = False

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

l = int(n*m / gcd(n, m))

if max(n, m) % min(n, m) != 0:
    print(l if s[0] == t[0] else -1)
    exit()

if len(s) < len(t):
    tmp = s
    s = t
    t = tmp

for i in range(1, m):
    if s[i*(n//m)] != t[i]:
        print(-1)
        exit()

print(l)

Submission status: Wrong Answer

Solution code:
n, m = list(map(int, input().split()))
s = input()
t = input()
flag = False

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

l = int(n*m / gcd(n, m))

if max(n, m) % min(n, m) != 0:
    print(l if s[0] == t[0] else -1)
    exit()

if len(s) < len(t):
    tmp = s
    s = t
    t = tmp

if len(s) == len(t):
    if s == t:
        print(len(s))
        exit()
    else:
        print(-1)
        exit()
for j in [1, 2, 3, 5]:
    for i in range(1, m):
        if s[j*i*(n//m)] != t[i]:
            print(-1)
            exit()

print(l)

Submission status: Wrong Answer

Solution code:
def main():
    n, m = list(map(int, input().split(" ")))
    s = input()
    t = input()

    if n < m:
        n, m = m, n
        s, t = t, s

    for i in range((n * m)**2):
        if i == 0:
            continue
        if i % n != 0 or i % m != 0:
            continue

        s_spliters = list(range(0, i, i // n))
        t_spliters = list(range(0, i, i // m))

        print(s_spliters)
        print(t_spliters)
        flag = True
        count = 0
        for t_spliter in t_spliters:
            if t_spliter in s_spliters:
                index = s_spliters.index(t_spliter)
                if s[index] != t[count]:
                    flag = False
                    break
            count += 1

        if flag:
            print(i)
            return
    print(-1)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

S = input()
T = input()

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

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

if x != N and x != M:
    print(x)
else:
    if N == M:
        if S == T:
            print(x)
            exit()
    elif N > M:
        for index, i in enumerate(S):
            if index % int(N // M) == 0:
                if T[int(index / int(N // M))] != i:
                    break
        else:
            print(-1)
            exit()
    else:
        for index, i in enumerate(T):
            if index % int(M // N) == 0:
                if S[int(index / int(M // N))] != i:
                    break
        else:
            print(-1)
            exit()

    print(-1)

Submission status: Wrong Answer

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

if M>N: N,M=M,N

if S[0]!=T[0]:
	print -1
	quit()

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

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


LCM=lcm(N,M)

if gcd(N,M)==1:
	print N*M
else:
	n=N/gcd(N,M)
	m=M/gcd(N,M)
	if n!=1 and m!=1:
		print LCM
	else:
		S_tmp=""
		for i in range(M):
			S_tmp+=S[i*2]
		if S_tmp==T:
			print LCM
		else:
			print -1

Submission status: Wrong Answer

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

S = input()
T = input()

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

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

if x != N and x != M:
    print(x)
else:
    print(-2)

Submission status: Wrong Answer

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

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

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

if N > M:
    a = N
    A = S
    b = M
    B = T
else:
    a = M
    A = T
    b = N
    B = S

if a % b == 0:
    i = 0
    while i < a:
        j = int(i * a / b)
        if A[j] != B[i]:
            ans = -1
            break
        else:
            continue
        i += 1 
        
    ans = (a - 1) / a * lcm(a, b) + 1
            
else:
    if A[0] == B[0]:
        ans = lcm(a, b)
    else:
        ans = -1        
        
print(ans)

Submission status: Wrong Answer

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

if M>N: N,M=M,N


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

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


LCM=lcm(N,M)

if gcd(N,M)==1:
	print N*M
else:
	n=N/gcd(N,M)
	m=M/gcd(N,M)
	if n!=1 and m!=1:
		print LCM
	else:
		S_tmp=""
		for i in range(M):
			S_tmp+=S[i*2]
		if S_tmp==T:
			print LCM
		else:
			print -1

Submission status: Wrong Answer

Solution code:
n, m = list(map(int, input().split()))
s = input()
t = input()
flag = False

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

l = int(n*m / gcd(n, m))

if max(n, m) % min(n, m) != 0:
    print(l if s[0] == t[0] else -1)
    exit()

if len(s) < len(t):
    tmp = s
    s = t
    t = tmp

if len(s) == len(t):
    if s == t:
        print(len(s))
        exit()
    else:
        print(-1)
        exit()
for i in range(1, m):
    if s[i*(n//m)] != t[i]:
        print(-1)
        exit()

print(l)

Submission status: Wrong Answer

Solution code:
import sys
n,m=map(int,input().split())
s=input()
t=input()
def g(a,b):
    while b:
        a,b=b,a%b
    return a
gcd=g(n,m)
l = n*m / gcd
slist=[1]
tlist=[1]
for i in range(1,n):
    j=i*(l/n)+1 
    jj=int(j)
    if jj > l:
        break
    if j==jj:
        slist.append(j)
for i in range(1,n):
    k=i*(l/m) + 1
    kk=int(k)
    if kk > l:
        break
    if k==kk:
        tlist.append(k)
slist=list(filter(lambda x: x <= l, slist))
tlist=list(filter(lambda x: x <= l, tlist))
se=[]
for i in range(int(l)):
    se.append(0)
for i in range(len(slist)):
    se[int(slist[i])-1] = s[i]
for i in range(len(tlist)):
    if se[int(tlist[i]) -1] != 0 and se[int(tlist[i]) -1] != t[i]:
        print(-1)
        sys.exit()
print(int(l))

Submission status: Wrong Answer

Solution code:
# coding:utf-8

n, m = [int(i) for i in input().split()]
s = input()
t = input()

if not s[0] == t[0]:
	print(-1)
	exit()

nl = max(n, m)
while nl < 1000:
	if nl % n or nl % m:
		nl += 1
		continue
	if not max(((n - 1) * nl / n) + 1, ((m - 1) * nl / m) + 1) <= nl:
		nl += 1
		continue

	nb = True
	nl_x = [""] * nl
	for i in range(1, n - 1):
		nl_x[int((i * nl / n) + 1)] = s[i]
	for j in range(1, m - 1):
		if nl_x[int((j * nl / m) + 1)] == "" or nl_x[int((j * nl / m) + 1)] == t[j]:
			nl_x[int((j * nl / m) + 1)] = t[j]
		else:
			nb = False
			break
	else:
		if nb:
			print(nl)
			exit()
	nl += 1
print(-1)

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split(' '))
s=input().split(',')
t=input().split(',')
i=1
i1=0
if (a%b!=0)&(b%a!=0):
	while True:
		i+=1
		i1=i
		if (i%a==0)&(i%b==0):
			break
	print(i1)
else:
	print(-1)

Submission status: Wrong Answer

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

N = len(S)
M = len(T)


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


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


#print(gcd(N, M))
#print(maxkobai(N, M))


L = maxkobai(N, M)
ret = L
#x = ['-' for i in range(L+1)]
x = {}
#x = ['-'] * (L*10)

for l in range(0, L + 1):
    x[l] = ord('-')


for n in range(0, N):
    x[n * (L//N) + 1] = ord(S[n])

# print(x)

for m in range(0, M):
    idx = m * (L // M) + 1
    x_idx = x[idx]
    if x_idx != ord('-') and x_idx != ord(T[m]):
        ret = -1
        break

Submission status: Wrong Answer

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

L = N * M
n, m = N, M
GCD = 0
if n < m:
    n, m = m, n
r = n % m
while r != 0:
    n, m, r = m, r, n%m
GCD = m
LCM = L//m

if GCD == 1 and S[0] == T[0]:
    print(L)
elif S[::LCM//N] == T[::LCM//M]:
    print(LCM)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import math
import fractions

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

# S is shorter
if N > M:
    x = N
    N = M
    M = x
    x = S
    S = T
    T = x

# from python3.5
# LCM = N * M // math.gcd(N, M)
LCM = N * M // fractions.gcd(N, M)

flag = True
if M % N == 0:
    n = M // N
    for i in range(N):
        if S[i] != T[n*i]:
            flag = False
if flag:
    print(LCM)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
def main():
    n, m = list(map(int, input().split(" ")))
    s = input()
    t = input()

    if n < m:
        n, m = m, n
        s, t = t, s

    for i in range(n * m):
        if i == 0:
            continue
        if i % n != 0 or i % m != 0:
            continue

        s_spliters = list(range(0, i, i // n))
        t_spliters = list(range(0, i, i // m))

        flag = True
        count = 0
        for t_spliter in t_spliters:
            if t_spliter in s_spliters:
                index = s_spliters.index(t_spliter)
                if s[index] != t[count]:
                    flag = False
                    break
            count += 1

        if flag:
            print(i)
            return
    print(-1)
if __name__ == "__main__":
    main()

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()

print(lcm(n,m) if n*2==m*3 or (n*2==(m+1)*3 and n!=m*2) or ((n+1)*2==m*3) else -1)

Submission status: Wrong Answer

Solution code:
import math

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

# S is shorter
if N > M:
    x = N
    N = M
    M = x
    x = S
    S = T
    T = x

Submission status: Wrong Answer

Solution code:
# coding:utf-8

n, m = [int(i) for i in input().split()]
s = input()
t = input()

if not s[0] == t[0]:
	print(-1)
	exit()

nl = max(n, m)
while nl < 10**6:
	if nl % n or nl % m:
		nl += 1
		continue
	if not max(((n - 1) * nl / n) + 1, ((m - 1) * nl / m) + 1) <= nl:
		nl += 1
		continue

	nb = True
	nl_x = [""] * nl
	for i in range(1, n - 1):
		nl_x[int((i * nl / n) + 1)] = s[i]
	for j in range(1, m - 1):
		if not nl_x[int((j * nl / m) + 1)] or nl_x[int((j * nl / m) + 1)] == t[j]:
			nl_x[int((j * nl / m) + 1)] = t[j]
		else:
			nb = False
			break
	else:
		if nb:
			print(nl)
			exit()
	nl += 1
print(-1)

Submission status: Wrong Answer

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

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

n, m = map(int, input().split())
s = input()
t = input()
if n == m and s == t:
    print(n)
elif max(n, m) % min(n, m) == 0:
    print(-1)
else:
    print(lcm(n, m))

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)


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

    L=int(lcm(N,M))

    N_ = int(L/N)
    M_ = int(L/M)

    L_ = int(lcm(N_,M_))

    for i in range(0,L-1,L_):
        if(S[int(i/N_)] != T[int(i/M_)]):
            print(-1)
            return 0;

    print(L)
    return 0;

main()

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
s = str(input())
t = str(input())
def get_gcd(a, b):
    if a < b:
        a, b = b, a
    if a % b == 0:
        return b
    else:
        return get_gcd(b, a % b)
def get_lcm(a, b, gcd):
    lcm = a * b // gcd
    return lcm
saisyou = get_lcm(n,m,get_gcd(n,m))
unti = max(saisyou,n,m)
ans = 0
if saisyou != n and saisyou != m:
    for l in range(unti,100000,saisyou):
        num = 0
        for i in range(min(n,m)):
            if 1+(l/n)*i+1 == 1+(l/m)*i+1:
                if s[i] == t[i]:
                    pass
                else:
                    num += 1
                    break
        if num == 0:
            ans = l
            break
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
s = input()
t = input()
def calc(n, m, s, t):
    def gcd(a, b):
        while b:
            a, b = b, a % b
        return a

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

    l = lcm(n, m)

    num_list = []
    t_list = []
    s_len = range(len(s))
    t_len = range(len(t))
    if len(s) >= len(t):
        for i,(j, k) in enumerate(zip(s_len, list(t_len) + [None]* (len(s_len) - len(t_len)))):
            if not i >= n:
                num_list.append(int((i ) * (l / n) + 1))
            if not i >= m:
                t_list.append(int((i ) * (l / m) + 1))
            else:
                break
    else:
         for i,(j, k) in enumerate(zip(list(s_len) + [None]* (len(t_len) - len(s_len)), t_len)):
            if not i >= n:
                num_list.append(int((i ) * (l / n) + 1))
            else:
                break
            if not i >= m:
                t_list.append(int((i ) * (l / m) + 1))
       
    
    check_list = (set(num_list) & set(t_list))
    for i in check_list:
        if s[num_list.index(i)] != t[t_list.index(i)]:
            return -1
    else:
        return l
print(calc(n, m, s, t))

Submission status: Wrong Answer

Solution code:
# coding:utf-8

n, m = [int(i) for i in input().split()]
s = input()
t = input()

if not s[0] == t[0]:
	print(-1)
	exit()

nl = max(n, m)
while nl < 1000:
	if nl % n or nl % m:
		nl += 1
		continue
	if not max(((n - 1) * nl / n) + 1, ((m - 1) * nl / m) + 1) <= nl:
		nl += 1
		continue

	nb = True
	nl_x = [""] * nl
	for i in range(1, n - 1):
		nl_x[int((i * nl / n) + 1)] = s[i]
	for j in range(1, m - 1):
		if not nl_x[int((j * nl / m) + 1)] or nl_x[int((j * nl / m) + 1)] == t[j]:
			nl_x[int((j * nl / m) + 1)] = t[j]
		else:
			nb = False
			break
	else:
		if nb:
			print(nl)
			exit()
	nl += 1
print(-1)

Submission status: Wrong Answer

Solution code:
from fractions import *

def lcm(a,b): 
    return int(abs(a * b) / gcd(a,b) if a and b else 0)

N,M = map(int,input().split())
a = input()
b = input()

if a[0] != b[0]:
    print(-1)
elif N % M != 0 and M % N != 0:
    print(lcm(N,M))
elif N % M == 0:
    # a is the larger string
    step = N // M
    for i in range(M):
        if b[i] != a[i * step]:
            print(-1)
            quit()
    print(N)
else:
    # b is the larger string
    step = M // N
    for i in range(N):
        if a[i] != b[i * step]:
            print(-1)
            quit()
    print(M)

Submission status: Wrong Answer

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

S = input()
T = input()

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

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

if x != N and x != M:
    print(x)
else:
    if N == M:
        if S == T:
            print(x)
            exit()
    elif N > M:
        for index, i in enumerate(S):
            if index % int(N // M) == 0:
                if T[int(index / int(N // M))] != i:
                    break
        else:
            print(x)
            exit()
    else:
        for index, i in enumerate(T):
            if index % int(M // N) == 0:
                if S[int(index / int(M // N))] != i:
                    break
        else:
            print(x)
            exit()

    print(-1)

Submission status: Wrong Answer

Solution code:
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a

N,M = map(int,input().split(" "))
S = input()
T = input()
Answer = int(len(S)*len(T)/gcd(len(S),len(T)))
X = [0 for i in range(Answer)]

if gcd(len(S),len(T)) == 1:
    print(Answer)
    exit()

for i in range(len(S)):
    X[int(i*Answer/len(S))] = S[i]

for i in range(len(T)):
    if (X[int(i*Answer/len(T))] == 0)or(X[int(i*Answer/len(T))] == T[i]):
        continue
    else:
        print(-1)
        exit()
print(Answer)

Submission status: Wrong Answer

Solution code:
n, m = list(map(int, input().split(" ")))
s = input()
t = input()
if n<m:
  s, t = t, s
  n, m = m, n
def gcd(a,b):
  while b > 0:
    a, b = b, a % b
  return a
d = gcd(n, m)
l = n*m // d
res = l

s_sep = n//d
t_sep = m//d

if s[0]==t[0] and s[-1]==t[-1]:
  for si,ti in zip(s[::s_sep], t[::t_sep]):
    if si!=ti:
      res = -1
      break
  print(res)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
import fractions
def lcm(x, y):
    return (int)((x * y) / fractions.gcd(x, y))

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

if S[0] != T[0]:
  flag = False

if N % M == 0 or M % N == 0:
  flag = False

if flag:
  print(lcm(N, M))
else:
  print(-1)

Submission status: Wrong Answer

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

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


if S[0] != T[0]:
    print(-1)
    exit()

if len(S) == len(T):
    if S == T:
        print(len(S))
    else:
        print(-1)
    exit()


gcd1 = gcd(len(S), len(T))
lcm = len(S)*len(T) // gcd1
print(lcm)
exit

Submission status: Wrong Answer

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

data = []
for i in range(1, 100000):
    ma = max([N, M])
    mi = min([N, M])

    if i * ma % mi == 0:
        data.append(i * ma)
    if i * ma > 100000:
        break

for d in data[:1]:
    slist = [j * d / N + 1  for j in range(d)][:N]
    tlist = [j * d / M + 1  for j in range(d)][:M]

    result = []
    for j in [j for j in slist if j in tlist]:
        result.append(S[slist.index(j)] == T[tlist.index(j)])
    if all(result):
        print(d)
        exit(0)

print(-1)

Submission status: Wrong Answer

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

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

print(-1)
exit()

Submission status: Wrong Answer

Solution code:
iN ,iM = [int(_) for _ in input().split()]
sS = input().rstrip()
sT = input().rstrip()

def fgcd(a,b):
    if b :
        return fgcd(b,a%b)
    else:
        return a

if sS[0] != sT[0]:
    print("-1")
    exit()
elif fgcd(iN,iM) == 1:
    print(iN*iM)
    exit()
else:
#iLX = iN * iM // fgcd(iN,iM)  の倍数
    iD = fgcd(iN,iM)
    i = 1
    while True:
        if iN -1  <=  i * iM //iD   or iM -1  <=  i*iN//iD :
            break
        if sS[i*iN//iD ] != sT[i*iM//iD  ] :
            print("-1")
            exit()
        i += 1

    #iLX = iN * iM // iD

    print(iN*iM//iD)

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()

print(lcm(n,m) if n*2==m*3 else lcm(n,m) if (n*2==(m+1)*3 and n!=m*2) else -1)

Submission status: Wrong Answer

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

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

if N==M:
    print(int(N*M)) if S==T else print(-1)
else:
    g = gcd(N, M)
    if g==1:
        print(int(N*M))
    else:
        flag = False
        s, t = N//g, M//g
        ss = list(map(lambda x: int(x*s), list(range(0,g))))
        tt = list(map(lambda x: int(x*t), list(range(0,g))))
        for i,j in zip(ss,tt):
            if S[i]!=T[j]:
                flag = True
        if flag:
            print(-1)
        else:
            print(int(s*t*g))

Submission status: Wrong Answer

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

data = []
for i in range(1, 100000):
    ma = max([N, M])
    mi = min([N, M])

    if i * ma % mi == 0:
        data.append(i * ma)
    if i * ma > 10000:
        break

for d in data[:1]:
    slist = [j * d / N + 1  for j in range(d)][:N]
    tlist = [j * d / M + 1  for j in range(d)][:M]

    result = []
    for j in [j for j in slist if j in tlist]:
        result.append(S[slist.index(j)] == T[tlist.index(j)])
    if all(result):
        print(d)
        exit(0)

print(-1)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
s = str(input())
t = str(input())
a = 0
def get_gcd(a, b):
    if a < b:
        a, b = b, a
    if a % b == 0:
        return b
    else:
        return get_gcd(b, a % b)
def get_lcm(a, b, gcd):
    lcm = a * b // gcd
    return lcm
saisyou = get_lcm(n,m,get_gcd(n,m))
if saisyou == n or saisyou == m:
    print(-1)
else:
    print(saisyou)

Submission status: Wrong Answer

Solution code:
from fractions import *

def lcm(a,b): 
    return int(abs(a * b) / gcd(a,b) if a and b else 0)

N,M = map(int,input().split())
a = input()
b = input()

if N % M != 0 and M % N != 0:
    print(lcm(N,M))
elif N % M == 0:
    # a is the larger string
    step = N // M
    for i in range(M-1):
        if b[i] != a[i + step]:
            print(-1)
            quit()
    print(N)
else:
    # b is the larger string
    step = M // N
    for i in range(N-1):
        if a[i] != b[i + step]:
            print(-1)
            quit()
    print(M)

Submission status: Wrong Answer

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

# if S[0] != T[0]:
#     print(-1)
#     exit(0)

data = []
for i in range(1, 10000):
    ma = max([N, M])
    mi = min([N, M])

    if i * ma % mi == 0:
        data.append(i * ma)
    if i * ma > 10000:
        break

for d in data[:1]:
    slist = [j * d / N + 1  for j in range(d)][:N]
    tlist = [j * d / M + 1  for j in range(d)][:M]

    result = []
    for j in [j for j in slist if j in tlist]:
        result.append(S[slist.index(j)] == T[tlist.index(j)])
    if all(result):
        print(d)
        exit(0)

print(-1)

Submission status: Wrong Answer

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

if S[0] != T[0]:
    print(-1)
    exit()


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

gcd1 = gcd(len(S), len(T))
lcm = len(S)*len(T) // gcd1

if len(S) == len(T):
    if S == T:
        print(len(S))
    else:
        print(-1)
    exit()

for i in range(lcm):
    if N*i > len(S) or M*i > len(T):
        break
    if N*i == M*i:
        if S[N*i] != T[M*i]:
            print(-1)
            exit()
print(lcm)

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

# 最小公倍数
def lcm(a, b):
  return a*b/gcd(a, b)


ans = int(lcm(N, M))

# L / len(x)
L_N = int(ans / N)
# M / len(x)
N_M = int(ans / M)

S_list = []
for i in range(1, ans, L_N):
    S_list.append(i)

T_list = []
for i in range(1, ans, N_M):
    T_list.append(i)

intersect = set(T_list) & set(S_list)

S_in = [x for x, y in enumerate(S_list) if y in intersect]
T_in = [x for x, y in enumerate(T_list) if y in intersect]

flg = True
for x, y in zip(S_in, T_in):
    if S[x] == T[y]:
        continue
    else:
        flg = False

if flg:
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

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

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

def enstr(A):
    S=''
    for i in A:
        S+=i
    return S
    
if S[0]==T[0]:
    gcd=gcd(N,M)
    if N<M:
        N,M=M,N
        S,T=T,S
    if gcd==M:
        if S==T*(N//M):
            print(N)
        else:
            print('-1')
    else:
        gcm=N*M//gcd
        print(gcm)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
def LCM(a, b):
    tmp = a * b
    while b != 0:
        a, b = b, a % b
    return tmp // a

N, M = map(int, input().split())
s = input()
t = input()
n = LCM(N, M)
a = n // N
b = n // M
for i in range(min((N - 1) // b, (N - 1) // a)):
    if s[b * i] != t[a * i]:
        print("-1")
        break

else:
    print(n)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
s = input()
t = input()
def calc(n, m, s, t):
    def gcd(a, b):
        while b:
            a, b = b, a % b
        return a

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

    l = lcm(n, m)

    num_list = []
    t_list = []
    s_len = range(len(s))
    t_len = range(len(t))
    for i,(j, k) in enumerate(zip(s_len, list(t_len) + [None]* (len(s_len) - len(t_len)))):
        if not i >= n:
            num_list.append(int((i ) * (l / n) + 1))
        if not i >= m:
            t_list.append(int((i ) * (l / m) + 1))
    
    check_list = (set(num_list) & set(t_list))
    for i in check_list:
        if s[num_list.index(i)] != t[t_list.index(i)]:
            return -1
    else:
        return l
print(calc(n, m, s, t))