#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
  
int main() {
    long long int p, q;
    while(cin >> p >> q) {
        p *= 10;
        vector<long long int> mod_vec;
        
        string ans = "", hat = "";
        while(1) {
            long long int mo = p % q;

            // ?????°
            for(size_t i=0; i<mod_vec.size(); i++) {
                if(mod_vec[i] == mo && (char)(p/q + '0') == ans[i]) {
                    for(size_t x=i; x<hat.size(); x++) {
                        hat[x] = '^';
                    }
                    goto print_ans;
                }
            }

            hat += " ";
            ans += (char)('0' + p / q);

            // ???????????????
            if(mo == 0) goto print_ans;

            mod_vec.push_back(mo);
            p = mo * 10;
        }
    print_ans:
        cout << ans << endl;
        if(hat.find('^') != string::npos) cout << hat << endl;
    }
    return 0;
}