#include <iostream>
#include <algorithm>
#include <map>
using namespace std;

int p, q;
map<int,int> m;

int main() {
  while(cin >> p >> q) {
    m.clear();
    p *= 10;
    m[p] = 0;
    string res = "";
    string res2 = "";
    while(p) {
      int d = p/q;
      p = (p - d*q)*10;
      res += d+'0';
      if(m.count(p)) {
        int i = m[p];
        res2 = string(i, ' ') + string(res.size()-i, '^');
        break;
      }
      m[p] = res.size();
    }
    cout << res << endl;
    if(res2.size()) cout << res2 << endl;
  }
  return 0;
}