#include <iostream>
#include <cstdio>
#include <vector>
#include <list>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <numeric>
#include <sstream>
#include <queue>
#include <iomanip>
using namespace std;

#define REP(i,n) for(int (i)=0; (i)<(n); (i)++)
#define FOR(i,a,b) for(int (i)=(a); (i)<(b); (i)++)
#define PUSH(n,v) for(int i=0; i<(n); i++) {int j; cin >> j; v.push_back(j);}
#define ALL(v) v.begin(), v.end()
#define print(s) cout << (s) << endl

void solve113(int p, int q) {
    vector<int> fractional, remainder;
    int beginIndex = -1;
    while (p) {
        int i = p * 10 / q;
        int j = p * 10 % q;
        if (find(ALL(remainder), j) != remainder.end()) {
            fractional.push_back(i);
            beginIndex = find(ALL(remainder),j) - remainder.begin();
            break;
        }else {
            fractional.push_back(i);
            remainder.push_back(j);
        }
        p = j;
    }
    REP(i,fractional.size()-1) {
        cout << fractional[i];
    }
    cout << fractional.back() << endl;
    if (beginIndex != -1) {
        REP(i,fractional.size()-1) {
            if (i >= beginIndex) cout << "^";
            else cout << " ";
        }
        cout << endl;
    }
}

int main() {
    int p, q;
    while (cin >> p >> q) {
        solve113(p,q);
    }
    return 0;
}