#include <iostream>

using namespace std;

int devide[83], modulo[83];

int main()
{
    int p, q;
    while(cin >> p >> q) {
        bool loop = false;
        int i, j;
        for(i = 0; i < 80 && !loop ; i++) {
            p *= 10;
            devide[i] = p / q;
            modulo[i] = p % q;
            if(modulo[i] == 0) {
                break;
            }
            for(j = 0; j < i; j++) {
                if(devide[i] == devide[j] && modulo[i] == modulo[j]) {
                    loop = true;
                    i -= 2;
                    break;
                }
            }
            p = modulo[i];
        }

        for(int k = 0; k <= i; k++) {
            cout << devide[k];
        }
        cout << endl;
        if(loop) {
            for(int l = 0; l <= i; l++) {
                if(l < j) {
                    cout << " ";
                } else {
                    cout << "^";
                }
            }
            cout << endl;
        }
    }
}