def solve1(m,n):

    div=[]

    rem=[]

    r=m

    rem = [-1 for _ in range(n)]

    digit=0

    while True:

        (d,r) = divmod(r,n)

        div.append(d)

        if r==0:

            return(''.join(map(str, div))[1:],'')

        elif rem[r] != -1:

            le = digit - rem[r]

            s = ''.join(map(str,div[1:]))

            t = (' '*100 + '^'*le)[-len(s):]

            return(s,t)

        else:

            rem[r] = digit

            r *= 10

            digit += 1

        

while True:

    try:

        m,n = list(map(int, input().strip().split()))

        s,t = solve1(m, n)

        print(s)

        if t!='':

            print(t)

    except EOFError:

        break
from decimal import *

import re

def solve2(m, n):

    maxreplen = 160

    PREC=200

    getcontext().prec = PREC

    x = Decimal(m) / Decimal(n)

    s = x.to_eng_string()

    if len(s) < PREC:

        return(s[2:],'')

    rep = 1

    while True:

        r = r'(.{%d})\1{%d,}' % (rep, int(maxreplen/rep)-1)

        #r = r'(.{%d})\1{%d,}' % (rep, 20)

                                         #ex. '(.{6})\\1{12,}'

        a=re.search(r, s)

        if a:

            break

        rep += 1

        if rep > maxreplen:

            raise ValueError('This cannot happen.')



    u = s[2:a.start()+len(a.group(1))]

    v = (' '*PREC + '^'*len(a.group(1)))[-len(u):]

    return(u,v)



while True:

    try:

        m,n = list(map(int, input().strip().split()))

        s,t = solve2(m, n)

        print(s)

        if t!='':

            print(t)

    except EOFError:

        break
def solve1(m, n):
    div = []
    rem = []
    r = m
    rem = [-1 for _ in range(n)]
    digit = 0
    while True:
        (d, r) = divmod(r, n)
        div.append(d)
        if r == 0:
            return ("".join(map(str, div))[1:], "")
        elif rem[r] != -1:
            le = digit - rem[r]
            s = "".join(map(str, div[1:]))
            t = (" " * 100 + "^" * le)[-len(s) :]
            return (s, t)
        else:
            rem[r] = digit
            r *= 10
            digit += 1


while True:
    try:
        m, n = list(map(int, input().strip().split()))
        s, t = solve1(m, n)
        print(s)
        if t != "":
            print(t)
    except EOFError:
        break

from decimal import *
import re


def solve2(m, n):
    maxreplen = 160
    PREC = 200
    getcontext().prec = PREC
    x = Decimal(m) / Decimal(n)
    s = x.to_eng_string()
    if len(s) < PREC:
        return (s[2:], "")
    rep = 1
    while True:
        r = r"(.{%d})\1{%d,}" % (rep, int(maxreplen / rep) - 1)
        # r = r'(.{%d})\1{%d,}' % (rep, 20)
        # ex. '(.{6})\\1{12,}'
        a = re.search(r, s)
        if a:
            break
        rep += 1
        if rep > maxreplen:
            raise ValueError("This cannot happen.")
    u = s[2 : a.start() + len(a.group(1))]
    v = (" " * PREC + "^" * len(a.group(1)))[-len(u) :]
    return (u, v)


while True:
    try:
        m, n = list(map(int, input().strip().split()))
        s, t = solve2(m, n)
        print(s)
        if t != "":
            print(t)
    except EOFError:
        break

-def solve1(m, n): -    div = [] -    rem = [] -    r = m -    rem = [-1 for _ in range(n)] -    digit = 0 +from decimal import * +import re + + +def solve2(m, n): +    maxreplen = 160 +    PREC = 200 +    getcontext().prec = PREC +    x = Decimal(m) / Decimal(n) +    s = x.to_eng_string() +    if len(s) < PREC: +        return (s[2:], "") +    rep = 1 -        (d, r) = divmod(r, n) -        div.append(d) -        if r == 0: -            return ("".join(map(str, div))[1:], "") -        elif rem[r] != -1: -            le = digit - rem[r] -            s = "".join(map(str, div[1:])) -            t = (" " * 100 + "^" * le)[-len(s) :] -            return (s, t) -        else: -            rem[r] = digit -            r *= 10 -            digit += 1 +        r = r"(.{%d})\1{%d,}" % (rep, int(maxreplen / rep) - 1) +        # r = r'(.{%d})\1{%d,}' % (rep, 20) +        # ex. '(.{6})\\1{12,}' +        a = re.search(r, s) +        if a: +            break +        rep += 1 +        if rep > maxreplen: +            raise ValueError("This cannot happen.") +    u = s[2 : a.start() + len(a.group(1))] +    v = (" " * PREC + "^" * len(a.group(1)))[-len(u) :] +    return (u, v) -        s, t = solve1(m, n) +        s, t = solve2(m, n)