#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

int main() {
  int p, q;
  while (scanf("%d %d", &p, &q) == 2) {
    vector<int> ds, rs;
    int s = -1;
    for (;;) {
      p *= 10;
      ds.push_back(p / q);
      p %= q;
      if (!p)
        break;
      vector<int>::iterator it = find(rs.begin(), rs.end(), p);
      if (it != rs.end()) {
        s = it - rs.begin() + 1;
        break;
      }
      rs.push_back(p);
    }
    for (int i = 0; i < ds.size(); i++)
      printf("%d", ds[i]);
    puts("");
    if (s >= 0) {
      for (int i = 0; i < s; i++)
        putchar(' ');
      for (int i = s; i < ds.size(); i++)
        putchar('^');
      puts("");
    }
  }
  return 0;
}