#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for( int i = 0; i < n; i++ )
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000


int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int p, q;
    while (cin >> p >> q) {
        int shou[1000], amari[1000];
        int first = -1, last = -1, iscycle = 1;
        for (int i = 0; p != 0; i++) {
            shou[i] = p / q;
            amari[i] = p % q;
            p = amari[i] * 10;
            /*
            dump(shou[i]);
            dump(amari[i]);
            dump(p);
            */
            // ??????????????????????????¢???
            rep(j, i) {
                if(amari[i] == amari[j]) {
                    first = j;
                    last = i;
                    break;
                }
            }
            if (first != -1) break;
            // ?????????????????¨???
            if(amari[i] == 0) {
                last = i;
                iscycle = 0;
                break;
            }
        }

        // 0 = false, other = true
        if (iscycle) {
            rep(i ,last) cout << shou[i + 1];
            cout << endl;
            rep(i ,last) {
                if (i < first) cout << " ";
                else cout << "^"; 
            }
            cout << endl;
        }
        else {
            //dump(last);
            rep(i, last) {
                cout << shou[i + 1];
            }
            cout << endl;
        }


    }



    return 0;
}