#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<cstdio>
#include<cstring>
#include<cstdio>
using namespace std;

#define rep(i,n) for(int i = 0;i<n;i++)
#define REP(n) rep(i,n)
#define all(n) n.begin(),n.end()

const int MAX = 185;

int main()
{
    int p, q;
    while((cin >> p >> q)&& !cin.eof() )
    {
        int quot[MAX] = {}, resi[MAX] = {};
        int b = -1, e = -1, iscycle = 1;
        for(int i = 0; p != 0;i++)
        {
            quot[i] = p / q;
            resi[i] = p % q;
            p = (p % q) * 10;

            rep(j,i)if(resi[i] == resi[j]){ b = j; e = i ; break; }
            if(b != -1) break;

            if(resi[i] == 0){ e = i + 1; iscycle = 0; break;}
        }
        if(iscycle)
        {
            REP(e) cout << quot[i + 1] ; cout << endl;
            REP(e)
            {
                if(i < b) cout << " ";
                else cout << "^";
            }
            cout << endl;
        }
        else { REP(e) cout << quot[i + 1]; cout << endl;}
    }

    return 0;
}