#include <cstdio>
#include <map>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, n)
int a[81], k, p, q, d;
int main()
{
    while (scanf("%d%d", &p, &q) == 2) {
        d = 0;
        a[0] = 0;
        std::map<int, int> m;
        m[p%q] = -1;
        p = p % q * 10;
        rep(i, 81) {
            int n = p / q;
            int r = p % q;
            a[i] = n;
            if (r == 0) { d = 1; break; }
            if (m.find(r) == m.end()) m[r] = i;
            else { k = m[r]; break; }
            p = p % q * 10;
        }
        rep(i, m.size()) printf("%d", a[i]); puts("");
        if (!d) {
            rep(i, k+1) putchar(' ');
            REP(i, k+1, m.size()) putchar('^');
            puts("");
        }
    }
    return 0;
}