#include <iostream>
#include <map>
using namespace std;
typedef map<int, int> M;

int main() {
  int i, j, k, p, q, r, t;
  string s;
  M m;
  pair<M::iterator, bool> res;
  while (cin >> p >> q) {
    r = p%q;
    i = 0;
    s.clear();
    m.clear();
    while (1) {
      r *= 10;
      t = r/q;
      r = r - t*q;
      res = m.insert(M::value_type(r, i));
      s += (char)('0' + t);
      if (!r) {
        cout << s << endl;
        break;
      }
      if (!res.second) {
        cout << s << endl;
        j = res.first->second;
        for (k=0; k<=j; k++) cout << " ";
        for (k=j+1; k<=i; k++) cout << "^";
        cout << endl;
        break;
      }
      i++;
    }
  }
  return 0;
}