#include <vector>
#include <iostream>
using namespace std;
int main(){
    int q,p;
    int di;
    int f = 0,count;
    vector<int> devide,remain;
    vector<int>::iterator it;
    while((cin >> p >> q)) {
        devide.clear();
        remain.clear();
        while(true) {
            count = 0;
            f = 0;
            di = p/q;
            p = p%q*10;
            it = remain.begin();
            while(it != remain.end()) {
                ++count;
                if (*it == p) {
                    f = 1;
                    break;
                }
                ++it;
            }
            devide.push_back(di);
            if (p == 0 || f == 1) {
                break;
            }
            remain.push_back(p);
        }
        it = devide.begin()+1;
        while(it != devide.end()) {
            cout << *it;
            ++it;
        }
        cout << endl;
        if (f == 1) {
            int i;
            for(it = devide.begin()+1, i = 0; it != devide.end() ; ++it, ++i) {
                if (i < count-1) {
                    cout << " ";
                }
                else {
                    cout << "^";
                }
            }
            cout << endl;
        }
    }
}