#include <iostream>

using namespace std;

int main()
{
    int p, q;
    while(cin >> p >> q) {
        int r[82], s[82];
        s[0] = p / q; r[0] = p % q;
        int top = -1, tail = -1, i;
        bool find = false;
        for(i = 1; !find && i <= 81; i++) {
            r[i] = (r[i-1] * 10) % q;
            s[i] = (r[i-1] * 10) / q;
            if(r[i] == 0) break;
            for(int j = 1; j < i; j++) {
                if(r[i] == r[j]) {
                    tail = j + 1; top = i;
                    find = true;
                }
            }
        }
        for(int k = 1; k < i; k++) {
            cout << s[k];
        }
        cout << endl;
        if(find) {
            for(int k = 1; k < i; k++) {
                if(tail <= k && k <= top) {
                    cout << "^";
                } else {
                    cout << " ";
                }
            }
            cout << endl;
        }
    }
}