Problem:
 Score : 100 points    Problem Statement You are given two strings s and t consisting of lowercase English letters and an integer L .  We will consider generating a string of length L by concatenating one or more copies of s and t . Here, it is allowed to use the same string more than once.  For example, when s =  at ,  t =  code and L = 6, the strings atatat ,  atcode and codeat can be generated.  Among the strings that can be generated in this way, find the lexicographically smallest one. In the cases given as input, it is always possible to generate a string of length L .      Constraints  1 ≤ L ≤ 2 × 10^5  1 ≤ |s|, |t| ≤ L  s and t consist of lowercase English letters.  It is possible to generate a string of length L in the way described in Problem Statement.         Input Input is given from Standard Input in the following format:  N  x_1  s_1  x_2  s_2  :  x_N  s_N       Output Print the lexicographically smallest string among the ones that can be generated in the way described in Problem Statement.        Sample Input 1 6
at
code      Sample Output 1 atatat  This input corresponds to the example shown in Problem Statement.       Sample Input 2 8
coding
festival      Sample Output 2 festival  It is possible that either s or t cannot be used at all in generating a string of length L .       Sample Input 3 8
same
same      Sample Output 3 samesame  It is also possible that s = t .       Sample Input 4 10
coin
age      Sample Output 4 ageagecoin

Solution (Python):
n=int(input())
a=sorted([input()for i in range(2)])
s,t=len(a[0]),len(a[1])
c=[]
for i in range(n):
    if (n-i*t)%s==0:
        c.append(a[0]*((n-i*t)//s)+a[1]*i)
        break
for i in range(n):
    if (n-i*s)%t==0:
        c.append(a[1]*((n-i*s)//t)+a[0]*i)
        break
print(sorted(c)[0])

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from fractions import gcd

L = int(readline())
S = readline().rstrip().decode('utf-8')
T = readline().rstrip().decode('utf-8')

ST = S + T
TS = T + S
if ST > TS:
    S,T = T,S



LS = len(S)
LT = len(T)

n = 0
for x in range(L+1):
    if (L - LS * x) % LT == 0:
        n = x
        break

n1, m1 = n, (L - LS * n) // LT
d = gcd(LS,LT)
n2, m2 = n1, m1
dn, dm = LT//d, LS//d
while m2 >= dm:
    m2 -= dm
    n2 += dn

x = S * n1 + T * m1
y = S * n2 + T * m2
answer = min(x,y)
print(answer)

Solution (Python):
L = int(input())
Ss = input()
Ts = input()

if Ss+Ts > Ts+Ss:
    Ss, Ts = Ts, Ss

lenS, lenT = len(Ss), len(Ts)

for numS in reversed(range(L//lenS+1)):
    numT = (L-lenS*numS) / lenT
    if int(numT) == numT:
        ans = Ss*numS + Ts*int(numT)
        print(ans)
        break

Solution (Python):
l = int(input())
a = input()
b = input()

if len(a) > len(b):
    k = b
    b = a
    a = k

mina = None
maxa = None
"""
f = [None] * (l + 1)

f[0] = ''

for i in range(l):
    tmpa = None
    tmpb = None
    if i + 1 >= len(a) and f[i + 1 - len(a)] is not None:
        tmpa = a + f[i + 1 - len(a)]
    if i + 1 >= len(b) and f[i + 1 - len(b)] is not None:
        tmpb = b + f[i + 1 - len(b)]
    if tmpa is None and tmpb is None:
        continue
    if tmpa is None and tmpb is not None:
        f[i+1] = tmpb
        continue
    if tmpb is None and tmpa is not None:
        f[i+1] = tmpa
        continue
    if tmpa < tmpb:
        f[i+1] = tmpa
    else:
        f[i+1] = tmpb
print(f[l])

def less(a, b):
    if a < b:
        if not b.startswith(a):
            return True
        else:
            c = b
            while c.startswith(a):
                c = c[len(a):]
            if a < c:
                return True
            else:
                return False

"""
for i in range(l+ 1) :
    if i * len(a) > l:
        break
    if (l - i * len(a)) % len(b) == 0:
        if mina is None:
            mina = i
            maxb = (l - i * len(a)) // len(b)
        maxa = i
        minb = (l - i * len(a)) // len(b)


ans1 = a * maxa + b * minb
ans2 = b * maxb + a * mina

if ans1 < ans2:
    print(ans1)
else:
    print(ans2)

Solution (Python):
def solve(l, s, t):
    queue = [(1, 0), (0, 1)]

    def gcd(a, b):
        r = a % b
        if r:
            d = a // b
            sb = queue.pop()
            sa = queue.pop()
            queue.append(sb)
            queue.append(tuple(x - d * y for x, y in zip(sa, sb)))
            return gcd(b, r)
        else:
            return b

    ls, lt = len(s), len(t)
    if ls > lt:
        s, t, ls, lt = t, s, lt, ls
    if (s * (lt // ls + 1) * 2)[:lt * 2] > t * 2:
        s, t, ls, lt = t, s, lt, ls
    g = gcd(ls, lt)
    l //= g
    ls //= g
    lt //= g
    a, b = queue[-1]
    a *= l
    b *= l
    k = b // ls
    b -= ls * k
    a += lt * k
    return s * a + t * b


l = int(input())
s = input()
t = input()
print(solve(l, s, t))

Solution (Python):
import sys
L = input()
s1,s2 = raw_input(),raw_input()
l1,l2 = len(s1), len(s2)

if l1 == l2:
  print( min(s1,s2) * (L//l1) )
  sys.exit()

cands = []
n1 = 0
while True:
  if (L - n1*l1) % l2 == 0:
    n2 = (L - n1*l1) // l2
    cands.append(s1*n1 + s2*n2)
    cands.append(s2*n2 + s1*n1)
    break
  n1 += 1

n2 = 0
while True:
  if (L - n2*l2) % l1 == 0:
    n1 = (L - n2*l2) // l1
    cands.append(s1*n1 + s2*n2)
    cands.append(s2*n2 + s1*n1)
    break
  n2 += 1

print(min(cands))

Solution (Python):
L = int(input())
s = input()
t = input()
if not s+t <= t+s:
    s, t = t, s
ls = len(s); lt = len(t)
for i in range(L // ls, -1, -1):
    if (L - ls*i) % lt == 0:
        ans = s * i + t * ((L - ls*i) // lt)
        break
print(ans)

Solution (Python):
def main():
    L = int(input())
    S, T = (input() for _ in [0] * 2)

    ans = []
    apnd = ans.append
    a = S
    b = T
    i = 0
    for _ in [0] * 2:
        while 1:
            x = L - (len(b) * i)
            div_, mod_ = divmod(x, len(a))
            if not mod_:
                apnd(a * div_ + b * i)
                break
            i += 1
        i = 0
        a, b = b, a
    print(min(ans))

main()