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