#include <iostream>
using namespace std;

int main() {
    int p, q;
    while (cin >> p >> q) {
        int prev[1000010];
        int hist[1000010];
        int ind = 0;

        fill(prev, prev + 1000010, -1);
        fill(hist, hist + 1000010, -1);

        while (true) {
            prev[p] = ind;
            hist[ind++] = 10 * p / q;
            p = 10 * p % q;
            if (p == 0 || prev[p] >= 0) {
                break;
            }
        }

        for (int j = 0; j < ind; ++j) {
            cout << hist[j];
        }
        cout << endl;
        if (p != 0) {
            for (int j = 0; j < prev[p]; ++j) {
                cout << ' ';
            }
            for (int j = prev[p]; j < ind; ++j) {
                cout << '^';
            }
            cout << endl;
        }
    }
    return 0;
}